在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 ...
随机推荐
- 剑指Offer 66. 机器人的运动范围 (回溯)
题目描述 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. 例如,当k为18时,机器人能 ...
- 线性求第k大
快排变种. 快排每次只进行部分排序,进入左边或者右边或者当前mid就是答案. 据说期望值是O(n) 然后STL中的 nth_element也是用这个思想. #include <cstdio> ...
- 准备学习wrf
namelist.wps 中的 geog_data_path=后面的文件夹的文件即土地覆被资料:(看下图) 推荐中科院地理所承担的地球系统科学数据共享平台上共享的100m分辨率资料,官网 http: ...
- python基础(八)
一.token加盐处理# import itsdangerous## salt='sdf234^#$@G'# t = itsdangerous.TimedJSONWebSignatureSeriali ...
- Vue双向数据绑定原理
https://www.cnblogs.com/kidney/p/6052935.html?utm_source=gold_browser_extension
- DJango 前三天小结
一 DJango 所有命令: 1下载: 控制台:pip install django== pip install django== -i 源解释器:找到解释器,点击加号搜索django 2创建项目; ...
- JVM垃圾收集器-ParNew收集器
今天我给大家讲讲ParNew收集器. ParNew收集器 ParNew收集器收集器其实就是Serial收集器的多线程版本,除了使用多线程进行垃圾收集之外,其余行为包括Serial收集器可用的所有控制参 ...
- PAT乙级考前总结(二)
简单模拟 1002 写出这个数 (20 分) 1006 换个格式输出整数 (15 分) 又是数数 1016 部分A+B (15 分) 相当于找数字 1018 锤子剪刀布 (20 分) 题目略 此处用了 ...
- java————数组 简单写出一个管理系统
数组的特点 1, 数组是一块连续的空间,下标描述空间的位置. 2, 下标从0开始,最大下标为数组长度—1.(*.length-1) 3, 数组元素都是变量.(就是每个下标对应的内容).变量的类型 ...
- ROS * 了解学习urdf的内容格式及编写
<?xml version="1.0" ?> 声明文件使用xml描述 <robot name="robot_name">定义这是一个机器 ...