[Java]View PlainCopy
- Package Xiaosi. Roundconcer;
- Import android.app.Activity;
- Import Android.graphics.Bitmap;
- Import Android.graphics.Bitmap.Config;
- Import Android.graphics.BitmapFactory;
- Import Android.graphics.Canvas;
- Import Android.graphics.Paint;
- Import Android.graphics.PorterDuff.Mode;
- Import Android.graphics.PorterDuffXfermode;
- Import Android.graphics.Rect;
- Import Android.graphics.RectF;
- Import Android.os.Bundle;
- Import Android.widget.ImageView;
- Public class Roundconceractivity extends Activity
- {
- /** Called when the activity is first created. * /
- private ImageView roundimage = null;
- @Override
- public void OnCreate (Bundle savedinstancestate)
- {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Roundimage = (ImageView) Findviewbyid (r.id.roundconcer);
- Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.a);
- Roundimage.setimagebitmap (Getroundcornerimage (bitmap, 50));
- }
- public static Bitmap getroundcornerimage (Bitmap Bitmap, int roundpixels)
- {
- //Create a bitmap of the same size as the original picture
- Bitmap roundconcerimage = Bitmap.createbitmap (Bitmap.getwidth (),
- Bitmap.getheight (), config.argb_8888);
- //Create a canvas with bitmap roundconcerimage
- Canvas canvas = new Canvas (roundconcerimage);
- //Create brushes
- Paint paint = new paint ();
- //Create a rectangle of the same size as the original picture
- Rect rect = new Rect (0, 0, Bitmap.getwidth (), Bitmap.getheight ());
- RECTF RECTF = new RECTF (rect);
- //anti-aliasing
- Paint.setantialias (true);
- //Draw a rounded rectangle of the same size as the original picture
- Canvas.drawroundrect (RECTF, Roundpixels, roundpixels, paint);
- //Set intersect mode
- Paint.setxfermode (new Porterduffxfermode (mode.src_in));
- //Draw the picture to the rectangle
- Canvas.drawbitmap (bitmap, null, rect, paint);
- return roundconcerimage;
- }
- }
Learn more about drawing rounded pictures in the Android learning notes