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 ;}