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

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

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

Glide3.x中最基础的用法

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

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

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

但是稍微复杂一点的用法就有很大的差异了,接下来我们一一列举。

 

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

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

升级到Glide4后,更新为了

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的用法还是比较繁琐的

接下来记录踩得第一个坑

 

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

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

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)

 

淡入淡出动画效果

 其实跟上面的一样,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实现一些特定的?#27493;?#31561;需求中,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需求,可以参考上面的代码作为模板,进行调整。

 

?#26434;?#21482;支持设置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

如?#38382;?#29992;Glide加载通知栏头像url http://javaexception.com/archives/19