Draw a rounded rectangle image and any shape image in Android

Source: Internet
Author: User

Rounded rectangular images are very popular in Apple products. Compared with ordinary rectangles, many people prefer the rounded rectangular image because it avoids the stiff right corner and brings a better user experience, below are several design examples:




In Android, the following code draws a normal rectangular image into a rounded rectangle. First, let's look at the final result:



Code List:
Package COM. example. phototest; import android. OS. bundle; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. path; import android. graphics. porterduxfermode; import android. graphics. porterduff; import android. graphics. rectf; import android. graphics. drawable. bitmapdrawable; import android. graphics. drawable. drawable; import android. view. menu; import android. widget. imageview; public class mainactivity extends activity {private imageview myimageview; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); myimageview = (imageview) findviewbyid (R. id. imageview1); bitmap photo = bitmapfactory. decoderesource (getresources (), R. drawable. photo1); myimageview. setimagebitmap (createframedphoto (500,400, photo, 20); // myimageview. setimagebitmap (createstarphoto (500,400, photo) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true;} private bitmap createframedphoto (int x, int y, bitmap image, float outerradiusrat) {// create a darwable object drawable imagedrawable = new bitmapdrawable (image) based on the source file; // create a new output image bitmap output = bitmap. createbitmap (X, Y, bitmap. config. argb_8888); canvas = new canvas (output); // create a new rectf outerrect = new rectf (0, 0, x, y ); // generate a red rounded rectangle paint = new paint (paint. anti_alias_flag); paint. setcolor (color. red); canvas. drawroundrect (outerrect, outerradiusrat, outerradiusrat, paint); // draw the source image to this rounded rectangle // see http://lipeng88213.iteye.com/blog/1189452paint.setXfermode (New porterduxfermode (porterduff. mode. src_in); imagedrawable. setbounds (0, 0, x, y); canvas. savelayer (outerrect, paint, canvas. all_save_flag); imagedrawable. draw (canvas); canvas. restore (); Return output ;}}

Implementation principle: Draw a final output shape in a canvas, and then display the image in a manner similar to a mask. The final image shape is the shape of the previously drawn image. The specific function is as follows:

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
Used to set the overlay mode. With this principle, I can draw images of various shapes, such:

Code:

Private bitmap createstarphoto (int x, int y, bitmap image) {// create a darwable object drawable imagedrawable = new bitmapdrawable (image) based on the source file ); // create a new output image bitmap output = bitmap. createbitmap (X, Y, bitmap. config. argb_8888); canvas = new canvas (output); // create a new rectangle rectf outerrect = new rectf (0, 0, x, y); paint = new paint (paint. anti_alias_flag); paint. setcolor (color. red); Path = New Path (); // draw a triangle // path. moveTo (0, 0); // path. lineto (320,250); // path. lineto (400, 0); // draw a positive infinity long tmpx, tmpy; Path. moveTo (200,200); // This point is the start point of the polygon for (INT I = 0; I <= 5; I ++) {tmpx = (long) (200 + 200 * Math. sin (I * 72 + 36) * 2 * Math. PI/360); tmpy = (long) (200 + 200 * Math. cos (I * 72 + 36) * 2 * Math. PI/360); Path. lineto (tmpx, tmpy);} path. close (); // make these points form a closed polygon canvas. drawpath (path, paint); // canvas. drawcircle (100,100,100, paint); // draw the source image to this rounded rectangle // generate a red rounded rectangle painting. setxfermode (New porterduxfermode (porterduff. mode. src_in); imagedrawable. setbounds (0, 0, x, y); canvas. savelayer (outerrect, paint, canvas. all_save_flag); imagedrawable. draw (canvas); canvas. restore (); 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.