Draw rounded rectangles in Android and any shape pictures

Source: Internet
Author: User

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:

  1. Package com.example.phototest;
  2. import Android.os.Bundle;
  3. import android.app.Activity;
  4. import Android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import Android.graphics.Canvas;
  7. import Android.graphics.Color;
  8. import Android.graphics.Paint;
  9. import Android.graphics.Path;
  10. import Android.graphics.PorterDuffXfermode;
  11. import Android.graphics.PorterDuff;
  12. import Android.graphics.RectF;
  13. import android.graphics.drawable.BitmapDrawable;
  14. import android.graphics.drawable.Drawable;
  15. import Android.view.Menu;
  16. import Android.widget.ImageView;
  17. Public class mainactivity extends Activity {
  18. private ImageView Myimageview;
  19. @Override
  20. Public void onCreate (Bundle savedinstancestate) {
  21. Super. OnCreate (savedinstancestate);
  22. Setcontentview (R.layout.activity_main);
  23. myimageview= (ImageView) Findviewbyid (R.ID.IMAGEVIEW1);
  24. Bitmap photo = Bitmapfactory.decoderesource (Getresources (), r.drawable.photo1);
  25. Myimageview.setimagebitmap (Createframedphoto (400,photo,));
  26. //myimageview.setimagebitmap (Createstarphoto (500,400,photo));
  27. }
  28. @Override
  29. Public boolean oncreateoptionsmenu (Menu menu) {
  30. Getmenuinflater (). Inflate (R.menu.activity_main, menu);
  31. return true;
  32. }
  33. Private Bitmap createframedphoto (int x, int y, Bitmap image, float outerradiusrat) {
  34. //Create a new Darwable object based on the source file
  35. drawable imagedrawable = new bitmapdrawable (image);
  36. //Create a new output image
  37. Bitmap output = Bitmap.createbitmap (x, y, Bitmap.Config.ARGB_8888);
  38. Canvas canvas = new canvas (output);
  39. //Create a new rectangle
  40. RECTF outerrect = new RECTF (0, 0, X, y);
  41. //produce a red rounded rectangle
  42. Paint paint = new paint (Paint.anti_alias_flag);
  43. Paint.setcolor (color.red);
  44. Canvas.drawroundrect (Outerrect, Outerradiusrat, Outerradiusrat, paint);
  45. //Draw the source picture onto the rounded rectangle
  46. //Detailed see http://lipeng88213.iteye.com/blog/1189452
  47. Paint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.SRC_IN));
  48. Imagedrawable.setbounds (0, 0, X, y);
  49. Canvas.savelayer (outerrect, Paint, canvas.all_save_flag);
  50. Imagedrawable.draw (canvas);
  51. Canvas.restore ();
  52. return output;
  53. }
  54. }

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:

  1. Private Bitmap Createstarphoto (int x, int y, Bitmap image)
  2. {
  3. //Create a new Darwable object based on the source file
  4. drawable imagedrawable = new bitmapdrawable (image);
  5. //Create a new output image
  6. Bitmap output = Bitmap.createbitmap (x, y, Bitmap.Config.ARGB_8888);
  7. Canvas canvas = new canvas (output);
  8. //Create a new rectangle
  9. RECTF outerrect = new RECTF (0, 0, X, y);
  10. Paint paint = new paint (Paint.anti_alias_flag);
  11. Paint.setcolor (color.red);
  12. Path PATH = new path ();
  13. //Draw triangles
  14. //path.moveto (0, 0);
  15. //path.lineto (320, 250);
  16. //path.lineto (400, 0);
  17. //Draw a positive infinity shape
  18. long tmpx,tmpy;
  19. Path.moveto (a ); This point is the starting point of the polygon
  20. For (int i=0;i<=5;i++)
  21. {
  22. TMPX = (long) (200+200*math.sin ((i*72+) *2*math.pi/));
  23. Tmpy = (long) (200+200*math.cos ((i*72+) *2*math.pi/));
  24. Path.lineto (TMPX, tmpy);
  25. }
  26. Path.close (); //Make these points form a closed polygon
  27. Canvas.drawpath (path, paint);
  28. //canvas.drawcircle (n, +, +, paint);
  29. //Draw the source picture onto the rounded rectangle
  30. //produce a red rounded rectangle
  31. Paint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.SRC_IN));
  32. Imagedrawable.setbounds (0, 0, X, y);
  33. Canvas.savelayer (outerrect, Paint, canvas.all_save_flag);
  34. Imagedrawable.draw (canvas);
  35. Canvas.restore ();
  36. return output;
  37. }

Draw rounded rectangles in Android and any shape pictures

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.