/**
* 转换图片成圆形
*
* @param bitmap
* 传入Bitmap对象
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
} Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output); final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst); paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
} /**
* to圆角Bitmap
* @param bitmap
* @return
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx) {
try {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;// 颜色值(0xff---alpha)
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);// Rect是使用int类型作为数值,RectF是使用float类型作为数值
// --------抗锯齿-------//
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
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, null, rect, paint);
return output;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

最新文章

  1. Character类
  2. C# 使用Conditional特性而不是#if条件编译
  3. CF 706B 简单二分,水
  4. MyEclipse中防止代码格式化时出现换行的情况的设置
  5. 获取RenderedGeometry不对的处理
  6. node开发 npm install -g express-generator@4
  7. 转:AFNetworking 与 UIKit+AFNetworking 详解
  8. 深入理解javascript函数进阶系列第二篇——函数柯里化
  9. 配置SQL Server on Linux(2)
  10. 痞子衡嵌入式:串口调试工具Jays-PyCOM诞生记(4)- 联合调试(vspd, sscom, PyCharm2018.2)
  11. Spring WebFlux, 它是一种异步的, 非阻塞的, 支持背压(Back pressure)机制的Web 开发WebFlux 支持两种编程风(姿)格(势) 使用@Controller这种基于注解
  12. gunicorn启动flask项目的坑
  13. [转]通过Spring Boot三分钟创建Spring Web项目
  14. 论文笔记:Visual Semantic Navigation Using Scene Priors
  15. 为什么NULL指针也能访问成员函数?(但不能访问成员变量)
  16. Oracle 未能加载文件或程序集Oracle.DataAccess
  17. 【Spring源码分析系列】bean的加载
  18. 移动端视频h5表现问题汇总
  19. PHP性能调优,PHP慢日志---善用php-fpm的慢执行日志slow log,分析php性能问题
  20. 〖Linux〗Qt5.2.0+gsoap开发Android的NDK程序遇到错误的解决

热门文章

  1. 认识Nodejs
  2. Pollard-Rho算法求大数质因子
  3. nginx配置url重定向&amp;反代
  4. struct iphdr
  5. mingw w64的下载地址
  6. mysql 遍历所有表并条件查询
  7. D3.js的v5版本入门教程(第八章)—— 坐标轴
  8. OpenFOAM——绕流振动
  9. 欧拉法求解常微分方程(c++)【转载】
  10. 开源一个golang小程序商城后台系统(moshopserver)