Android 自定义的圆角矩形ImageView 工具类

上图看效果

自定义圆角矩形ImageView工具类

 1 package com.wechaotou.utils;
 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.Paint;
 8 import android.graphics.PorterDuff.Mode;
 9 import android.graphics.PorterDuffXfermode;
10 import android.graphics.Rect;
11 import android.graphics.RectF;
12 import android.graphics.drawable.BitmapDrawable;
13 import android.graphics.drawable.Drawable;
14 import android.util.AttributeSet;
15 import android.widget.ImageView;
16 
17 /**
18  * 自定义的圆角矩形ImageView,可以直接当组件在布局中使用。
19  * @author caizhiming
20  *
21  */
22 public class XCRoundRectImageView extends ImageView {
23 
24     private Paint paint;
25 
26     public XCRoundRectImageView(Context context) {
27         this(context,null);
28     }
29 
30     public XCRoundRectImageView(Context context, AttributeSet attrs) {
31         this(context, attrs,0);
32     }
33 
34     public XCRoundRectImageView(Context context, AttributeSet attrs, int defStyle) {
35         super(context, attrs, defStyle);
36         paint  = new Paint();
37     }
38 
39     /**
40      * 绘制圆角矩形图片
41      * @author caizhiming
42      */
43     @Override
44     protected void onDraw(Canvas canvas) {
45 
46         Drawable drawable = getDrawable();
47         if (null != drawable) {
48             Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
49             Bitmap b = getRoundBitmap(bitmap, 20);
50             final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
51             final Rect rectDest = new Rect(0,0,getWidth(),getHeight());
52             paint.reset();
53            canvas.drawBitmap(b, rectSrc, rectDest, paint);
54 
55         } else {
56             super.onDraw(canvas);
57         }
58     }
59 
60     /**
61      * 获取圆角矩形图片方法
62      * @param bitmap
63      * @param roundPx,一般设置成14
64      * @return Bitmap
65      * @author caizhiming
66      */
67     private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) {
68         Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
69                 bitmap.getHeight(), Config.ARGB_8888);
70         Canvas canvas = new Canvas(output);
71 
72         final int color = 0xff424242;
73 
74         final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
75         final RectF rectF = new RectF(rect);
76         paint.setAntiAlias(true);
77         canvas.drawARGB(0, 0, 0, 0);
78         paint.setColor(color);
79         int x = bitmap.getWidth();
80 
81         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
82         paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
83         canvas.drawBitmap(bitmap, rect, rect, paint);
84         return output;
85 
86 
87     }
88 }

 布局文件使用

        <com.wechaotou.utils.XCRoundRectImageView
            android:id="@+id/image_head"
            android:layout_width="77dp"
            android:layout_height="77dp"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="68dp"
            android:gravity="center_vertical"
            android:src="@drawable/text_order" />

 

posted @ 2019-01-16 22:15  monkey0928  阅读(967)  评论(0编辑  收藏  举报