Android 自定义圆形图片

本文转自 http://blog.csdn.net/qq_17250009/article/details/49030165

 

代码注释很多,简单说下思路,然后直接贴代码

1、截取选定图片中间区域(宽等于高的正方形)

2、按照控件大小进行缩放

3、画圆,设置paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

4、画图

 

  1. package com.dyk.thebest.view;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Bitmap.Config;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.PorterDuff.Mode;  
  10. import android.graphics.PorterDuffXfermode;  
  11. import android.graphics.Rect;  
  12. import android.graphics.drawable.BitmapDrawable;  
  13. import android.graphics.drawable.Drawable;  
  14. import android.util.AttributeSet;  
  15. import android.util.Log;  
  16. import android.widget.ImageView;  
  17.   
  18. /** 
  19.  * 自定义圆形图片 
  20.  * <p> 
  21.  * 在<u><font color="#0000ff">onMeasure()</font></u>中强制设置控件的宽高一致 <br/> 
  22.  * 在<u><font color="#0000ff">getCroppedBitmap()</font></u>中截取图片中间区域并返回一个bitmap对象 
  23.  *  
  24.  * @author 一口仨馍 
  25.  * 
  26.  */  
  27. public class RoundImageView extends ImageView {  
  28.   
  29.     public RoundImageView(Context context, AttributeSet attrs) {  
  30.         super(context, attrs);  
  31.     }  
  32.   
  33.     public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {  
  34.         super(context, attrs, defStyleAttr);  
  35.     }  
  36.   
  37.     @Override  
  38.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  39.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  40.         // 强制设置圆形图片控件的宽高一致  
  41.         int min = Math.min(getMeasuredWidth(), getMeasuredHeight());  
  42.         setMeasuredDimension(min, min);  
  43.     }  
  44.   
  45.     @Override  
  46.     protected void onDraw(Canvas canvas) {  
  47.         Drawable drawable = getDrawable();  
  48.   
  49.         if (drawable == null) {  
  50.             return;  
  51.         }  
  52.   
  53.         if (getWidth() == 0 || getHeight() == 0) {  
  54.             return;  
  55.         }  
  56.         // 注意:此条语句不能智能提示,只能手动输入  
  57.         Bitmap b = ((BitmapDrawable) drawable).getBitmap();  
  58.   
  59.         if (null == b) {  
  60.             return;  
  61.         }  
  62.   
  63.         Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);  
  64.   
  65.         // 因为后来在onMeasure中强制设置了控件宽高相等,所以相当于下面的w强制等于h  
  66.         // min == w == h == sbmp.getWidth() == sbmp.getHeight()是成立的  
  67.         int w = getWidth(), h = getHeight();  
  68.         int min = Math.min(w, h);  
  69.         Bitmap roundBitmap = getCroppedBitmap(bitmap, min);  
  70.         canvas.drawBitmap(roundBitmap, 0, 0, null);  
  71.   
  72.     }  
  73.   
  74.     /** 
  75.      * 截取bmp中间区域的图像,并缩放至与视图宽高大小一致。画图。 
  76.      *  
  77.      * @param bmp 
  78.      *            Bitmap对象 
  79.      * @param min 
  80.      *            视图宽高的最小值,单位:px 
  81.      * @return bitmap 
  82.      */  
  83.     public static Bitmap getCroppedBitmap(Bitmap bmp, int min) {  
  84.         // 截取后的bitmap  
  85.         Bitmap squareBitmap;  
  86.         int bmpWidth = bmp.getWidth();  
  87.         int bmpHeight = bmp.getHeight();  
  88.         int squareWidth = 0, squareHeight = 0;  
  89.         int x = 0, y = 0;  
  90.         if (bmpHeight > bmpWidth) {// 高大于宽  
  91.             squareWidth = squareHeight = bmpWidth;  
  92.             x = 0;  
  93.             y = (bmpHeight - bmpWidth) / 2;  
  94.             // 截取正方形图片  
  95.             squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,  
  96.                     squareHeight);  
  97.         } else if (bmpHeight < bmpWidth) {// 宽大于高  
  98.             squareWidth = squareHeight = bmpHeight;  
  99.             x = (bmpWidth - bmpHeight) / 2;  
  100.             y = 0;  
  101.             squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,  
  102.                     squareHeight);  
  103.         } else {  
  104.             squareBitmap = bmp;  
  105.         }  
  106.   
  107.         Bitmap sbmp;  
  108.         //对截取后的squareBitmap缩放至控件宽高  
  109.         if (squareBitmap.getWidth() != min || squareBitmap.getHeight() != min) {  
  110.             sbmp = Bitmap.createScaledBitmap(squareBitmap, min, min, false);  
  111.         } else {  
  112.             sbmp = squareBitmap;  
  113.         }  
  114.         Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),  
  115.                 Config.ARGB_8888);  
  116.         Canvas canvas = new Canvas(output);  
  117.   
  118.         final Paint paint = new Paint();  
  119.         //指定抠图/画图区域  
  120.         final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());  
  121.         paint.setAntiAlias(true);  
  122.         paint.setFilterBitmap(true);  
  123.         paint.setDither(true);  
  124.         canvas.drawARGB(0, 0, 0, 0);//将内容以外的区域设置为完全透明的黑色  
  125.         paint.setColor(Color.parseColor("#BAB399"));  
  126.         canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,  
  127.                 sbmp.getHeight() / 2 + 0.7f, min / 2 + 0.1f, paint);  
  128.         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  129.         canvas.drawBitmap(sbmp, rect, rect, paint);  
  130.   
  131.         return output;  
  132.     }  
  133. }  
posted @ 2016-08-16 16:44  书中翱翔  阅读(102)  评论(0)    收藏  举报