ImageView的刷新与滑翔 [英] ImageView refresh with Glide

查看:279
本文介绍了ImageView的刷新与滑翔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageView的并加载它与滑翔一个图像:

  Glide.with(ImageView.getContext())
    .load(URL)
    .dontAnimate()
    .placeholder(R.drawable.placeholder)
    的.signature(stringSignature)
    .into(新GlideDrawableImageViewTarget(ImageView的){
        @覆盖
        公共无效onResourceReady(GlideDrawable绘制,GlideAnimation阿尼姆){
            super.onResourceReady(绘制,动画);
            progressBar.setVisibility(View.GONE);
        }
    });

而当我想刷新形象,我又只能用新的签名执行与此相同code。它的工作完美,但是当新的装载开始时,可​​视图像被立即消失。


  

有可能保持ImageView的形象,取代它的新图像下载后?



解决方案

这是预期的行为。结果
每次通话时间 .load(X),滑翔通话 .clear()对目标及其相关请求。结果
这就是滑翔如何能够处理它的位图的池,否则就没有办法知道何时回收位图。结果
为了实现这一点,需要两个目标之间切换,这里的核心思想是:

 公开< T>无效loadNextImage(@NonNull T型,
                                  @NonNull BitmapTransformation ...变换){
        // noinspection魔术数字
        INT哈希= model.hash code()+ 31 * Arrays.hash code(转换);
        如果(mLastLoadHash ==哈希)回报;
        Glide.with(mContext).load(模型).asBitmap()转换(转换).into(mCurrentTarget)。
        mLastLoadHash =散列;
    }目标mCurrentTarget;私有类DiaporamaViewTarget扩展ViewTarget< ImageView的,位图> {        @覆盖
        公共无效onResourceReady(位图资源,GlideAnimation&LT ;?超级位图> glideAnimation){
            mLoadedDrawable =新BitmapDrawable(mImageView.getResources(),资源);
           //显示加载的图像
            mCurrentTarget = M previousTarget;

I have one ImageView and one image loaded in it with Glide:

Glide.with(ImageView.getContext())
    .load(url)
    .dontAnimate()
    .placeholder(R.drawable.placeholder)
    .signature(stringSignature)
    .into(new GlideDrawableImageViewTarget(ImageView) {
        @Override
        public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
            super.onResourceReady(drawable, anim);
            progressBar.setVisibility(View.GONE);
        }
    });

and when I want refresh the image, I run this same code again only with new signature. It's working perfectly, but when new loading is started, the visible image is gone immediately.

Question

Is possible keep the image in ImageView and replace it after new image is downloaded?

解决方案

That's the expected behavior.
Each time you call .load(x), Glide call .clear() on the target and its associated request.
That's how Glide is able to handle its pool of Bitmaps, otherwise it would have no way to know when to recycle a Bitmap.
In order to implement this, you need to switch between two Targets, here is the core idea :

    public <T> void loadNextImage(@NonNull T model,
                                  @NonNull BitmapTransformation... transformations) {
        //noinspection MagicNumber
        int hash = model.hashCode() + 31 * Arrays.hashCode(transformations);
        if (mLastLoadHash == hash) return;
        Glide.with(mContext).load(model).asBitmap().transform(transformations).into(mCurrentTarget);
        mLastLoadHash = hash;
    }

Target mCurrentTarget;



private class DiaporamaViewTarget extends ViewTarget<ImageView, Bitmap> {

        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
            mLoadedDrawable = new BitmapDrawable(mImageView.getResources(), resource);
           // display the loaded image 
            mCurrentTarget = mPreviousTarget;

这篇关于ImageView的刷新与滑翔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