Set any corner of the bitmap picture in Android to rounded corners

Source: Internet
Author: User

http://blog.csdn.net/l448288137/article/details/48276681

Recent project development to use the fillet picture, the web found the round corner picture control is more rigid, only can be rounded corners. One of the best feeling is the semicircular corner link here. Thinking about it, I made a little improvement on the basis of this, so that the picture can be set to a rounded corner.

First on:

Core code

[Java]View PlainCopy
  1. Package fillet.sgn.com.filletimage;
  2. Import Android.graphics.Bitmap;
  3. Import Android.graphics.Canvas;
  4. Import Android.graphics.Color;
  5. Import Android.graphics.Paint;
  6. Import Android.graphics.PorterDuff;
  7. Import Android.graphics.PorterDuffXfermode;
  8. Import Android.graphics.Rect;
  9. Import Android.graphics.RectF;
  10. /**
  11. * Created by Liujinhua on 15/9/7.
  12. */
  13. Public class Bitmapfillet {
  14. public static final int corner_none = 0;
  15. public static final int corner_top_left = 1;
  16. public static final int corner_top_right = 1 << 1;
  17. public static final int corner_bottom_left = 1 << 2;
  18. public static final int corner_bottom_right = 1 << 3;
  19. public static final int corner_all = Corner_top_left | Corner_top_right | Corner_bottom_left |  Corner_bottom_right;
  20. public static final int corner_top = Corner_top_left |  Corner_top_right;
  21. public static final int corner_bottom = Corner_bottom_left |  Corner_bottom_right;
  22. public static final int corner_left = Corner_top_left |  Corner_bottom_left;
  23. public static final int corner_right = Corner_top_right |  Corner_bottom_right;
  24. public static Bitmap fillet (Bitmap Bitmap, int roundpx,int corners) {
  25. try {
  26. //The principle is: first create a transparent bitmap artboard with the same size as the picture
  27. ///Then draw an area of the desired shape on the artboard.
  28. //finally post the source image.
  29. final int width = bitmap.getwidth ();
  30. final int height = bitmap.getheight ();
  31. Bitmap Paintingboard = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);
  32. Canvas canvas = new Canvas (Paintingboard);
  33. Canvas.drawargb (Color.transparent, Color.transparent, Color.transparent, color.transparent);
  34. Final Paint paint = new paint ();
  35. Paint.setantialias (true);
  36. Paint.setcolor (Color.Black);
  37. //Draw 4 rounded corners
  38. final RECTF RECTF = new RECTF (0, 0, width, height);
  39. Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);
  40. //Remove unwanted fillets
  41. int notroundedcorners = Corners ^ Corner_all;
  42. if ((Notroundedcorners & corner_top_left)! = 0) {
  43. Cliptopleft (Canvas,paint,roundpx,width,height);
  44. }
  45. if ((Notroundedcorners & corner_top_right)! = 0) {
  46. Cliptopright (canvas, Paint, roundpx, width, height);
  47. }
  48. if ((Notroundedcorners & corner_bottom_left)! = 0) {
  49. Clipbottomleft (Canvas,paint,roundpx,width,height);
  50. }
  51. if ((Notroundedcorners & corner_bottom_right)! = 0) {
  52. Clipbottomright (canvas, Paint, roundpx, width, height);
  53. }
  54. Paint.setxfermode (new Porterduffxfermode (PorterDuff.Mode.SRC_IN));
  55. //Post map
  56. final rect src = new rect (0, 0, width, height);
  57. final Rect dst = src;
  58. Canvas.drawbitmap (bitmap, SRC, DST, paint);
  59. return paintingboard;
  60. } catch (Exception exp) {
  61. return bitmap;
  62. }
  63. }
  64. private static void Cliptopleft (final canvas canvas, final paint paint, int offset, int width, int height) {  
  65. Final rect block = new rect (0, 0, offset, offset);
  66. Canvas.drawrect (block, paint);
  67. }
  68. private static void Cliptopright (final canvas canvas, final paint paint, int offset, int Widt h, int height) {
  69. Final rect block = new rect (Width-offset, 0, width, offset);
  70. Canvas.drawrect (block, paint);
  71. }
  72. private static void Clipbottomleft (final canvas canvas, final paint paint, int offset, int wid th, int height) {
  73. Final rect block = new rect (0, Height-offset, offset, height);
  74. Canvas.drawrect (block, paint);
  75. }
  76. private static void Clipbottomright (final canvas canvas, final paint paint, int offset, int WI DTH, int height) {
  77. Final rect block = new rect (Width-offset, height-offset, width, height);
  78. Canvas.drawrect (block, paint);
  79. }
  80. }

Set any corner of the bitmap picture in Android to rounded corners

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.