【Android】图片切角,切指定的边。

来源:互联网 发布:ae mac中文版下载 编辑:程序博客网 时间:2024/04/28 14:41

【Android】图片切角,切指定的边。

    博客分类: 
  • Android
  • Java
android

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

 

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

直接可用的代码: 

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