Rounded rectangle picture is very popular in Apple products, compared to ordinary rectangles, a lot of people like the rounded rectangle picture, because it avoids the blunt right angle, brings a better user experience, here are a few examples of design:
The following is implemented in Android to draw a normal rectangular picture into a rounded rectangle. First look at the final effect:
Code Listing:
- 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.PorterDuffXfermode;
- 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;
- @Override
- Public 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 (400,photo,));
- //myimageview.setimagebitmap (Createstarphoto (500,400,photo));
- }
- @Override
- Public boolean oncreateoptionsmenu (Menu menu) {
- Getmenuinflater (). Inflate (R.menu.activity_main, menu);
- return true;
- }
- Private Bitmap createframedphoto (int x, int y, Bitmap image, float outerradiusrat) {
- //Create a new Darwable object based on the source file
- drawable imagedrawable = new bitmapdrawable (image);
- //Create a new output image
- Bitmap output = Bitmap.createbitmap (x, y, Bitmap.Config.ARGB_8888);
- Canvas canvas = new canvas (output);
- //Create a new rectangle
- RECTF outerrect = new RECTF (0, 0, X, y);
- //produce a red rounded rectangle
- Paint paint = new paint (Paint.anti_alias_flag);
- Paint.setcolor (color.red);
- Canvas.drawroundrect (Outerrect, Outerradiusrat, Outerradiusrat, paint);
- //Draw the source picture onto the rounded rectangle
- //Detailed see http://lipeng88213.iteye.com/blog/1189452
- Paint.setxfermode (new Porterduffxfermode (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;
- }
- }
The principle of implementation: by drawing a final output shape in a canvas, and then displaying the graphic as a mask, the final picture shape is the shape of the previously drawn graphic. The function that specifically works is this:
Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN));
Used to set the overlay mode.
With this principle, I can draw pictures of various shapes, such as:
Code:
- Private Bitmap Createstarphoto (int x, int y, Bitmap image)
- {
- //Create a new Darwable object based on the source file
- drawable imagedrawable = new bitmapdrawable (image);
- //Create a new output image
- Bitmap output = Bitmap.createbitmap (x, y, Bitmap.Config.ARGB_8888);
- Canvas canvas = new canvas (output);
- //Create a new rectangle
- RECTF outerrect = new RECTF (0, 0, X, y);
- Paint paint = new paint (Paint.anti_alias_flag);
- Paint.setcolor (color.red);
- Path PATH = new path ();
- //Draw triangles
- //path.moveto (0, 0);
- //path.lineto (320, 250);
- //path.lineto (400, 0);
- //Draw a positive infinity shape
- long tmpx,tmpy;
- Path.moveto (a ); This point is the starting point of the polygon
- For (int i=0;i<=5;i++)
- {
- TMPX = (long) (200+200*math.sin ((i*72+) *2*math.pi/));
- Tmpy = (long) (200+200*math.cos ((i*72+) *2*math.pi/));
- Path.lineto (TMPX, tmpy);
- }
- Path.close (); //Make these points form a closed polygon
- Canvas.drawpath (path, paint);
- //canvas.drawcircle (n, +, +, paint);
- //Draw the source picture onto the rounded rectangle
- //produce a red rounded rectangle
- Paint.setxfermode (new Porterduffxfermode (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;
- }
Draw rounded rectangles in Android and any shape pictures