在App开放中经常遇到设置ImageView为部分圆角的情况,但是Glide又没有提供这个方法,该怎么办呢?直接上代码!
/**
* @author csc
* @date 2019-01-18
* Todo 设置图片部分圆角
*/
public class RoundedCornersTransform implements Transformation<Bitmap> {
private BitmapPool mBitmapPool;
private float radius;
private boolean isLeftTop, isRightTop, isLeftBottom, isRightBotoom;
/**
* 需要设置圆角的部分
*
* @param leftTop 左上角
* @param rightTop 右上角
* @param leftBottom 左下角
* @param rightBottom 右下角
*/
public void setNeedCorner(boolean leftTop, boolean rightTop, boolean leftBottom, boolean rightBottom) {
isLeftTop = leftTop;
isRightTop = rightTop;
isLeftBottom = leftBottom;
isRightBotoom = rightBottom;
}
/**
* @param context 上下文
* @param radius 圆角幅度
*/
public RoundedCornersTransform(Context context, float radius) {
this.mBitmapPool = Glide.get(context).getBitmapPool();
this.radius = radius;
}
@NonNull
@Override
public Resource<Bitmap> transform(@NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
int finalWidth, finalHeight;
//输出目标的宽高或高宽比例
float scale;
if (outWidth > outHeight) {
//如果 输出宽度 > 输出高度 求高宽比
scale = (float) outHeight / (float) outWidth;
finalWidth = source.getWidth();
//固定原图宽度,求最终高度
finalHeight = (int) ((float) source.getWidth() * scale);
if (finalHeight > source.getHeight()) {
//如果 求出的最终高度 > 原图高度 求宽高比
scale = (float) outWidth / (float) outHeight;
finalHeight = source.getHeight();
//固定原图高度,求最终宽度
finalWidth = (int) ((float) source.getHeight() * scale);
}
} else if (outWidth < outHeight) {
//如果 输出宽度 < 输出高度 求宽高比
scale = (float) outWidth / (float) outHeight;
finalHeight = source.getHeight();
//固定原图高度,求最终宽度
finalWidth = (int) ((float) source.getHeight() * scale);
if (finalWidth > source.getWidth()) {
//如果 求出的最终宽度 > 原图宽度 求高宽比
scale = (float) outHeight / (float) outWidth;
finalWidth = source.getWidth();
finalHeight = (int) ((float) source.getWidth() * scale);
}
} else {
//如果 输出宽度=输出高度
finalHeight = source.getHeight();
finalWidth = finalHeight;
}
//修正圆角
this.radius *= (float) finalHeight / (float) outHeight;
Bitmap outBitmap = this.mBitmapPool.get(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
if (outBitmap == null) {
outBitmap = Bitmap.createBitmap(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(outBitmap);
Paint paint = new Paint();
//关联画笔绘制的原图bitmap
BitmapShader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//计算中心位置,进行偏移
int width = (source.getWidth() - finalWidth) / 2;
int height = (source.getHeight() - finalHeight) / 2;
if (width != 0 || height != 0) {
Matrix matrix = new Matrix();
matrix.setTranslate((float) (-width), (float) (-height));
shader.setLocalMatrix(matrix);
}
paint.setShader(shader);
paint.setAntiAlias(true);
RectF rectF = new RectF(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
//先绘制圆角矩形
canvas.drawRoundRect(rectF, this.radius, this.radius, paint);
//左上角圆角
if (!isLeftTop) {
canvas.drawRect(0, 0, radius, radius, paint);
}
//右上角圆角
if (!isRightTop) {
canvas.drawRect(canvas.getWidth() - radius, 0, canvas.getWidth(), radius, paint);
}
//左下角圆角
if (!isLeftBottom) {
canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint);
}
//右下角圆角
if (!isRightBotoom) {
canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint);
}
return BitmapResource.obtain(outBitmap, this.mBitmapPool);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
}
如何使用?
Glide 4.0+ 的使用
RoundedCornersTransform transform = new RoundedCornersTransform(mContext, DensityUtil.dip2px(10));
transform.setNeedCorner(true, false, true, false);
RequestOptions options = new RequestOptions().placeholder(R.color.color_eee).transform(transform);
Glide.with(mContext).asBitmap().load(mPicturesBeans.get(0).getSrc()).apply(options).into(holder.mPictureComment);
上面的实现效果就是:左上角和左下角为圆角。
Glide 小于4.0的使用:
RoundedCornersTransform transform = new RoundedCornersTransform(mContext, DensityUtil.dip2px(10));
transform.setNeedCorner(true, false, true, false);
Glide.with(this).
load(mPicturesBeans.get(0).getSrc()).
asBitmap().
skipMemoryCache(true).
diskCacheStrategy(DiskCacheStrategy.NONE).
transform(transform).
into(holder.mPictureComment);
---------------------
作者:csc_1024
来源:CSDN
原文:https://blog.csdn.net/csclmf/article/details/86544031
版权声明:本文为博主原创文章,转载请附上博文链接!
- glide 加载圆角图片
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABEIAAAD3CAIAAACW6Gb7AAAgAElEQVR4nOy9e1QbZf74//zO4XvOYz
- Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果
Android Glide加载图片时转换为圆形.圆角.毛玻璃等图片效果 附录1简单介绍了Android开源的图片加载框架.在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬 ...
- RoundedImageView使用吐槽心得(RoundedImageView与Glide加载图片,第一次加载无法圆角问题)
最近使用的时候发现一个问题, RoundedImageView与Glide搭配使用的时候,第一次加载图片(内存中没有),后图片无法圆角,后来尝试各种改,最后想到了一个办法,就是让Glide加载图片的 ...
- Glide加载图片问题记录
Glide加载图片相比于Picasso而言性能较好,又比Fresco轻巧,而且又支持加载gif动图,是Google 推荐.专注平滑的滚动.简单易用.可扩展的一款图片加载框架.但是使用时还是会遇到一些问 ...
- 使用Glide加载Android图片
一.概述 Glide是一个在Android端非常好的图片缓冲工具,总体上来说,他有以下优点 使用简单 自适应程度高 支持常见的图片格式,如jpg,png等 支持多种数据源,网络,本地,资源,Asset ...
- Glide加载图片到自定义的圆形ImageView中不显示
当使用自定义的圆形ImageView时,发现使用Glide加载并设置默认初始图片时,自定义的ImageView一直显示默认图片,无法更新到加载的图片. 使用下面代码可以解决这个问题 Glide.wit ...
- Glide 加载图片
//通过model获取到图片的url,将Url转换成bitmap对象: //设置不保存内存和硬盘缓存, 1 Glide.with(mContext).load(model.getVideoUrl()) ...
- Android中的Glide加载图片
注意:在Android Studio的项目的build.gradle中添加: compile 'com.github.bumptech.glide:glide:3.6.1' 然后同步一下 目录: 使用 ...
- Android Glide 加载图片
0.借鉴文章地址:http://blog.csdn.net/zivensonice/article/details/51835802 和 http://www.cnblogs.com/zhaoyanj ...
随机推荐
- Laravel Scout 开启队列, 自定义queue name和queue connection
scout.php的默认配置: 'queue' => env('SCOUT_QUEUE', false), 修改为: 'queue' => [ 'queue' => env('SCO ...
- C++进程间通信的十一种方法
转载: https://www.cnblogs.com/swunield/articles/3893250.html 进程通常被定义为一个正在运行的程序的实例,它由两个部分组成: 一个是操作系统用来管 ...
- leetcode 买卖股票问题
leetcode121 Best Time to Buy and Sell Stock 说白了找到最大的两组数之差即可 class Solution { public: int maxProfit(v ...
- Linux 驱动——Button驱动3(poll机制)
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- CSV空行问题
当写入CSV时生成的数据会有空行如果加入 newline =‘’ 不会新增空行 writefile = open('result.csv','w',newline =‘’) 原贴 https://bl ...
- Python中常见的序列及其函数
分片:分片操作的实现需要提供两个索引作为边界,第一个包含在分片内,第二个不包含 number =[1,2,3,4,5,6,7,8,9,10] number [3:6] -->[4,5,6] n ...
- 使用linux命令行调整非图形界面分辨率
第一步,调整linux内核显示参数: 打开内核菜单配置列表文件: vi /boot/grub/menu.lst 或者 vi /boot/grub/gurb.conf 在kernel设置一行末尾添加: ...
- ES5数组、对象常用方法总结
数组方法: Array.isArray(items);判断判断一个变量是否包含数组数据: forEach(function(value, index, fullArray){ }); every(fu ...
- 八个commit让你学会爬取京东商品信息
我发现现在不用标题党的套路还真不好吸引人,最近在做相关的事情,从而稍微总结出了一些文字.我一贯的想法吧,虽然才疏学浅,但是还是希望能帮助需要的人.博客园实在不适合这种章回体的文章.这里,我贴出正文的前 ...
- linux服务器ssh免密登录
环境:两台服务器,Park01.Park02,配置ssh免密登录 在Park01执行:ssh-keygen 然后一直回车 生成节点的公钥和私钥,生成的文件会自动放在/root/.ssh目录下 然后 ...