android中對Bitmap圖片設定任意角為圓角

來源:互聯網
上載者:User

標籤:try   number   color   class   get   drawrect   highlight   ber   data-   

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. }  

android中對Bitmap圖片設定任意角為圓角

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.