在做安卓项目的过程中,我们总会遇到需要以圆角矩形控件来显示图标、图片或者按钮的需求,解决办法有两种,一种是在drawable下创建shape布局xml文件,另一种是自定义一个继承于ImageView的自定义控件类来实现,下面是具体的实现办法。
首先我们命名一个XCRoundRectImageView类,并继承于ImageView。代码如下:
1 import android.content.Context; 2 import android.graphics.Bitmap; 3 import android.graphics.Bitmap.Config; 4 import android.graphics.Canvas; 5 import android.graphics.Paint; 6 import android.graphics.PixelFormat; 7 import android.graphics.RectF; 8 import android.graphics.PorterDuff.Mode; 9 import android.graphics.PorterDuffXfermode; 10 import android.graphics.Rect; 11 import android.graphics.drawable.BitmapDrawable; 12 import android.graphics.drawable.Drawable; 13 import android.util.AttributeSet; 14 import android.widget.ImageView; 15 16 17 18 /** 19 * Created by Yang on 2016-03-18. 20 * 自定义的圆角矩形ImageView,可以直接当组件在布局中使用。 21 */ 22 public class XCRoundRectImageView extends ImageView { 23 private Paint paint; 24 25 public XCRoundRectImageView(Context context) { 26 this(context, null); 27 } 28 29 public XCRoundRectImageView(Context context, AttributeSet attrs) { 30 this(context, attrs, 0); 31 } 32 33 public XCRoundRectImageView(Context context, AttributeSet attrs, int defStyle) { 34 super(context, attrs, defStyle); 35 paint = new Paint(); 36 } 37 38 /** 39 * 绘制圆角矩形图片 40 */ 41 @Override 42 protected void onDraw(Canvas canvas) { 43 Drawable drawable = getDrawable(); 44 if (null != drawable) { 45 Bitmap bitmap = null; 46 if (drawable instanceof BitmapDrawable) { 47 bitmap = ((BitmapDrawable) drawable).getBitmap(); 48 } else if (drawable instanceof AsyncDrawable) { 49 bitmap = Bitmap.createBitmap(getWidth(), 50 getHeight(), 51 drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); 52 Canvas canvas1 = new Canvas(bitmap); 53 drawable.setBounds(0, 0, getWidth(), getHeight()); 54 drawable.draw(canvas1); 55 } 56 //10为设置的圆角矩形圆角角度大小,可修改 57 Bitmap b = getRoundBitmap(bitmap, 10); 58 final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight()); 59 final Rect rectDest = new Rect(0, 0, getWidth(), getHeight()); 60 paint.reset(); 61 canvas.drawBitmap(b, rectSrc, rectDest, paint); 62 } else { 63 super.onDraw(canvas); 64 } 65 } 66 67 /** 68 * 获取圆角矩形图片方法 69 * @param bitmap 70 * @return Bitmap 71 */ 72 private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) { 73 Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 74 Canvas canvas = new Canvas(output); 75 final int color = 0xff424242; 76 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 77 final RectF rectF = new RectF(rect); 78 paint.setAntiAlias(true); 79 canvas.drawARGB(0, 0, 0, 0); 80 paint.setColor(color); 81 int x = bitmap.getWidth(); 82 canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 83 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 84 canvas.drawBitmap(bitmap, rect, rect, paint); 85 return output; 86 } 87 }
只需要在需要圆角矩形的地方引用自定义控件即可,代码如下
<<!--你的包名-->.view.XCRoundRectImageView android:id="@+id/xCRoundRectImageView" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
时间: 2024-10-13 01:32:13