desmond1121 / Fresco-Source-Analysis

中文的Fresco源码解读
862 stars 127 forks source link

How to get preview image from AnimatedDrawable #4

Open ChenSiLiang opened 8 years ago

ChenSiLiang commented 8 years ago

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!

desmond1121 commented 8 years ago

@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);
ChenSiLiang commented 8 years ago

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

desmond1121 commented 8 years ago

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

ChenSiLiang commented 8 years ago

@desmond1121 好的 谢谢 ;)