We often see some apps can display rounded pictures, such as the QQ contact icon and so on, to realize the fillet picture one way is to directly use the fillet picture resources, of course, if there is no fillet picture resources, we can also implement their own programs, the following describes a custom fillet ImageView method:
Package Com.yulongfei.imageview;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.bitmap;import Android.graphics.bitmap.config;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.path;import Android.graphics.porterduff;import Android.graphics.porterduffxfermode;import Android.graphics.RectF;import Android.util.attributeset;import Android.widget.imageview;public class Roundangleimageview extends ImageView { private int roundwidth = 13;private int roundheight = 13;public Roundangleimageview (context context) {super (context); init (context, null);} Public Roundangleimageview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); Init ( context, attrs);} Public Roundangleimageview (context context, AttributeSet Attrs) {Super (context, attrs); init (context, attrs);} private void init (context context, AttributeSet attrs) {if (attrs! = null) {TypedArray a = Context.obtainStyledattributes (attrs,r.styleable.roundangleimageview); roundwidth = A.getdimensionpixelsize ( R.styleable.roundangleimageview_roundwidth, roundwidth); roundheight = A.getdimensionpixelsize ( R.styleable.roundangleimageview_roundheight, Roundheight); A.recycle ();} else {float density = context.getresources (). Getdisplaymetrics (). Density;roundwidth = (int) (roundwidth * density); Roundheight = (int) (roundheight * density);}} /** Rewrite Draw () */@Overridepublic void draw (canvas canvas) {//Instantiate a bitmapbitmap of the same size as imageview bitmap = Bitmap.createbitmap (GetWidth (), GetHeight (), config.argb_8888);//Instantiate a canvas, the canvas corresponding to the memory of the above Bitmapcanvas CANVAS2 = new Canvas (bitmap), if (bitmap.isrecycled ()) {bitmap = Bitmap.createbitmap (GetWidth (), GetHeight (), config.argb_8888); canvas2 = new Canvas (bitmap);} Draw ImageView himself to the CANVAS2, which causes bitmap to store Imageviewsuper.draw (CANVAS2);//Use canvas to draw a rounded rectangle, This modifies the bitmap data drawroundangle (CANVAS2);//Draws the cropped bitmap to the current canvas of the system so that the cropped imageview can be displayed on the screen paint paint = new paint () ;p Aint.setxfermodE (null); Canvas.drawbitmap (bitmap, 0, 0, paint); Bitmap.recycle ();} public void setroundwidth (int roundwidth, int roundheight) {this.roundwidth = Roundwidth;this.roundheight = RoundHeight;} private void Drawroundangle (canvas canvas) {Paint maskpaint = new Paint (); Maskpaint.setantialias (True); Maskpaint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.CLEAR)); Path Maskpath = new Path (); Maskpath.addroundrect (New RECTF (0.0F, 0.0F, GetWidth (), GetHeight ()), Roundwidth, Roundheight, Path.Direction.CW); This is set up with fill mode, very critical maskpath.setfilltype (Path.FillType.INVERSE_WINDING); Canvas.drawpath (Maskpath, Maskpaint);}}
Android Custom rounded Corners ImageView