设为首页 加入收藏

TOP

Glide3升级到Glide4碰到的问题汇总以及部分代码修改(二)
2019-09-03 03:36:52 】 浏览:101
Tags:Glide3 升级 Glide4 碰到 问题 汇总 以及 部分 代码 修改
e.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(
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇入职小白随笔之高通项目编译流程 下一篇Flutter学习笔记(16)--Scaffold..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目