Create a rounded image for android Image Processing

Source: Internet
Author: User

The following is an example of Improving the rounded corner image made by our predecessors.

Public static Bitmap roundCorners (final Bitmap source, final float radius ){

Int width = source. getWidth ();

Int height = source. getHeight ();

Paint paint = new Paint ();

Paint. setAntiAlias (true );

Paint. setColor (android. graphics. Color. WHITE );

Bitmap clipped = Bitmap. createBitmap (width, height, Bitmap. Config. ARGB_8888 );

Canvas canvas = new Canvas (clipped );

Canvas. drawRoundRect (new RectF (0, 0, width, height), radius, radius,

Paint );

Paint. setXfermode (new porterduxfermode (android. graphics. PorterDuff. Mode. SRC_IN ));

Canvas. drawBitmap (source, 0, 0, paint );

Source. recycle ();

Return clipped;

}

Example:

/**

* Round the corners of a {@ link Bitmap}

*

* @ Param source

* @ Param radius

* @ Return rounded corner bitmap

*/

Public static Bitmap roundCorners (final Bitmap source, final float radius ){

Int width = source. getWidth ();

Int height = source. getHeight ();

Paint paint = new Paint ();

Paint. setAntiAlias (true );

Paint. setColor (WHITE );

Bitmap clipped = Bitmap. createBitmap (width, height, ARGB_8888 );

Canvas canvas = new Canvas (clipped );

Canvas. drawRoundRect (new RectF (0, 0, width, height), radius, radius,

Paint );

Paint. setXfermode (new porterduxfermode (DST_IN ));

Bitmap rounded = Bitmap. createBitmap (width, height, ARGB_8888 );

Canvas = new Canvas (rounded );

Canvas. drawBitmap (source, 0, 0, null );

Canvas. drawBitmap (clipped, 0, 0, paint );

Source. recycle ();

Clipped. recycle ();

Return rounded;

}

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.