公司的项目,UI和应用都是我自己做的。前几天设计了一个UI,出现了半边圆角的情况,如下图片所示。图片都来自服务器,肯定不能要求返回的图片按这个格式,必须在应用端对图片进行切角。

Google了好久,发现能找到的代码都是重复的,而且代码垃圾很多。于是按着那段代码的解决方式,自己写了一个实现指定切某一边的工具类。

直接可用的代码:

  1. package com.lurencun.androidsysteminfomation;
  2. import android.graphics.Bitmap;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.PorterDuffXfermode;
  7. import android.graphics.Rect;
  8. import android.graphics.RectF;
  9. import android.graphics.Bitmap.Config;
  10. import android.graphics.PorterDuff.Mode;
  11. /**
  12. * @author : 桥下一粒砂
  13. * @email  : chenyoca@gmail.com
  14. * @date   : 2012-11-8
  15. * @desc   :
  16. */
  17. public class BitmapFillet {
  18. public static final int ALL = 347120;
  19. public static final int TOP = 547120;
  20. public static final int LEFT = 647120;
  21. public static final int RIGHT = 747120;
  22. public static final int BOTTOM = 847120;
  23. /**
  24. *
  25. * 指定图片的切边,对图片进行圆角处理
  26. * @param type 具体参见:{@link BitmapFillet.ALL} , {@link BitmapFillet.TOP} ,
  27. *              {@link BitmapFillet.LEFT} , {@link BitmapFillet.RIGHT} , {@link BitmapFillet.BOTTOM}
  28. * @param bitmap 需要被切圆角的图片
  29. * @param roundPx 要切的像素大小
  30. * @return
  31. *
  32. */
  33. public static Bitmap fillet(int type,Bitmap bitmap,int roundPx) {
  34. try {
  35. // 其原理就是:先建立一个与图片大小相同的透明的Bitmap画板
  36. // 然后在画板上画出一个想要的形状的区域。
  37. // 最后把源图片帖上。
  38. final int width = bitmap.getWidth();
  39. final int height = bitmap.getHeight();
  40. Bitmap paintingBoard = Bitmap.createBitmap(width,height, Config.ARGB_8888);
  41. Canvas canvas = new Canvas(paintingBoard);
  42. canvas.drawARGB(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT);
  43. final Paint paint = new Paint();
  44. paint.setAntiAlias(true);
  45. paint.setColor(Color.BLACK);
  46. if( TOP == type ){
  47. clipTop(canvas,paint,roundPx,width,height);
  48. }else if( LEFT == type ){
  49. clipLeft(canvas,paint,roundPx,width,height);
  50. }else if( RIGHT == type ){
  51. clipRight(canvas,paint,roundPx,width,height);
  52. }else if( BOTTOM == type ){
  53. clipBottom(canvas,paint,roundPx,width,height);
  54. }else{
  55. clipAll(canvas,paint,roundPx,width,height);
  56. }
  57. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  58. //帖子图
  59. final Rect src = new Rect(0, 0, width, height);
  60. final Rect dst = src;
  61. canvas.drawBitmap(bitmap, src, dst, paint);
  62. return paintingBoard;
  63. } catch (Exception exp) {
  64. return bitmap;
  65. }
  66. }
  67. private static void clipLeft(final Canvas canvas,final Paint paint,int offset,int width,int height){
  68. final Rect block = new Rect(offset,0,width,height);
  69. canvas.drawRect(block, paint);
  70. final RectF rectF = new RectF(0, 0, offset * 2 , height);
  71. canvas.drawRoundRect(rectF, offset, offset, paint);
  72. }
  73. private static void clipRight(final Canvas canvas,final Paint paint,int offset,int width,int height){
  74. final Rect block = new Rect(0, 0, width-offset, height);
  75. canvas.drawRect(block, paint);
  76. final RectF rectF = new RectF(width - offset * 2, 0, width , height);
  77. canvas.drawRoundRect(rectF, offset, offset, paint);
  78. }
  79. private static void clipTop(final Canvas canvas,final Paint paint,int offset,int width,int height){
  80. final Rect block = new Rect(0, offset, width, height);
  81. canvas.drawRect(block, paint);
  82. final RectF rectF = new RectF(0, 0, width , offset * 2);
  83. canvas.drawRoundRect(rectF, offset, offset, paint);
  84. }
  85. private static void clipBottom(final Canvas canvas,final Paint paint,int offset,int width,int height){
  86. final Rect block = new Rect(0, 0, width, height - offset);
  87. canvas.drawRect(block, paint);
  88. final RectF rectF = new RectF(0, height - offset * 2 , width , height);
  89. canvas.drawRoundRect(rectF, offset, offset, paint);
  90. }
  91. private static void clipAll(final Canvas canvas,final Paint paint,int offset,int width,int height){
  92. final RectF rectF = new RectF(0, 0, width , height);
  93. canvas.drawRoundRect(rectF, offset, offset, paint);
  94. }
  95. }

