android中对Bitmap图片设置任意角为圆角

http://blog.csdn.net/l448288137/article/details/48276681

最近项目开发中使用到了圆角图片,网上找到的圆角图片控件大多比较死板,只可以全圆角。其中感觉最好的也就是半圆角 链接在这里。想了一下,我自己在这个的基础上进行了一点改进,使得图片可以设置任意角为圆角。

先上效果图:

 

核心代码

 

[java] view plain copy
 
  1. package fillet.sgn.com.filletimage;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.graphics.PorterDuff;  
  8. import android.graphics.PorterDuffXfermode;  
  9. import android.graphics.Rect;  
  10. import android.graphics.RectF;  
  11.   
  12. /** 
  13.  * Created by liujinhua on 15/9/7. 
  14.  */  
  15. public class BitmapFillet {  
  16.   
  17.     public static final int CORNER_NONE = 0;  
  18.     public static final int CORNER_TOP_LEFT = 1;  
  19.     public static final int CORNER_TOP_RIGHT = 1 << 1;  
  20.     public static final int CORNER_BOTTOM_LEFT = 1 << 2;  
  21.     public static final int CORNER_BOTTOM_RIGHT = 1 << 3;  
  22.     public static final int CORNER_ALL = CORNER_TOP_LEFT | CORNER_TOP_RIGHT | CORNER_BOTTOM_LEFT | CORNER_BOTTOM_RIGHT;  
  23.     public static final int CORNER_TOP = CORNER_TOP_LEFT | CORNER_TOP_RIGHT;  
  24.     public static final int CORNER_BOTTOM = CORNER_BOTTOM_LEFT | CORNER_BOTTOM_RIGHT;  
  25.     public static final int CORNER_LEFT = CORNER_TOP_LEFT | CORNER_BOTTOM_LEFT;  
  26.     public static final int CORNER_RIGHT = CORNER_TOP_RIGHT | CORNER_BOTTOM_RIGHT;  
  27.   
  28.   
  29.   
  30.     public static Bitmap fillet(Bitmap bitmap, int roundPx,int corners) {  
  31.         try {  
  32.             // 其原理就是:先建立一个与图片大小相同的透明的Bitmap画板  
  33.             // 然后在画板上画出一个想要的形状的区域。  
  34.             // 最后把源图片帖上。  
  35.             final int width = bitmap.getWidth();  
  36.             final int height = bitmap.getHeight();  
  37.   
  38.             Bitmap paintingBoard = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);  
  39.             Canvas canvas = new Canvas(paintingBoard);  
  40.             canvas.drawARGB(Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT, Color.TRANSPARENT);  
  41.   
  42.             final Paint paint = new Paint();  
  43.             paint.setAntiAlias(true);  
  44.             paint.setColor(Color.BLACK);  
  45.   
  46.             //画出4个圆角  
  47.             final RectF rectF = new RectF(0, 0, width, height);  
  48.             canvas.drawRoundRect(rectF, roundPx, roundPx, paint);  
  49.   
  50.             //把不需要的圆角去掉  
  51.             int notRoundedCorners = corners ^ CORNER_ALL;  
  52.             if ((notRoundedCorners & CORNER_TOP_LEFT) != 0) {  
  53.                 clipTopLeft(canvas,paint,roundPx,width,height);  
  54.             }  
  55.             if ((notRoundedCorners & CORNER_TOP_RIGHT) != 0) {  
  56.                 clipTopRight(canvas, paint, roundPx, width, height);  
  57.             }  
  58.             if ((notRoundedCorners & CORNER_BOTTOM_LEFT) != 0) {  
  59.                 clipBottomLeft(canvas,paint,roundPx,width,height);  
  60.             }  
  61.             if ((notRoundedCorners & CORNER_BOTTOM_RIGHT) != 0) {  
  62.                 clipBottomRight(canvas, paint, roundPx, width, height);  
  63.             }  
  64.             paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));  
  65.   
  66.             //帖子图  
  67.             final Rect src = new Rect(0, 0, width, height);  
  68.             final Rect dst = src;  
  69.             canvas.drawBitmap(bitmap, src, dst, paint);  
  70.             return paintingBoard;  
  71.         } catch (Exception exp) {  
  72.             return bitmap;  
  73.         }  
  74.     }  
  75.   
  76.     private static void clipTopLeft(final Canvas canvas, final Paint paint, int offset, int width, int height) {  
  77.         final Rect block = new Rect(0, 0, offset, offset);  
  78.         canvas.drawRect(block, paint);  
  79.     }  
  80.   
  81.     private static void clipTopRight(final Canvas canvas, final Paint paint, int offset, int width, int height) {  
  82.         final Rect block = new Rect(width - offset, 0, width, offset);  
  83.         canvas.drawRect(block, paint);  
  84.     }  
  85.   
  86.     private static void clipBottomLeft(final Canvas canvas, final Paint paint, int offset, int width, int height) {  
  87.         final Rect block = new Rect(0, height - offset, offset, height);  
  88.         canvas.drawRect(block, paint);  
  89.     }  
  90.   
  91.     private static void clipBottomRight(final Canvas canvas, final Paint paint, int offset, int width, int height) {  
  92.         final Rect block = new Rect(width - offset, height - offset, width, height);  
  93.         canvas.drawRect(block, paint);  
  94.     }  
  95. }  
posted @ 2016-10-28 15:04  wikiki  阅读(5721)  评论(0编辑  收藏  举报