Android生成带圆角的Bitmap图片
作者:鉴客 时间:2022-09-08 11:18:19
本文实例讲述了Android生成带圆角的Bitmap图片。分享给大家供大家参考。具体如下:
有时候我们在开发Android应用时,会遇到圆角图片的问题,那么,我们如何在Android中用代码来生成圆角Bitmap图片呢?下面这段代码也许能够帮到你。
该方法主要用到了drawRoundRect来画圆角矩形,然后通过drawBitmap来画图片。
//生成圆角图片
public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {
try {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight()));
final float roundPx = 14;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.BLACK);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
final Rect src = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
canvas.drawBitmap(bitmap, src, rect, paint);
return output;
} catch (Exception e) {
return bitmap;
}
}
希望本文所述对大家的Android程序设计有所帮助。
标签:Android,圆角,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决Maven静态资源过滤问题
2023-04-27 21:04:53
![](https://img.aspxhome.com/file/2023/7/92697_0s.png)
c#模拟js escape方法的简单实例
2023-01-26 07:29:24
SpringBoot项目为何引入大量的starter?如何自定义starter?
2022-12-28 21:39:02
![](https://img.aspxhome.com/file/2023/5/78955_0s.webp)
c#连接mdf文件示例分享
2022-12-15 10:48:33
Spring Boot 整合mybatis 与 swagger2
2022-05-01 06:32:17
Java httpClient连接池支持多线程高并发的实现
2022-09-14 04:13:15
Spring Boot中配置定时任务、线程池与多线程池执行的方法
2021-08-08 13:31:30
IDEA中Mybatis的MGB使用逆向工程配置的详细教程
2022-01-04 17:19:50
![](https://img.aspxhome.com/file/2023/3/88583_0s.png)
Java中控制流程语句的深入讲解
2022-07-21 15:29:25
C#移除字符串中的不可见Unicode字符 案例代码
2023-04-28 19:06:06
Java 中的 clone( ) 和 new哪个效率更高
2023-11-09 05:27:44
![](https://img.aspxhome.com/file/2023/6/59326_0s.png)
Spring Cloud Eureka 服务上下线监控的实现
2022-02-18 21:06:15
Springboot启动执行特定代码的方式汇总
2023-08-06 04:42:24
![](https://img.aspxhome.com/file/2023/0/85900_0s.png)
java设计模式学习之装饰模式
2021-10-02 04:22:54
![](https://img.aspxhome.com/file/2023/7/121937_0s.jpg)
Android自定义StickinessView粘性滑动效果
2022-11-22 08:56:20
![](https://img.aspxhome.com/file/2023/9/90179_0s.gif)
验证本机的excel版本的C#代码
2021-11-01 07:13:20
带你了解Java数据结构和算法之队列
2022-07-03 12:45:34
![](https://img.aspxhome.com/file/2023/8/79968_0s.png)
IDEA 端口占用的解决方法(推荐)
2023-09-05 06:05:15
![](https://img.aspxhome.com/file/2023/6/116116_0s.png)
JVM中的GC初识
2022-08-04 08:37:13
![](https://img.aspxhome.com/file/2023/5/113215_0s.jpg)
Java获取e.printStackTrace()打印的信息方式
2022-05-18 05:19:26