Android中快速自定義圓形ImageView圖形!
阿新 • • 發佈:2019-01-28
1 public class ImageViewPlus extends ImageView{ 2 /** 3 * android.widget.ImageView 4 */ 5 public static final int TYPE_NONE = 0; 6 /** 7 * 圓形 8 */ 9 public static final int TYPE_CIRCLE = 1; 10 /** 11 * 圓角矩形 12 */ 13 public static finalint TYPE_ROUNDED_RECT = 2; 14 15 private static final int DEFAULT_TYPE = TYPE_NONE; 16 private static final int DEFAULT_BORDER_COLOR = Color.TRANSPARENT; 17 private static final int DEFAULT_BORDER_WIDTH = 0; 18 private static final int DEFAULT_RECT_ROUND_RADIUS = 0; 19 20private int mType; 21 private int mBorderColor; 22 private int mBorderWidth; 23 private int mRectRoundRadius; 24 25 private Paint mPaintBitmap = new Paint(Paint.ANTI_ALIAS_FLAG); 26 private Paint mPaintBorder = new Paint(Paint.ANTI_ALIAS_FLAG); 27 28 privateRectF mRectBorder = new RectF(); 29 private RectF mRectBitmap = new RectF(); 30 31 private Bitmap mRawBitmap; 32 private BitmapShader mShader; 33 private Matrix mMatrix = new Matrix(); 34 35 public ImageViewPlus(Context context, AttributeSet attrs) { 36 super(context, attrs); 37 //取xml檔案中設定的引數 38 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ImageViewPlus); 39 mType = ta.getInt(R.styleable.ImageViewPlus_type, DEFAULT_TYPE); 40 mBorderColor = ta.getColor(R.styleable.ImageViewPlus_borderColor, DEFAULT_BORDER_COLOR); 41 mBorderWidth = ta.getDimensionPixelSize(R.styleable.ImageViewPlus_borderWidth, dip2px(DEFAULT_BORDER_WIDTH)); 42 mRectRoundRadius = ta.getDimensionPixelSize(R.styleable.ImageViewPlus_rectRoundRadius, dip2px(DEFAULT_RECT_ROUND_RADIUS)); 43 ta.recycle(); 44 } 45 46 @Override 47 protected void onDraw(Canvas canvas) { 48 Bitmap rawBitmap = getBitmap(getDrawable()); 49 50 if (rawBitmap != null && mType != TYPE_NONE){ 51 int viewWidth = getWidth(); 52 int viewHeight = getHeight(); 53 int viewMinSize = Math.min(viewWidth, viewHeight); 54 float dstWidth = mType == TYPE_CIRCLE ? viewMinSize : viewWidth; 55 float dstHeight = mType == TYPE_CIRCLE ? viewMinSize : viewHeight; 56 float halfBorderWidth = mBorderWidth / 2.0f; 57 float doubleBorderWidth = mBorderWidth * 2; 58 59 if (mShader == null || !rawBitmap.equals(mRawBitmap)){ 60 mRawBitmap = rawBitmap; 61 mShader = new BitmapShader(mRawBitmap, TileMode.CLAMP, TileMode.CLAMP); 62 } 63 if (mShader != null){ 64 mMatrix.setScale((dstWidth - doubleBorderWidth) / rawBitmap.getWidth(), (dstHeight - doubleBorderWidth) / rawBitmap.getHeight()); 65 mShader.setLocalMatrix(mMatrix); 66 } 67 68 mPaintBitmap.setShader(mShader); 69 mPaintBorder.setStyle(Paint.Style.STROKE); 70 mPaintBorder.setStrokeWidth(mBorderWidth); 71 mPaintBorder.setColor(mBorderWidth > 0 ? mBorderColor : Color.TRANSPARENT); 72 73 if (mType == TYPE_CIRCLE){ 74 float radius = viewMinSize / 2.0f; 75 canvas.drawCircle(radius, radius, radius - halfBorderWidth, mPaintBorder); 76 canvas.translate(mBorderWidth, mBorderWidth); 77 canvas.drawCircle(radius - mBorderWidth, radius - mBorderWidth, radius - mBorderWidth, mPaintBitmap); 78 } else if (mType == TYPE_ROUNDED_RECT){ 79 mRectBorder.set(halfBorderWidth, halfBorderWidth, dstWidth - halfBorderWidth, dstHeight - halfBorderWidth); 80 mRectBitmap.set(0.0f, 0.0f, dstWidth - doubleBorderWidth, dstHeight - doubleBorderWidth); 81 float borderRadius = mRectRoundRadius - halfBorderWidth > 0.0f ? mRectRoundRadius - halfBorderWidth : 0.0f; 82 float bitmapRadius = mRectRoundRadius - mBorderWidth > 0.0f ? mRectRoundRadius - mBorderWidth : 0.0f; 83 canvas.drawRoundRect(mRectBorder, borderRadius, borderRadius, mPaintBorder); 84 canvas.translate(mBorderWidth, mBorderWidth); 85 canvas.drawRoundRect(mRectBitmap, bitmapRadius, bitmapRadius, mPaintBitmap); 86 } 87 } else { 88 super.onDraw(canvas); 89 } 90 } 91 92 private int dip2px(int dipVal) 93 { 94 float scale = getResources().getDisplayMetrics().density; 95 return (int)(dipVal * scale + 0.5f); 96 } 97 98 private Bitmap getBitmap(Drawable drawable){ 99 if (drawable instanceof BitmapDrawable){ 100 return ((BitmapDrawable)drawable).getBitmap(); 101 } else if (drawable instanceof ColorDrawable){ 102 Rect rect = drawable.getBounds(); 103 int width = rect.right - rect.left; 104 int height = rect.bottom - rect.top; 105 int color = ((ColorDrawable)drawable).getColor(); 106 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 107 Canvas canvas = new Canvas(bitmap); 108 canvas.drawARGB(Color.alpha(color), Color.red(color), Color.green(color), Color.blue(color)); 109 return bitmap; 110 } else { 111 return null; 112 } 113 } 114 }