Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
一、簡介:
介紹兩種使用 BitmapTransformation 來實(shí)現(xiàn) Glide 加載圓形圖片和圓角圖片的方法。Glide 并不能直接支持 Round Pictures ,需要使用 BitmapTransformation 來進(jìn)行處理。
二、網(wǎng)上的實(shí)現(xiàn)方式
這里介紹下網(wǎng)上常見的方式和使用 RoundedBitmapDrawable 兩種方法,本質(zhì)上是差不多的:
- 使用 Canvas 和 Paint 來繪制
- 使用 Android.support.v4.graphics.drawable.RoundedBitmapDrawable
實(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);
}
}
三、筆者比較喜歡的簡便的實(shí)現(xiàn)方式
//加載圓角圖片
public static void loadRoundImage(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) {
super.setResource(resource);
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCornerRadius(10); //設(shè)置圓角弧度
imageView.setImageDrawable(circularBitmapDrawable);
}
});
}
//加載圓形圖片
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);
}
});
}
關(guān)于drawableToBitmap的源碼的實(shí)現(xiàn)是這樣的
public static Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的長寬
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 的畫布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 內(nèi)容畫到畫布中
drawable.draw(canvas);
return bitmap;
}
/**
* RoundedBitmapDrawable 是 V4 下的一個(gè)類,不能簡單的通過:強(qiáng)制轉(zhuǎn)換成 BitmapDrawable
* Bitmap bitmap = ((BitmapDrawable)xxx).getBitmap();
*/
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)仿iOS菊花加載圈動(dòng)畫效果
- Android繪制圓形百分比加載圈效果
- Android自定義加載圈動(dòng)畫效果
- android動(dòng)態(tài)加載布局文件示例
- Android使用控件ImageView加載圖片的方法
- Android使用glide加載gif動(dòng)畫設(shè)置播放次數(shù)
- Android實(shí)現(xiàn)跳動(dòng)的小球加載動(dòng)畫效果
- Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
- Android通用LoadingView加載框架詳解
- Android實(shí)現(xiàn)加載圈
相關(guān)文章
Android Studio 配置:自定義頭部代碼注釋及添加模版方式
這篇文章主要介紹了Android Studio 配置:自定義頭部代碼注釋及添加模版方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android實(shí)現(xiàn)一個(gè)簡單的單詞本
大家好,本篇文章主要講的是Android實(shí)現(xiàn)一個(gè)簡單的單詞本,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Android使用注解進(jìn)行代碼檢查的實(shí)現(xiàn)方法
這篇文章主要介紹了Android如何使用注解進(jìn)行代碼檢查,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
如何正確實(shí)現(xiàn)Android啟動(dòng)屏畫面的方法(避免白屏)
本篇文章主要介紹了如何正確實(shí)現(xiàn)Android啟動(dòng)屏畫面的方法(避免白屏),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
Android 判斷某個(gè)Activity 是否在前臺(tái)運(yùn)行的實(shí)例
下面小編就為大家分享一篇Android 判斷某個(gè)Activity 是否在前臺(tái)運(yùn)行的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Android入門之計(jì)時(shí)器Chronometer的使用教程
Chronometer是一個(gè)簡單的定時(shí)器,你可以給它一個(gè)開始時(shí)間,并以此定時(shí)。本文將利用個(gè)簡單的示例為大家講解一下它的使用,感興趣的小伙伴可以嘗試一下2022-11-11
Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框(4)
這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06

