Android中使用Bitmap类将矩形图片转为圆形的方法
作者:神神的蜗牛 时间:2022-01-04 18:47:40
一般要做正圆形图片,只能是正方形的基础上才能实现,否则就变成椭圆了,下面说说如何使长方形的图片生成正圆形图片
废话不多说,没图没真相,先上图吧:
原图:
变成正圆后:
下面上代码:
public static Bitmap makeRoundCorner(Bitmap bitmap)
{
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int left = 0, top = 0, right = width, bottom = height;
float roundPx = height/2;
if (width > height) {
left = (width - height)/2;
top = 0;
right = left + height;
bottom = height;
} else if (height > width) {
left = 0;
top = (height - width)/2;
right = width;
bottom = top + width;
roundPx = width/2;
}
ZLog.i(TAG, "ps:"+ left +", "+ top +", "+ right +", "+ bottom);
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(left, top, right, bottom);
RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
下面再解释下:
由于图片是长方形,所以图片的 宽、高 肯定会有一边要小于另一边,要生成正方形就已最小的一边为基准,再来裁切定位另一边的显示范围
至于圆角的半径则是正方形宽的一半,用过 CSS 的就知道,画圆很方便 border-radius设为 50% 就可以了,都是一个道理
android 的 UI 真是太繁琐了
矩形画个圆角的代码:
public static Bitmap makeRoundCorner(Bitmap bitmap, int px)
{
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
int color = 0xff424242;
Paint paint = new Paint();
Rect rect = new Rect(0, 0, width, height);
RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, px, px, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
标签:Android,圆形,Bitmap
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JAVA通过HttpClient发送HTTP请求的方法示例
2023-08-24 18:45:47
Android 将 android view 的位置设为右下角的解决方法
2022-02-26 19:01:27
RegexOptions.IgnoreCase正则表达式替换,忽略大小写
2022-05-03 17:25:45
如何把JAR发布到maven中央仓库的几种方法
2022-11-08 11:07:06
JAVAsynchronized原理详解
2023-05-17 12:18:32
![](https://img.aspxhome.com/file/2023/8/80318_0s.png)
微服务通过Feign调用进行密码安全认证操作
2023-07-30 02:43:38
![](https://img.aspxhome.com/file/2023/8/57958_0s.png)
使用springboot logback动态获取application的配置项
2023-09-03 21:21:41
Java 判断实体对象及所有属性是否为空的操作
2022-12-06 14:32:07
Spring框架实现AOP添加日志记录功能过程详解
2023-05-31 01:18:19
![](https://img.aspxhome.com/file/2023/2/81962_0s.png)
IDEA中的clean,清除项目缓存图文教程
2022-02-05 09:02:02
![](https://img.aspxhome.com/file/2023/1/75431_0s.jpg)
C# 使用multipart form-data方式post数据到服务器
2023-12-20 21:10:58
Java数组的扩容代码示例
2021-10-10 00:04:21
Java线程中的常见方法(start方法和run方法)
2023-11-16 17:41:32
![](https://img.aspxhome.com/file/2023/4/68694_0s.png)
MyBatis插入数据返回主键的介绍
2023-10-26 03:15:21
mybatis错误之in查询 <foreach>循环问题
2023-11-17 01:10:09
c#文件名/路径处理方法示例
2021-11-28 21:02:40
Java线程池Executor用法详解
2022-02-13 01:21:16
![](https://img.aspxhome.com/file/2023/2/62812_0s.jpg)
Android 如何本地加载pdf文件
2023-05-24 02:11:48
c#模拟js escape方法的简单实例
2023-01-26 07:29:24
C#判断多个文本框是否为空的方法
2022-05-12 23:05:32