Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get preview image from AnimatedDrawable #4

Open
ChenSiLiang opened this issue Feb 18, 2016 · 4 comments
Open

How to get preview image from AnimatedDrawable #4

ChenSiLiang opened this issue Feb 18, 2016 · 4 comments

Comments

@ChenSiLiang
Copy link

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
Copy link
Owner

@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
Copy link
Author

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

@desmond1121
Copy link
Owner

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

@ChenSiLiang
Copy link
Author

@desmond1121 好的 谢谢 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants