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());
}
}
Glide.with(this)
.load(url)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
stvInfo.setLeftIcon(resource);
}
});
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 bitmap = Glide.with(BaseApplication.getAppContext())
.load(url).asBitmap()
.into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
Bitmap bitmap = Glide.with(BaseApplication.getAppContext()).asBitmap().load(url)
.apply(new RequestOptions().override(