一、簡(jiǎn)介:
介紹兩種使用 BitmapTransformation 來(lái)實(shí)現(xiàn) Glide 加載圓形圖片和圓角圖片的方法深纲。Glide 并不能直接支持 Round Pictures 劲妙,需要使用 BitmapTransformation 來(lái)進(jìn)行處理。
二币呵、網(wǎng)上的實(shí)現(xiàn)方式
這里介紹下網(wǎng)上常見(jiàn)的方式和使用 RoundedBitmapDrawable 兩種方法唆途,本質(zhì)上是差不多的:
使用 Canvas 和 Paint 來(lái)繪制
使用 Android.support.v4.graphics.drawable.RoundedBitmapDrawable
PS: RoundedBitmapDrawable 是 support.v4 下的一個(gè)類(lèi),想了解更多没佑,可以閱讀我之前的文章: Android 必知必會(huì)-使用 supportV4 的 RoundedBitmapDrawable 實(shí)現(xiàn)圓角 蛤奢。
實(shí)現(xiàn)圓形圖片:
/**
*
* Glide 圓形圖片 Transform
*/
public class GlideCircleTransform extends BitmapTransformation {
public GlideCircleTransform(Context context) {
super(context);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
return circleCrop(pool, toTransform);
}
private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if (source == null) return null;
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if (result == null) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return result;
}
@Override
public String getId() {
return getClass().getName();
}
}
實(shí)現(xiàn)圓角圖片:
/**
* Glide 圓角 Transform
*/
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
/**
* 構(gòu)造函數(shù) 默認(rèn)圓角半徑 4dp
*
* @param context Context
*/
public GlideRoundTransform(Context context) {
this(context, 4);
}
/**
* 構(gòu)造函數(shù)
*
* @param context Context
* @param dp 圓角半徑
*/
public GlideRoundTransform(Context context, int dp) {
super(context);
radius = Resources.getSystem().getDisplayMetrics().density * 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);
}
}
三、筆者比較喜歡的簡(jiǎn)便的實(shí)現(xiàn)方式
//加載圓角圖片
//加載圓角圖片
public static void loadRoundImage(Context context, String url, final ImageView imageView) {
Glide.with(context)
.load(url)
.placeholder(placeholder)
.error(placeholder)
//.centerCrop() 千萬(wàn)不要加拜秧,加了就沒(méi)有圓角效果了
.transform(new CenterCrop(context), new GlideRoundTransform(context,10))
.into(imageView);
}
//加載圓形圖片
public static void loadCirclePic(final Context context, String url, final ImageView imageView) {
Glide.with(context)
.load(url)
.asBitmap()
.placeholder(placeholder)
.error(placeholder)
.diskCacheStrategy(DiskCacheStrategy.ALL) //設(shè)置緩存
.into(new BitmapImageViewTarget(imageView) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
imageView.setImageDrawable(circularBitmapDrawable);
}
});
}
原因到底是什么呢枉氮? 其實(shí)主要問(wèn)題是在 centerCrop()方法里面。
看centerCrop()方法的源碼可知,也是需要調(diào)用transform()方法的.所以前后共用CenterCrop會(huì)覆蓋掉GlideRoundImage的效果:
public BitmapRequestBuilder<ModelType, TranscodeType> centerCrop() {
return transform(glide.getBitmapCenterCrop());
}
關(guān)于drawableToBitmap的源碼的實(shí)現(xiàn)是這樣的
public static Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的長(zhǎng)寬
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// 取 drawable 的顏色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 建立對(duì)應(yīng) bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立對(duì)應(yīng) bitmap 的畫(huà)布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 內(nèi)容畫(huà)到畫(huà)布中
drawable.draw(canvas);
return bitmap;
}
/**
* RoundedBitmapDrawable 是 V4 下的一個(gè)類(lèi),不能簡(jiǎn)單的通過(guò):強(qiáng)制轉(zhuǎn)換成 BitmapDrawable
* Bitmap bitmap = ((BitmapDrawable)xxx).getBitmap();
*/