Android self-defined rounded corners ImageView

Source: Internet
Author: User

We often see images in some apps that show rounded corners. For example, QQ's contact icon and so on, to achieve a rounded picture is a way to directly use the rounded picture resources, of course, without rounded corners picture resources. We are also able to implement our own programs, and here is a way to define your own fillet ImageView:

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, this canvas corresponding memory for 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 will change bitmap data drawroundangle (CANVAS2);//Draw the cropped bitmap to the current canvas on 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 the fill mode set.        Very critical Maskpath.setfilltype (Path.FillType.INVERSE_WINDING); Canvas.drawpath (Maskpath, Maskpaint);}}

Android self-defined rounded corners ImageView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.