在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 ...
随机推荐
- 最新vue.js完整视频教程12套
0.1智能社vuejs(1-11章全套) 0.2英文版learing vuejs 0.3Vue.js实战小米阅读开发 0.4走进Vue.js2.0 0.5Vuejs教程45节课 0.6Vue.js+N ...
- http error: "request body stream exhausted"
'request body stream exhausted' after authentication challenge #661 Closed aburgel opened this issu ...
- 三维机翼某一断面的压力系数X-Y曲线绘制——使用tecplot的extract功能
目标:绘制三维物体表面或者某等值面上某一截断线上的压力系数X-Y曲线 Slices不光可以在一个体上切出来一个平面,还可以和一个面相交切出一条曲线,命令是在Slice Details里面的Slice ...
- 20175120彭宇辰 《Java程序设计》第六周学习总结
教材学习内容总结 第七章 一.内部类与外部类的关系 1.内部类可以使用外嵌类的成员变量和方法.2.类体中不可以声明类变量和类方法,外部类可以用内部类声明对象.3.内部类仅供外嵌类使用.4.类声明可以使 ...
- JDBCTM中Statement接口提供的execute、executeQuery和executeUpdate之间的区别
Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 方法execut ...
- centos下源码编译安装MySQL
解压下载的软件压缩包 tar xzvf mysql-5.1.63.tar.gz 进入解压的目录 cd mysql-5.1.63/ 安装需要的依赖包 yum install gcc gcc-c++ ...
- react安装
1. npm install -g create-react-app 2. create-react-app my-app 3. cd my-app npm start 4.浏览器打开 http:/ ...
- java基本数据类型和运算符
一.基本数据类型 种类: 内置数据类型 引用数据类型 1.内置数据类型 一共有八种基本类型,六个数字类型(四个整数类型,两个浮点型),一个布尔型,一个字符类型. (1)byte: byte数据类型是8 ...
- java面试总躲不过的并发(二):volatile原理 + happens-before原则
一.happens-before原则 同一个线程中的,前面的操作 happens-before 后续的操作.(即单线程内按代码顺序执行.但是,在不影响在单线程环境执行结果的前提下,编译器和处理器可以进 ...
- 下一站 java
一直都在windows的圈子里打滚,偶尔玩玩Linux, Python, Java. 可是最近聊起windows的时候,总是觉得有些不得力,比如说,windows下有IE,MSMQ,IIS,普通使用没 ...