One, custom control 1
First, create an abstract class Maskedimage that inherits ImageView. Let him rewrite the OnDraw method. The code is as follows
ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PorterDuff;ImportAndroid.graphics.PorterDuffXfermode;ImportAndroid.graphics.Xfermode;Importandroid.graphics.drawable.Drawable;ImportAndroid.util.AttributeSet;ImportAndroid.widget.ImageView; Public Abstract classMaskedimageextendsImageView {Private Static FinalXfermode Mask_xfermode; PrivateBitmap Mask; PrivatePaint paint; Static{Porterduff.mode Localmode=PorterDuff.Mode.DST_IN; Mask_xfermode=NewPorterduffxfermode (Localmode); } Publicmaskedimage (Context paramcontext) {Super(Paramcontext); } Publicmaskedimage (Context paramcontext, AttributeSet paramattributeset) {Super(Paramcontext, Paramattributeset); } PublicMaskedimage (Context paramcontext, AttributeSet Paramattributeset,intparamint) { Super(Paramcontext, Paramattributeset, Paramint); } Public AbstractBitmap createmask (); protected voidOnDraw (Canvas paramcanvas) {drawable localdrawable=getdrawable (); if(Localdrawable = =NULL) return; Try { if( This. Paint = =NULL) {Paint localPaint1=NewPaint (); This. Paint =LocalPaint1; This. Paint.setfilterbitmap (false); Paint LocalPaint2= This. Paint; Xfermode LocalXfermode1=Mask_xfermode; @SuppressWarnings ("Unused") Xfermode LocalXfermode2=Localpaint2.setxfermode (LOCALXFERMODE1); } floatF1 =getwidth (); floatF2 =getheight (); inti = Paramcanvas.savelayer (0.0F, 0.0F, F1, F2,NULL, 31); intj =getwidth (); intK =getheight (); Localdrawable.setbounds (0, 0, J, K); Localdrawable.draw (Paramcanvas); if(( This. Mask = =NULL) || ( This. mask.isrecycled ())) {Bitmap LocalBitmap1=Createmask (); This. Mask =LocalBitmap1; } Bitmap localBitmap2= This. Mask; Paint LocalPaint3= This. Paint; Paramcanvas.drawbitmap (LOCALBITMAP2,0.0F, 0.0F, LocalPaint3); Paramcanvas.restoretocount (i); return; } Catch(Exception localexception) {StringBuilder Localstringbuilder=NewStringBuilder (). Append ("Attempting to draw with recycled bitmap. View ID = "); System.out.println ("localstringbuilder==" +Localstringbuilder); } }}
Then create a new class circularimage inherit maskedimage. The code is as follows:
ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.graphics.RectF;ImportAndroid.util.AttributeSet; Public classCircularimageextendsMaskedimage { Publiccircularimage (Context paramcontext) {Super(Paramcontext); } Publiccircularimage (Context paramcontext, AttributeSet paramattributeset) {Super(Paramcontext, Paramattributeset); } PublicCircularimage (Context paramcontext, AttributeSet Paramattributeset,intparamint) { Super(Paramcontext, Paramattributeset, Paramint); } PublicBitmap Createmask () {inti =getwidth (); intj =getheight (); Bitmap.config Localconfig=Bitmap.Config.ARGB_8888; Bitmap Localbitmap=Bitmap.createbitmap (i, J, Localconfig); Canvas Localcanvas=NewCanvas (LOCALBITMAP); Paint Localpaint=NewPaint (1); Localpaint.setcolor (-16777216); floatF1 =getwidth (); floatF2 =getheight (); RECTF LOCALRECTF=NewRECTF (0.0F, 0.0F, F1, F2); Localcanvas.drawoval (LOCALRECTF, localpaint); returnLocalbitmap; }}
Two, custom control 2
Create a class Roundimageview Inheritance ImageView
ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Bitmap.Config;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;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 Circular imageview * *@authorAdministrator*/ Public classRoundimageviewextendsImageView { PublicRoundimageview (Context context) {Super(context); //TODO auto-generated Constructor stub } PublicRoundimageview (Context context, AttributeSet attrs) {Super(context, attrs); } PublicRoundimageview (context context, AttributeSet attrs,intDefstyle) { Super(context, attrs, Defstyle); } @Overrideprotected voidOnDraw (canvas canvas) {drawable drawable=getdrawable (); if(drawable = =NULL) { return; } if(getwidth () = = 0 | | getheight () = = 0) { return; } Bitmap b=NULL; if(drawableinstanceofbitmapdrawable) {b=((bitmapdrawable) drawable). Getbitmap (); } if(NULL==b) {return; } Bitmap Bitmap= B.copy (Bitmap.Config.ARGB_8888,true); intW = getwidth (), h =getheight (); Bitmap Roundbitmap=Getcroppedbitmap (Bitmap, W); Canvas.drawbitmap (Roundbitmap,0, 0,NULL); } Public StaticBitmap Getcroppedbitmap (Bitmap bmp,intradius) {Bitmap sbmp; if(Bmp.getwidth ()! = Radius | | bmp.getheight ()! =radius) sbmp= Bitmap.createscaledbitmap (bmp, radius, radius,false); Elsesbmp=bmp; Bitmap Output=Bitmap.createbitmap (Sbmp.getwidth (), Sbmp.getheight (), config.argb_8888); Canvas Canvas=NewCanvas (output); Final intcolor = 0xffa19774; FinalPaint paint =NewPaint (); FinalRect rect =NewRect (0, 0, Sbmp.getwidth (), Sbmp.getheight ()); Paint.setantialias (true); Paint.setfilterbitmap (true); Paint.setdither (true); Canvas.drawargb (0, 0, 0, 0); Paint.setcolor (Color.parsecolor ("#BAB399")); Canvas.drawcircle (Sbmp.getwidth ()/2 + 0.7f, Sbmp.getheight ()/2 + 0.7f, Sbmp.getwidth ()/2 + 0.1f, paint); Paint.setxfermode (NewPorterduffxfermode (mode.src_in)); Canvas.drawbitmap (sbmp, rect, rect, paint); returnoutput; }}
Android custom control's round head