Android Corner Rectangle creation Tool RoundRect class

Source: Internet
Author: User

Tool class RoundRect class for converting normal images to rounded images (copy to use):

ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PorterDuff;ImportAndroid.graphics.PorterDuffXfermode;ImportAndroid.graphics.RectF;Importandroid.graphics.drawable.BitmapDrawable;Importandroid.graphics.drawable.Drawable; Public classRoundRect {Private intwidth; Private intheight; Private floatCornerradius; /*** Basic parameters for initializing rounded rectangles * *@paramwidth Picture Wide *@paramHeight Picture Altitude *@paramCornerradius Fillet radius*/     PublicRoundRect (intWidthintHeightfloatCornerradius) {         This. width =width;  This. Height =height;  This. Cornerradius =Cornerradius; }    /*** Used to convert a normal picture to a rounded rectangle image * *@paramImageID Image Resource ID *@paramContext Contextual Object *@returnoutput converted rounded rectangle image*/Bitmap Toroundrect (context context,intImageID) {        //creating a Bitmap objectBitmap photo =Bitmapfactory.decoderesource (Context.getresources (), ImageID); //Create a new Darwable object based on the source filedrawable imagedrawable =Newbitmapdrawable (photo); //Create a new output imageBitmap output =bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888); Canvas Canvas=NewCanvas (output); //Create a new rectangleRECTF Outerrect =NewRECTF (0, 0, width, height); //produces a red rounded rectanglePaint paint =NewPaint (Paint.anti_alias_flag);        Paint.setcolor (color.red);        Canvas.drawroundrect (Outerrect, Cornerradius, Cornerradius, paint); //draw the source picture onto the rounded rectanglePaint.setxfermode (NewPorterduffxfermode (PorterDuff.Mode.SRC_IN)); Imagedrawable.setbounds (0, 0, width, height);        Canvas.savelayer (outerrect, Paint, canvas.all_save_flag);        Imagedrawable.draw (canvas);        Canvas.restore (); returnoutput; }}

Test results:

To create a rectangle icon:

 Public class extends Activity {    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);        Setcontentview (r.layout.image);         = (ImageView) Findviewbyid (r.id.image);         New RoundRect (500,500,100);         = Roundrect.toroundrect (this, r.drawable.kms);        Image.setimagebitmap (photo);}    }

Create a circle head:

 Public class extends Activity {    @Override    protectedvoid  onCreate (Bundle savedinstancestate) {         Super . OnCreate (savedinstancestate);        Setcontentview (r.layout.image);         = (ImageView) Findviewbyid (r.id.image);         New RoundRect (500,500,300);         = Roundrect.toroundrect (this, R.drawable.indark);        Image.setimagebitmap (photo);}    }

Original address: http://www.cnblogs.com/woider/p/5122911.html

Android Corner Rectangle creation Tool RoundRect class

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.