1. /**
  2. * 转换图片成圆形
  3. *
  4. * @param bitmap
  5. * 传入Bitmap对象
  6. * @return
  7. */
  8. public static Bitmap toRoundBitmap(Bitmap bitmap) {
  9. int width = bitmap.getWidth();
  10. int height = bitmap.getHeight();
  11. float roundPx;
  12. float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
  13. if (width <= height) {
  14. roundPx = width / 2;
  15. top = 0;
  16. bottom = width;
  17. left = 0;
  18. right = width;
  19. height = width;
  20. dst_left = 0;
  21. dst_top = 0;
  22. dst_right = width;
  23. dst_bottom = width;
  24. } else {
  25. roundPx = height / 2;
  26. float clip = (width - height) / 2;
  27. left = clip;
  28. right = width - clip;
  29. top = 0;
  30. bottom = height;
  31. width = height;
  32. dst_left = 0;
  33. dst_top = 0;
  34. dst_right = height;
  35. dst_bottom = height;
  36. }
  37.  
  38. Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
  39. Canvas canvas = new Canvas(output);
  40.  
  41. final int color = 0xff424242;
  42. final Paint paint = new Paint();
  43. final Rect src = new Rect((int) left, (int) top, (int) right,
  44. (int) bottom);
  45. final Rect dst = new Rect((int) dst_left, (int) dst_top,
  46. (int) dst_right, (int) dst_bottom);
  47. final RectF rectF = new RectF(dst);
  48.  
  49. paint.setAntiAlias(true);
  50. canvas.drawARGB(0, 0, 0, 0);
  51. paint.setColor(color);
  52. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  53. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  54. canvas.drawBitmap(bitmap, src, dst, paint);
  55. return output;
  56. }
  57.  
  58. /**
  59. * to圆角Bitmap
  60. * @param bitmap
  61. * @return
  62. */
  63. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx) {
  64. try {
  65. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
  66. bitmap.getHeight(), Config.ARGB_8888);
  67. Canvas canvas = new Canvas(output);
  68. final int color = 0xff424242;// 颜色值(0xff---alpha)
  69. final Paint paint = new Paint();
  70. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  71. final RectF rectF = new RectF(rect);// Rect是使用int类型作为数值,RectF是使用float类型作为数值
  72. // --------抗锯齿-------//
  73. paint.setAntiAlias(true);
  74. canvas.drawARGB(0, 0, 0, 0);
  75. paint.setColor(color);
  76. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  77. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  78. final Rect src = new Rect(0, 0, bitmap.getWidth(),
  79. bitmap.getHeight());
  80. canvas.drawBitmap(bitmap, null, rect, paint);
  81. return output;
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. return null;
  85. }
  86. }

Android--Bitmap处理、圆角、圆形的更多相关文章

  1. Android Xfermode 实战 实现圆形、圆角图片

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/42094215,本文出自:[张鸿洋的博客] 1.概述 其实这篇本来准备Androi ...

  2. Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案

     Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案 RoundedBitmapDrawable是Android在support v4的扩展包中新 ...

  3. Android bitmap图片处理

    一.View转换为Bitmap         在Android中所有的控件都是View的直接子类或者间接子类,通过它们可以组成丰富的UI界面.在窗口显示的时候Android会把这些控件都加载到内存中 ...

  4. android ImageView加圆角

    1.attrs添加 <declare-styleable name="RoundImageView"> <attr name="circle" ...

  5. Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现

     Android View加载圆形图片且同时绘制圆形图片的外部边缘边线及边框:LayerDrawable实现 LayerDrawable实现的结果和附录文章1,2,3中的layer-list一致. ...

  6. Android ImageView加载圆形图片且同时绘制圆形图片的外部边缘边线及边框

     Android ImageView加载圆形图片且同时绘制圆形图片的外部边缘边线及边框 在Android早期的开发中,如果涉及到圆形图片的处理,往往需要借助于第三方的实现,见附录文章1,2.And ...

  7. Android自己定义圆角ImageView 支持网络图片

    先看下效果图 我们再来看一张CSDN的圆角图片 从布局能够看出csdn app 的头像也是圆角的Image,但能够看到.有明显的毛刺感.不知道是csdn 程序猿的疏忽还是 我手机的问题,本人手机(小米 ...

  8. Android开发之制作圆形头像自定义View,直接引用工具类,加快开发速度。带有源代码学习

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...

  9. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  10. Android Bitmap 和 ByteArray的互相转换

    Android Bitmap 和 ByteArray的互相转换 移动平台图像处理,需要将图像传给native处理,如何传递?将bitmap转换成一个 byte[] 方便传递也方便cpp代码直接处理图像 ...

随机推荐

  1. mybatis的判定时间字段问题 java.lang.IllegalArgumentException: invalid comparison: cn.hutool.core.date.DateTime and java.lang.String

    今天听组员说: mybatis在3.30版本及以上判定时间时 <if test="date_time != null and date_time != '' "> da ...

  2. Cogs 727. [网络流24题] 太空飞行计划(最大权闭合子图)

    [网络流24题] 太空飞行计划 ★★☆ 输入文件:shuttle.in 输出文件:shuttle.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] W 教授正在为国家航天中心计 ...

  3. 退役II次后做题记录

    退役II次后做题记录 感觉没啥好更的,咕. atcoder1219 历史研究 回滚莫队. [六省联考2017]组合数问题 我是傻逼 按照组合意义等价于\(nk\)个物品,选的物品\(\mod k\) ...

  4. 机器学习---三种线性算法的比较(线性回归,感知机,逻辑回归)(Machine Learning Linear Regression Perceptron Logistic Regression Comparison)

    最小二乘线性回归,感知机,逻辑回归的比较:   最小二乘线性回归 Least Squares Linear Regression 感知机 Perceptron 二分类逻辑回归 Binary Logis ...

  5. ThinkPad T410i 2516A21 升級手札(換SSD固態硬碟、I7 CPU、開機20秒)

    最近筆記本越來越慢,開機得20分鐘,而且CPU動不動就飆到80度,趁著開學網上活動,準備給老伙計來一次重大升級.查一下主板芯片,最高支持8G內存,已經滿了,光驅位加了一個1T機械硬盤,那麼能升級的就只 ...

  6. MongoDB 表(集合) 创建删除、数据增删改查

    MongoDB 表(集合) 创建删除和增删改查数据 创建一个集合(emp) 在创建集合之前先使用use xxx,选择数据库,如果没有会创建(并不是真正的创建,只有在数据库里面保存集合数据之后才能够真正 ...

  7. NFV实验平台

    NFV架构如下图所示. NFVI对应于数据平面,数据平面转发数据并提供用于运行网络服务的资源. MANO对应于控制平面,该控制平面负责构建各种VNF之间的连接以及编排NFVI中的资源. VNF层对应于 ...

  8. 关于jvm系统属性-Djava.awt.headless 模式

    1. 什么是 java.awt.headless? Headless模式是系统的一种配置模式.在系统可能缺少显示设备.键盘或鼠标这些外设的情况下可以使用该模式. 2. 何时使用和headless mo ...

  9. 什么是CN2线路

      CN2全称为中国电信下一代承载网,英文Chinatelecom Next Carrier Network,缩写为CNCN,进一步缩写为CN2. CN2线路的优势在哪里 CN2作为“精品网络项目”被 ...

  10. nginx反向代理结合apache和php的配置示例

    .前端nginx主配置文件 # cat nginx.conf worker_processes ; #pid logs/nginx.pid; pid /data/www/logs/nginx.pid; ...