1 package com.example.view; 2 import android.content.Context; 3 import android.graphics.Bitmap; 4 import android.graphics.Bitmap.Config; 5 import android.graphics.Canvas; 6 import android.graphics.Paint; 7 import android.graphics.PorterDuff.Mode; 8 import android.graphics.PorterDuffXfermode; 9 import android.graphics.Rect; 10 import android.graphics.drawable.BitmapDrawable; 11 import android.graphics.drawable.Drawable; 12 import android.util.AttributeSet; 13 import android.widget.ImageView; 14 15 /** 16 * 圆形ImageView,可设置最多两个宽度不同且颜色不同的圆形边框 17 * 设置颜色在xml布局文件中由自定义属性配置参数指定 18 */ 19 public class MyView extends ImageView { 20 private Context mContext; 21 // 控件默认长、宽 22 private int defaultWidth = 0; 23 private int defaultHeight = 0; 24 public MyView(Context context) { 25 super(context); 26 mContext = context; 27 } 28 29 public MyView(Context context, AttributeSet attrs) { 30 super(context, attrs); 31 mContext = context; 32 } 33 34 public MyView(Context context, AttributeSet attrs, int defStyle) { 35 super(context, attrs, defStyle); 36 mContext = context; 37 } 38 39 @Override 40 protected void onDraw(Canvas canvas) { 41 Drawable drawable = getDrawable() ; 42 Bitmap b = ((BitmapDrawable) drawable).getBitmap(); 43 Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true); 44 defaultWidth = getWidth(); 45 defaultHeight = getHeight(); 46 //如果defaultWidth 小于defaultHeight 成立,就返回defaultWidth ,否则就返回defaultHeight。 47 int radius = (defaultWidth < defaultHeight ? defaultWidth : defaultHeight) / 2; 48 Bitmap roundBitmap = getCroppedRoundBitmap(bitmap, radius); 49 canvas.drawBitmap(roundBitmap, defaultWidth / 2 - radius, defaultHeight / 2 - radius, null); 50 } 51 52 /** 53 * 获取裁剪后的圆形图片 54 * @param radius半径 55 */ 56 public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) { 57 Bitmap scaledSrcBmp; 58 int diameter = radius * 2; 59 // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片 60 int squareWidth = 0, squareHeight = 0; 61 int x = 0, y = 0; 62 Bitmap squareBitmap; 63 if (bmp.getHeight() > bmp.getWidth()) { 64 // 高大于宽的时候,正方形的宽、高==图片的宽 65 squareWidth = squareHeight = bmp.getWidth(); 66 x = 0; 67 y = (bmp.getHeight() - bmp.getWidth()) / 2; 68 // 截取正方形图片 69 squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight); 70 } else if (bmp.getHeight() < bmp.getWidth()) { 71 // 宽大于高的时候,正方形的宽、高==图片的高 72 squareWidth = squareHeight = bmp.getHeight(); 73 x = (bmp.getWidth() - bmp.getHeight()) / 2; 74 y = 0; 75 // 截取正方形图片 76 squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,squareHeight); 77 } else { 78 //宽等于高的时候,就直接取本图片 79 squareBitmap = bmp; 80 } 81 if (squareBitmap.getWidth() != diameter || squareBitmap.getHeight() != diameter) { 82 //如果正方形的宽、高不等于圆的直径,就将正方形图片进行缩放到和圆的直径相等的正方形 83 scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter,diameter, true); 84 } else { 85 scaledSrcBmp = squareBitmap; 86 } 87 //创建一个位图,等于正方形的宽、高 88 Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(),scaledSrcBmp.getHeight(),Config.ARGB_8888); 89 //创建一个画布 90 Canvas canvas = new Canvas(output); 91 //创建一个画笔 92 Paint paint = new Paint(); 93 //创建一个正方形的矩阵 94 Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(),scaledSrcBmp.getHeight()); 95 //设置画笔的锯齿效果 96 paint.setAntiAlias(true); 97 //绘制一个圆, 98 canvas.drawCircle(scaledSrcBmp.getWidth() / 2,scaledSrcBmp.getHeight() / 2,scaledSrcBmp.getWidth() / 2,paint); 99 //取绘制交集显示上层 100 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 101 //画一个位图, 102 canvas.drawBitmap(scaledSrcBmp, rect, rect, paint); 103 bmp = null; 104 squareBitmap = null; 105 scaledSrcBmp = null; 106 return output; 107 } 108 }