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