【Android】图片切角,切指定的边。的更多相关文章

  1. Android屏幕适配与切图_汇总

    首先和最后,还是先看好官方文档:http://developer.android.com/guide/practices/screens_support.html 对应的翻译blog有牛人做了:And ...

  2. Css-深入学习之弧形切角矩形

    本文是作者从别的网站和文章学习了解的知识,简单做了个笔记,想要学习更多的可以参考这里:[css进阶]伪元素的妙用--单标签之美,奇思妙想 (弧形切角矩形) 代码: width: 180px; heig ...

  3. Css--深入学习之切角

    本文是作者从别的网站和文章学习了解的知识,简单做了个笔记,想要学习更多的可以参考这里:[css进阶]伪元素的妙用--单标签之美,奇思妙想 带切角的矩形: 该图来源于(奇思妙想) Css代码: .not ...

  4. 解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移

    解决iOS中 tabBarItem设置图片(image+title切图在一起)时造成的图片向上偏移 解决办法1:设置tabBarItem的imageInsets属性 代码示例: childContro ...

  5. css实现切角效果

    1. 一个切角 思路:如果我们要得到有一个切角的元素,我们只需要使用一个径向渐变就可以达到这个目标,这个渐变需要把一个透明色标放在切角处,然后再相同的位置设置另一个色标,并且把它的颜色设置成我们想要的 ...

  6. CSS奇思妙想图形(心形、气泡三角形、切角、梯形、饼图等)

    今天看到一篇不错文章,在原来CSS3图形创建基础上扩展了很多. 这里记录总结下 心形 原理:利用 圆形 和 正方形实现 HTML: <div class="heartShaped&qu ...

  7. DDGScreenShot--iOS 图片裁剪,切圆角,加边框,你还用cornerRadius,还有更高级的用法

    写在前面 我们肯定做过这样的需求,给一个图片切圆角, 当然我们大多采用简单粗暴的方法 myIcon.layer.cornerRadius = 16.5 myIcon.layer.masksToBoun ...

  8. css 折角效果/切角效果

    首先我们先创建一个图案为100像素的斜面切角的图案 html <div class="one">12345</div> css .one{ width: 1 ...

  9. Android图片加载框架最全解析(八),带你全面了解Glide 4的用法

    本篇将是我们这个Glide系列的最后一篇文章. 其实在写这个系列第一篇文章的时候,Glide就推出4.0.0的RC版了.那个时候因为我一直研究的都是Glide 3.7.0版本,再加上RC版本还不太稳定 ...

随机推荐

  1. bash和shell的关系

    bash是borne again shell的缩写,它是shell的一种,Linux上默认采用的是bash. shell脚本中的方法带不带function的区别,例如: function foo () ...

  2. Getting in Line UVA 216

     Getting in Line  Computer networking requires that the computers in the network be linked. This pro ...

  3. Converter Standardalone 5 versus Converter Boot CD

    The new Converter Standalone 5 lacks the Converter Boot CD. Fortunately you can still use the old ve ...

  4. linux发送邮件的功能总结

    今天添加了发送邮件的功能,总结一下,供以后参考: 1.直接使用管道发送邮件 echo "hello,this is the content of mail.welcome to www.mz ...

  5. apache跨域

    http://www.cnblogs.com/2050/p/3191744.html http://blog.csdn.net/qq_15283821/article/details/54405805 ...

  6. 禁用系统的Ctrl+Alt+Left/Right(方向键)

    对于非常多工具,如IntelliJ IDE,Ctrl+Alt+Left/Right(方向键)是一个非常重要的快捷键,可是这个快捷键经常会被一些显示相关的附属应用给占用用于调整屏幕显示的朝向,有时候即使 ...

  7. python解析命令行参数

    常常需要解析命令行参数,经常忘记,好烦,总结下来吧. 1.Python 中也可以所用 sys 的 sys.argv 来获取命令行参数: sys.argv 是命令行参数列表 参数个数:len(sys.a ...

  8. python获取自己的环境变量

    1. import sys sys.path 2. from distutils.sysconfig import get_python_lib get_python_lib() 3. import ...

  9. 一、Django用Eclipse编写一个登录界面

    一.Django用Eclipse编写一个登录界面 二.Django用Eclipse编写一个登录界面Ajax和Django交互 Eclipse安装Python插件和Django的步骤直接省略. 创建de ...

  10. Oracle Data Integrator 12c----一致性 CDC(Consistent CDC)

    一致性 CDC 中引入了变化集的概念.一个变化集中可以包括多个相互存在关联关系(如主外键引用关系)的表.CDC 在捕获和发布一个变化集中的变化时能够保证数据的一致性.这个练习介绍如何使用能够保证一致性 ...