Glide3升级到Glide4碰到的问题汇总以及部分代码修改

版权声明:本文为xing_star原创文章,转载请注明出处!html

本文同步自http://javaexception.com/archives/188java

Glide.3x的版本是3.7.0,Glide4.x的版本是4.2.0canvas

Glide3.x中最基础的用法

Glide.with(getActivity()).load(url).into(imageView)

那么在Glide4.x中,其实仍是同样的,最基本的用法不变app

Glide.with(context).load(url).into(imageView)

可是稍微复杂一点的用法就有很大的差别了,接下来咱们一一列举。ide

 

接下来看一个稍微常规点的复杂用法

Glide.with(BaseApplication.getAppContext())
.load(url)
.placeholder(R.drawable.xxx)
.crossFade()
.into(imageView);

升级到Glide4后,更新为了post

DrawableCrossFadeFactory drawableCrossFadeFactory = new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();
Glide.with(BaseApplication.getAppContext())
.load(url)
.apply(new RequestOptions().placeholder(R.drawable.xxx))
.transition(DrawableTransitionOptions.with(drawableCrossFadeFactory))
.into(imageView);

Glide3.x的链式调用,Glide4.x的用法仍是比较繁琐的动画

接下来记录踩得第一个坑ui

 

Glide4.0后占位图和过渡动画冲突 

在实际使用过程当中发现升级到Glide4以后,展位图跟过渡动画存在冲突,最后找到解决办法,Glide4加载url的代码调整为了this

DrawableCrossFadeFactory drawableCrossFadeFactory = new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();
Glide.with(BaseApplication.getAppContext())
.load(url)
.apply(new RequestOptions().placeholder(R.drawable.xxx))
.transition(DrawableTransitionOptions.with(drawableCrossFadeFactory))
.into(imageView);

关键点在于
setCrossFadeEnabled(true)url

 

淡入淡出动画效果

 其实跟上面的同样,Glide3.x中

Glide.with(BaseApplication.getAppContext())
.load(url)
.crossFade()
.placeholder(R.drawable.xxx)
.into(imageView);

用法是这样的

到Glide4.x中

DrawableCrossFadeFactory drawableCrossFadeFactory = new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();
Glide.with(BaseApplication.getAppContext())
.load(url)
.transition(DrawableTransitionOptions.with(drawableCrossFadeFactory))
.apply(new RequestOptions().placeholder(R.drawable.post))
.into(imageView);

须要使用的是transition方法以及apply方法。apply方法里面能够用来设置placeholder,errorr,centerCrop等方法。这个地方跟Glide3.x是不同的,用起来可能会以为别扭。

 

预加载问题

Glide3.x是

Glide.with(BaseApplication.getAppContext())
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE);

升级到Glide4.x以后,有所调整,用的是preload方法

Glide.with(BaseApplication.getAppContext())
.load(url)
.preload();

自定义BitmapTransformation 

升级后有几个方法发生了变动, 在咱们自定义BitmapTransformation实现一些特定的圆角等需求中,Glide3.x中只须要实现getId方法, 而在Glide4.x中,须要重写equals方法,以及hashCode方法,还有updateDiskCacheKey。
咱们以GlideRoundTransform为例,看看两个版本的细微差别:

Glide3.x的代码以下:

public class GlideRoundTransform extends BitmapTransformation {
 
    private static float radius = 0f;
 
    public GlideRoundTransform(Context context) {
        this(context, 4);
    }
 
    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = DisplayUtils.dip2px(dp);
    }
 
    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }
 
    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;
 
        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
 
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }
 
    @Override
    public String getId() {
        return getClass().getName() + Math.round(radius);
    }
}

 

public class GlideRoundTransform extends BitmapTransformation {
 
    private static final String ID = "com.star.wall.glide.GlideRoundTransform";
 
    private float radius = 0f;
 
    public GlideRoundTransform(Context context) {
        this(context, 4);
    }
 
    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = DisplayUtils.dip2px(dp);
    }
 
    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }
 
    private Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;
 
        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }
 
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }
 
    @Override
    public boolean equals(Object o) {
        if (o instanceof GlideRoundTransform) {
            GlideRoundTransform other = (GlideRoundTransform) o;
            return radius == other.radius;
        }
        return false;
    }
 
    @Override
    public int hashCode() {
        return (ID + "_" + radius).hashCode();
    }
 
    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {
        messageDigest.update((ID + "_" + radius).getBytes());
    }
}

若是还有其余的自定义transform需求,能够参考上面的代码做为模板,进行调整。

 

对于只支持设置imageView.setImageDrawable方法的view

加载url的代码Glide3.x中是

Glide.with(this)
.load(url)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
stvInfo.setLeftIcon(resource);
}
});

Glide4.x中是

Glide.with(this)
.load(url)
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
stvInfo.setLeftIcon(resource);
}
});

这一块的关键点是SimpleTarget,经过实现这个抽象类的特定方法,咱们能够获取到drawable,拿到了drawable就能够给imageView设置图片源了,Glide3.x和Glide4.x的区别在于一个是GlideDrawable,一个是Drawable.

 

同步代码中,获取bitmap

在Glide3.x中

Bitmap bitmap = Glide.with(BaseApplication.getAppContext())
.load(url).asBitmap()
.into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();

asBitmap后,调用get()方法,就可以获取到bitmap了,而在Glide4.x中,还得调整下代码。

Bitmap bitmap = Glide.with(BaseApplication.getAppContext()).asBitmap().load(url)
.apply(new RequestOptions().override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)).submit().get();

能够观察下,这两个差别还挺大的,Glide4.x里面是先asBitmap,再load(url),还有就是经过submit().get()的方式获取到bitmap

 

包含centerCrop,thumbnail,placeholder,error等经常使用方法的例子

Glide3中是

Glide.with(this)
        .load(url)
        .centerCrop()
        .thumbnail(0.1f)
        .placeholder(R.drawable.icon_pic_default)
        .error(R.drawable.icon_pic_default)
        .into(imageView);

而Glide4中是

Glide.with(this)
        .load(url)
        .apply(new RequestOptions().centerCrop().placeholder(R.drawable.icon_pic_default).error(R.drawable.icon_pic_default))
        .thumbnail(0.1f)
        .into(imageView);

未完待续。

 

补充资料:

Glide4.0后占位图和过渡动画冲突解决方案 https://www.jianshu.com/p/28f5bcee409f

关于ImageView的几个常见问题 http://javaexception.com/archives/173

Glide处理圆形ImageView http://javaexception.com/archives/182

如何使用Glide加载通知栏头像url http://javaexception.com/archives/19