Fresco-Source-Analysis icon indicating copy to clipboard operation
Fresco-Source-Analysis copied to clipboard

How to get preview image from AnimatedDrawable

Open ChenSiLiang opened this issue 8 years ago • 4 comments

I use the code to get a AnimatedDrawable

if (image instanceof CloseableAnimatedImage) {
            AnimatedImageResult img = ((CloseableAnimatedImage) image).getImageResult();
            AnimatedDrawableFactory animatedDrawableFactory = Fresco.getImagePipelineFactory().getAnimatedDrawableFactory();
            if (animatedDrawableFactory != null) {
                AnimatedDrawableOptions options = new AnimatedDrawableOptionsBuilder().setForceKeepAllFramesInMemory(true).setAllowPrefetching(true).build();
                AnimatedDrawable animatedDrawable = animatedDrawableFactory.create(img, options);
                if (animatedDrawable != null) {
                    return animatedDrawable;
                }
            }

and I want to get a preview image for this AnimatedDrawable I try renderFrame() but did not work hope reply soon!Thanks!

ChenSiLiang avatar Feb 18 '16 08:02 ChenSiLiang

@ChenSiLiang 我看了你在Fresco的issue,老外可能觉得你会oom不告诉你方法= = 这个问题我这几天看下来有一种解决方法,亲测可行:

ControllerListener listener = new BaseControllerListener<ImageInfo>(){
    @Override
    public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable
            animatable) {
        super.onFinalImageSet(id, imageInfo, animatable);

        if(imageInfo instanceof CloseableAnimatedImage){
            AnimatedImageResult result = ((CloseableAnimatedImage) imageInfo).getImageResult();
            if(result == null) return;

            AnimatedImage image = result.getImage();

            if(image != null){

                Bitmap.Config conf = Bitmap.Config.ARGB_8888;
                Bitmap bmp = Bitmap.createBitmap(100, 100, conf);

                image.getFrame(0).renderFrame(100, 100, bmp);  //这里获取到你需要的Bitmap,我设置的是第0帧。 尺寸需要和创建的Bitmap相同

                imageView.setImageBitmap(bmp);  
            }
        }
    }
};

DraweeController controller = Fresco.newDraweeControllerBuilder()
        .setUri("your uri")
        .setAutoPlayAnimations(true)
        .setControllerListener(listener)
        .build();

SimpleDraweeView view = (SimpleDraweeView) findViewById(R.id.drawee_view);
view.setController(controller);

desmond1121 avatar Feb 26 '16 02:02 desmond1121

@desmond1121 的确,后来我也是这样解决的,但是预览帧也是需要做大图压缩,不然很容易引发OOM。然后我一直在研究在RecycleView里怎么整合这种方法,但是一直找不到一种正确回收Bitmap的方法,一旦Bitmap没有及时回收,也容易导致to much Bitmap的异常。 我也试过在RecycleView的生命周期方法里回收,但是它的重用机制比较复杂,又容易导致used a recycled Bitmap异常。 我最后选择在onBindViewHolder,绑定数据之前先赋值null. 谢谢您!

ChenSiLiang avatar Feb 26 '16 02:02 ChenSiLiang

@ChenSiLiang 这个场景有点复杂,祝你好运~ 有啥图片加载方面的问题可以一起研究:)

desmond1121 avatar Feb 26 '16 03:02 desmond1121

@desmond1121 好的 谢谢 ;)

ChenSiLiang avatar Feb 26 '16 03:02 ChenSiLiang