Android中Glide加载圆形图片和圆角图片实例代码
作者:陪你唠嗑 时间:2022-08-06 08:26:59
一、简介:
介绍两种使用 BitmapTransformation 来实现 Glide 加载圆形图片和圆角图片的方法。Glide 并不能直接支持 Round Pictures ,需要使用 BitmapTransformation 来进行处理。
二、网上的实现方式
这里介绍下网上常见的方式和使用 RoundedBitmapDrawable 两种方法,本质上是差不多的:
使用 Canvas 和 Paint 来绘制
使用 Android.support.v4.graphics.drawable.RoundedBitmapDrawable
实现圆形图片:
/**
*
* 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();
}
}
实现圆角图片:
/**
* Glide 圆角 Transform
*/
public class GlideRoundTransform extends BitmapTransformation {
private static float radius = 0f;
/**
* 构造函数 默认圆角半径 4dp
*
* @param context Context
*/
public GlideRoundTransform(Context context) {
this(context, 4);
}
/**
* 构造函数
*
* @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);
}
}
三、笔者比较喜欢的简便的实现方式
//加载圆角图片
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) //设置缓存
.into(new BitmapImageViewTarget(imageView){
@Override
protected void setResource(Bitmap resource) {
super.setResource(resource);
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCornerRadius(10); //设置圆角弧度
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) //设置缓存
.into(new BitmapImageViewTarget(imageView) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
imageView.setImageDrawable(circularBitmapDrawable);
}
});
}
关于drawableToBitmap的源码的实现是这样的
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;
// 建立对应 bitmap
Bitmap bitmap = Bitmap.createBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
/**
* RoundedBitmapDrawable 是 V4 下的一个类,不能简单的通过:强制转换成 BitmapDrawable
* Bitmap bitmap = ((BitmapDrawable)xxx).getBitmap();
*/
来源:http://www.jianshu.com/p/89567c934008?utm_source=tuicool&utm_medium=referral
标签:glide,圆角
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#微信开发之启用开发者模式
2022-07-07 11:24:54
![](https://img.aspxhome.com/file/2023/9/91369_0s.png)
springboot跨域CORS处理代码解析
2022-07-29 21:12:20
![](https://img.aspxhome.com/file/2023/4/69284_0s.png)
mybatis plus自动生成代码的示例代码
2022-03-01 10:00:25
Springboot2.1.6集成activiti7出现登录验证的实现
2022-08-03 10:44:48
![](https://img.aspxhome.com/file/2023/0/77410_0s.png)
java并发编程之ThreadLocal详解
2023-03-15 11:44:33
![](https://img.aspxhome.com/file/2023/5/62115_0s.png)
SpringBoot Jpa分页查询配置方式解析
2023-03-02 10:04:02
winform去掉右上角关闭按钮的方法
2023-02-11 16:31:40
SpringBoot自动配置特点与原理详细分析
2023-11-19 19:15:01
![](https://img.aspxhome.com/file/2023/7/62057_0s.jpg)
SpringBoot深入了解日志的使用
2023-01-06 15:40:34
![](https://img.aspxhome.com/file/2023/7/65177_0s.png)
springBoot controller,service,dao,mapper,model层的作用说明
2022-02-28 15:38:49
![](https://img.aspxhome.com/file/2023/0/98710_0s.jpg)
C# char类型字符转换大小写的实现代码
2021-07-27 19:21:09
java GUI编程之paint绘制操作示例
2023-11-24 17:58:39
Jackson序列化和反序列化忽略字段操作
2022-08-29 14:01:14
SpringBoot使用GraphQL开发Web API实现方案示例讲解
2023-05-17 16:50:22
![](https://img.aspxhome.com/file/2023/8/85418_0s.png)
基于Tomcat7、Java、WebSocket的服务器推送聊天室实例
2023-11-25 23:35:34
![](https://img.aspxhome.com/file/2023/0/60390_0s.jpg)
完美解决Spring声明式事务不回滚的问题
2023-07-12 14:38:50
![](https://img.aspxhome.com/file/2023/1/81371_0s.jpg)
java实现顺时针打印矩阵
2023-06-26 19:17:22
Mybatis中连接查询和嵌套查询实例代码
2021-08-23 13:24:31
SpringBoot部署在tomcat容器中运行的部署方法
2023-08-04 13:02:28
解决logback-classic 使用testCompile的打包问题
2021-07-01 08:16:50