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(bitma |