Now we come to see how to turn the four corners of the picture into a circle, why do that, if so the interface will be very beautiful, let's look at the code below.
Java code:
Copy Code code as follows:
public static Bitmap Toroundcorner (Bitmap Bitmap, int pixels) {
Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888);
Canvas Canvas = new Canvas (output);
final int color = 0xff424242;
Final Paint Paint = new Paint ();
Final Rect Rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ());
Final RECTF RECTF = new RECTF (rect);
Final float roundpx = pixels;
Paint.setantialias (TRUE);
Canvas.drawargb (0, 0, 0, 0);
Paint.setcolor (color);
Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);
Paint.setxfermode (New Porterduffxfermode (mode.src_in));
Canvas.drawbitmap (Bitmap, rect, rect, paint);
return output;
}
When we need rounded corners, call this method, the first parameter is passed into the need to convert to rounded corners of the picture, the second parameter is the degree of fillet, the larger the number, the greater the rounded corners
Here's an example of interest, I have a linearlayout, I want to set the background picture to rounded corners.
Layout file Main.xml
Java code:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/layout"
android:orientation= "Vertical"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
>
</LinearLayout>
Java code:
Copy Code code as follows:
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.Bitmap.Config;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.PorterDuff.Mode;
Import Android.graphics.PorterDuffXfermode;
Import Android.graphics.Rect;
Import Android.graphics.RectF;
Import android.graphics.drawable.BitmapDrawable;
Import android.graphics.drawable.Drawable;
Import Android.os.Bundle;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;
public class MyActivity extends activity {
/** called the activity is a. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
drawable drawable = Getresources (). getdrawable (r.drawable.bg);
Bitmapdrawable bitmapdrawable = (bitmapdrawable) drawable;
Bitmap Bitmap = Bitmapdrawable.getbitmap ();
LinearLayout layout = (linearlayout) Findviewbyid (r.id.layout);
drawable drawable = Getresources (). getdrawable (r.drawable.bg);
Bitmapdrawable bitmapdrawable = (bitmapdrawable) drawable;
Bitmap Bitmap = Bitmapdrawable.getbitmap ();
bitmapdrawable bbb = new Bitmapdrawable (Toroundcorner (bitmap, 30));
Layout.setbackgrounddrawable (BBB);
ImageView ImageView = (imageview) Findviewbyid (r.id.imgshow);
Imageview.setimagebitmap (Myactivity.getroundedcornerbitmap (bitmap));
Imageview.setimagebitmap (Myactivity.toroundcorner (Bitmap, 20));
}
public static Bitmap Toroundcorner (Bitmap Bitmap, int pixels) {
Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888);
Canvas Canvas = new Canvas (output);
final int color = 0xff424242;
Final Paint Paint = new Paint ();
Final Rect Rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ());
Final RECTF RECTF = new RECTF (rect);
Final float roundpx = pixels;
Paint.setantialias (TRUE);
Canvas.drawargb (0, 0, 0, 0);
Paint.setcolor (color);
Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);
Paint.setxfermode (New Porterduffxfermode (mode.src_in));
Canvas.drawbitmap (Bitmap, rect, rect, paint);
return output;
}
}