Android realizes fillet picture solution _android

Source: Internet
Author: User
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;
}
}
Related Article

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.