xvrh / lottie-flutter

Render After Effects animations natively on Flutter. This package is a pure Dart implementation of a Lottie player.
https://pub.dev/packages/lottie
MIT License
1.16k stars 196 forks source link

Using ResizeImage causing image asset location mismatch #229

Open akindone opened 2 years ago

akindone commented 2 years ago

Flutter provides ResizeImage ,This allows finer control of the size of the image in [ImageCache] and is generally used to reduce the memory footprint of [ImageCache]. But when using ResizeImage in imageProviderFactory, the animation images looks not right.

Lottie.asset(
  'assets/example_with_images/data.json',
  imageProviderFactory: (LottieImageAsset lottieImage) {
    var imageAssetPath = p.url.join(
        p.dirname('assets/example_with_images/data.json'),
        lottieImage.dirName,
        lottieImage.fileName);
    var imageProvider =
        AssetImage(imageAssetPath, bundle: null, package: null);
    // return imageProvider;
    return ResizeImage(imageProvider, width: 600);
  },
),

this video shows the asset location mismatch issue; https://user-images.githubusercontent.com/17843738/185293375-8f4dd717-122c-4667-9118-4641c9a19c25.mp4

If I passing null to imageProviderFactory, it can show correctly as below image shows.

normal image
akindone commented 2 years ago

I finally make it works by modifying the ImageLayer.drawLayout dst rect's width, height as below

//ImageLayer
@override
  void drawLayer(Canvas canvas, Size size, Matrix4 parentMatrix,
      {required int parentAlpha}) {
    //...
    // var dst = Rect.fromLTWH(0, 0, bitmap.width * density,
    //     bitmap.height.toDouble() * density);
    var imageAsset = lottieDrawable.composition.images[layerModel.refId]!;
    var dst = Rect.fromLTWH(0, 0, imageAsset.width * density, imageAsset.height * density);
    canvas.drawImageRect(bitmap, src, dst, paint);
    canvas.restore();
}

The original animation images taskes 70M memory, while using ResizeImage set width 600 only takes 18M. If this code is correct and no other side effect, can you put the changes to the next version? @xvrh

xvrh commented 2 years ago

@akindone thanks for the report and sample code. I see the problem but I'm not really sure what is the correct fix. A user may want to return a different image with a different size (and aspect ratio) and expect the image to be rendered at the new size... Maybe imageProviderFactory should allow to return the size to use?