Android ImageView can only display the picture of the rectangle, so that does not meet our other needs, such as to show the picture of the rounded rectangle, this time, we need to customize the ImageView, the principle is to first get to the image of the bitmap, It then trims the bitmap of the corresponding rounded rectangle and then draws the rounded rectangle picture output in OnDraw ().
As follows:
The implementation code for the custom circular ImageView class is as follows:
PackageCom.xc.xcskin.view;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Bitmap.Config;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.graphics.RectF;ImportAndroid.graphics.PorterDuff.Mode;ImportAndroid.graphics.PorterDuffXfermode;ImportAndroid.graphics.Rect;Importandroid.graphics.drawable.BitmapDrawable;Importandroid.graphics.drawable.Drawable;ImportAndroid.util.AttributeSet;ImportAndroid.widget.ImageView;/*** Custom Rounded rectangle ImageView that can be used directly when the component is in the layout. * @authorcaizhiming **/ Public classXcroundrectimageviewextendsimageview{PrivatePaint paint; PublicXcroundrectimageview (Context context) { This(Context,NULL); } PublicXcroundrectimageview (Context context, AttributeSet attrs) { This(Context, attrs,0); } PublicXcroundrectimageview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); Paint=NewPaint (); } /*** Draw Rounded Rectangle picture *@authorcaizhiming*/@Overrideprotected voidOnDraw (canvas canvas) {drawable drawable=getdrawable (); if(NULL!=drawable) {Bitmap Bitmap=((bitmapdrawable) drawable). Getbitmap (); Bitmap b= Getroundbitmap (Bitmap, 20); FinalRect RECTSRC =NewRect (0, 0, B.getwidth (), B.getheight ()); FinalRect rectdest =NewRect (0,0, GetWidth (), getheight ()); Paint.reset (); Canvas.drawbitmap (b, Rectsrc, rectdest, paint); } Else { Super. OnDraw (canvas); } } /*** Get Rounded Rectangle picture method *@paramBitmap *@paramROUNDPX, generally set to *@returnBitmap *@authorcaizhiming*/ PrivateBitmap Getroundbitmap (Bitmap Bitmap,introundpx) {Bitmap Output=Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); Canvas Canvas=NewCanvas (output); Final intcolor = 0xff424242; FinalRect rect =NewRect (0, 0, Bitmap.getwidth (), Bitmap.getheight ()); FinalRECTF RECTF =NewRECTF (rect); Paint.setantialias (true); Canvas.drawargb (0, 0, 0, 0); Paint.setcolor (color); intx =bitmap.getwidth (); Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); Paint.setxfermode (NewPorterduffxfermode (mode.src_in)); Canvas.drawbitmap (Bitmap, rect, rect, paint); returnoutput; } }
Once you have completed this custom class, you can use this class as a component in the layout, such as:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" Match_parent " android:layout_height = "Match_parent" > <com.xc.xcskin.view.XCRoundRectImageView android:id= "@+id/ Roundrectimageview " android:layout_centerinparent=" true " android:layout_width=" 200DP " android:layout_height=" 200DP " android:src=" @drawable/roundimageview " /> </RelativeLayout>