一、简介:

介绍两种使用 BitmapTransformation 来实现 Glide 加载圆形图片和圆角图片的方法。Glide 并不能直接支持 Round Pictures ,需要使用 BitmapTransformation 来进行处理。
二、网上的实现方式
这里介绍下网上常见的方式和使用 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();
*/
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# glide加载圆角
# glide加载圆角图片
# glide加载圆形图片
# Android实现仿iOS菊花加载圈动画效果
# Android绘制圆形百分比加载圈效果
# Android自定义加载圈动画效果
# android动态加载布局文件示例
# Android使用控件ImageView加载图片的方法
# Android使用glide加载gif动画设置播放次数
# Android实现跳动的小球加载动画效果
# Android自定义Dialog实现加载对话框效果
# Android通用LoadingView加载框架详解
# Android实现加载圈
# 圆角
# 两种
# 加载
# 是这样
# 网上
# 并不能
# 转换成
# 来实现
# 比较喜欢
# 本质上
# 长宽
# 大家多多
# circleCrop
# return
# outHeight
# source
# static
# private
# outWidth
# pool
相关文章:
官网建站费用明细查询_企业建站套餐价格及收费标准指南
建站主机与虚拟主机有何区别?如何选择最优方案?
如何快速生成专业多端适配建站电话?
大连 网站制作,大连天途有线官网?
中山网站推广排名,中山信息港登录入口?
如何基于云服务器快速搭建个人网站?
韩国服务器如何优化跨境访问实现高效连接?
建站VPS配置与SEO优化指南:关键词排名提升策略
高防服务器如何保障网站安全无虞?
定制建站哪家更专业可靠?推荐榜单揭晓
广德云建站网站建设方案与建站流程优化指南
如何快速搭建高效简练网站?
建站主机选购指南:核心配置与性价比推荐解析
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
如何生成腾讯云建站专用兑换码?
网站设计制作公司地址,网站建设比较好的公司都有哪些?
网站广告牌制作方法,街上的广告牌,横幅,用PS还是其他软件做的?
早安海报制作网站推荐大全,企业早安海报怎么每天更换?
青岛网站建设如何选择本地服务器?
c++怎么实现高并发下的无锁队列_c++ std::atomic原子变量与CAS操作【详解】
如何用PHP工具快速搭建高效网站?
建站之星安装失败:服务器环境不兼容?
平台云上自助建站如何快速打造专业网站?
如何在阿里云域名上完成建站全流程?
弹幕视频网站制作教程下载,弹幕视频网站是什么意思?
网站代码制作软件有哪些,如何生成自己网站的代码?
如何选择高效可靠的多用户建站源码资源?
建站之星在线版空间:自助建站+智能模板一键生成方案
如何基于PHP生成高效IDC网络公司建站源码?
网站建设制作需要多少钱费用,自己做一个网站要多少钱,模板一般多少钱?
宝塔建站后网页无法访问如何解决?
兔展官网 在线制作,怎样制作微信请帖?
东莞专业网站制作公司有哪些,东莞招聘网站哪个好?
如何确保西部建站助手FTP传输的安全性?
潮流网站制作头像软件下载,适合母子的网名有哪些?
如何通过虚拟机搭建网站?详细步骤解析
智能起名网站制作软件有哪些,制作logo的软件?
简单实现Android文件上传
网站企业制作流程,用什么语言做企业网站比较好?
专业型网站制作公司有哪些,我设计专业的,谁给推荐几个设计师兼职类的网站?
建站主机助手选型指南:2025年热门推荐与高效部署技巧
再谈Python中的字符串与字符编码(推荐)
建站之星在线客服如何快速接入解答?
如何在阿里云虚拟主机上快速搭建个人网站?
网站制作价目表怎么做,珍爱网婚介费用多少?
公司网站制作需要多少钱,找人做公司网站需要多少钱?
免费视频制作网站,更新又快又好的免费电影网站?
建站主机服务器选型指南与性能优化方案解析
如何选择PHP开源工具快速搭建网站?
Android自定义listview布局实现上拉加载下拉刷新功能
*请认真填写需求信息,我们会在24小时内与您取得联系。