Learn more about drawing rounded pictures in the Android learning notes

Source: Internet
Author: User


[Java]View PlainCopy
    1. Package Xiaosi. Roundconcer;
    2. Import android.app.Activity;
    3. Import Android.graphics.Bitmap;
    4. Import Android.graphics.Bitmap.Config;
    5. Import Android.graphics.BitmapFactory;
    6. Import Android.graphics.Canvas;
    7. Import Android.graphics.Paint;
    8. Import Android.graphics.PorterDuff.Mode;
    9. Import Android.graphics.PorterDuffXfermode;
    10. Import Android.graphics.Rect;
    11. Import Android.graphics.RectF;
    12. Import Android.os.Bundle;
    13. Import Android.widget.ImageView;
    14. Public class Roundconceractivity extends Activity
    15. {
    16. /** Called when the activity is first created. * /
    17. private ImageView roundimage = null;
    18. @Override
    19. public void OnCreate (Bundle savedinstancestate)
    20. {
    21. super.oncreate (savedinstancestate);
    22. Setcontentview (R.layout.main);
    23. Roundimage = (ImageView) Findviewbyid (r.id.roundconcer);
    24. Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.a);
    25. Roundimage.setimagebitmap (Getroundcornerimage (bitmap, 50));
    26. }
    27. public static Bitmap getroundcornerimage (Bitmap Bitmap, int roundpixels)
    28. {
    29. //Create a bitmap of the same size as the original picture
    30. Bitmap roundconcerimage = Bitmap.createbitmap (Bitmap.getwidth (),
    31. Bitmap.getheight (), config.argb_8888);
    32. //Create a canvas with bitmap roundconcerimage
    33. Canvas canvas = new Canvas (roundconcerimage);
    34. //Create brushes
    35. Paint paint = new paint ();
    36. //Create a rectangle of the same size as the original picture
    37. Rect rect = new Rect (0, 0, Bitmap.getwidth (), Bitmap.getheight ());
    38. RECTF RECTF = new RECTF (rect);
    39. //anti-aliasing
    40. Paint.setantialias (true);
    41. //Draw a rounded rectangle of the same size as the original picture
    42. Canvas.drawroundrect (RECTF, Roundpixels, roundpixels, paint);
    43. //Set intersect mode
    44. Paint.setxfermode (new Porterduffxfermode (mode.src_in));
    45. //Draw the picture to the rectangle
    46. Canvas.drawbitmap (bitmap, null, rect, paint);
    47. return roundconcerimage;
    48. }
    49. }

Learn more about drawing rounded pictures in the Android learning notes

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.