Android rounded corner image Solution

Source: Internet
Author: User

Now let's take a look at how to turn the four corners of the image into a circle. Why should we do this? If this is the case, the interface will be very beautiful. Let's take a look at the code.
Java code: Copy codeThe Code is 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 = 0xff0000242;
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 porterduxfermode (Mode. SRC_IN ));
Canvas. drawBitmap (bitmap, rect, rect, paint );
Return output;
}

Call this method when you need a rounded corner. The first parameter is the image to be converted to a rounded corner. The second parameter is the degree of the rounded corner. The larger the value, the larger the rounded corner.
The following is an example of interest. I have a LinearLayout. I want to set the background image to a rounded corner.
Layout file main. xml
Java code:Copy codeThe Code is 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 codeThe Code is 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. porterduduxfermode;
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 when the activity is first created .*/
@ 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 = 0xff0000242;
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 porterduxfermode (Mode. SRC_IN ));
Canvas. drawBitmap (bitmap, rect, rect, paint );
Return output;
}
}

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.