Android uses bitmap to get rounded rectangles, round pictures

Source: Internet
Author: User

1. In many cases, we want to display the picture resources, need to display his resources as rounded, sample source code is as follows:

 Public StaticBitmap Getroundedcornerbitmap (Bitmap Bitmap,floatroundpx) {Bitmap Output=Bitmap.createbitmap (Bitmap.getwidth (), Bitmap. GetHeight (), config.argb_8888); Canvas Canvas=NewCanvas (output); Final intcolor = 0xff424242; FinalPaint paint =NewPaint (); FinalRect rect =NewRect (0, 0, Bitmap.getwidth (), Bitmap.getheight ()); FinalRECTF RECTF =NewRECTF (rect); Paint.setantialias (true); Canvas.drawargb (0, 0, 0, 0);            Paint.setcolor (color);              Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); Paint.setxfermode (NewPorterduffxfermode (mode.src_in));                 Canvas.drawbitmap (Bitmap, rect, rect, paint); returnoutput; }    

Note: Bitmap is a picture resource; roundpx angle value float;

2. Get a picture of the circular resource.

 Public StaticBitmap Getovalbitmap (Bitmap Bitmap) {Bitmap output=Bitmap.createbitmap (Bitmap.getwidth (), Bitmap. GetHeight (), config.argb_8888); Canvas Canvas=NewCanvas (output); Final intcolor = 0xff424242; FinalPaint paint =NewPaint (); FinalRect rect =NewRect (0, 0, Bitmap.getwidth (), Bitmap.getheight ()); FinalRECTF RECTF =NewRECTF (rect); Paint.setantialias (true); Canvas.drawargb (0, 0, 0, 0);                    Paint.setcolor (color);             Canvas.drawoval (RECTF, paint); Paint.setxfermode (NewPorterduffxfermode (mode.src_in));            Canvas.drawbitmap (Bitmap, rect, rect, paint); returnoutput; }    

Note: The above code is tested and practical, copy can be used, if there is a problem, please leave a message to talk;

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.