The Android rectangle picture generates a circle, and a rectangular picture creates a rounded corner

Source: Internet
Author: User

Generally to do a positive round picture, can only be achieved on the basis of the square, otherwise it becomes an ellipse, the following said how to make a picture of the rectangle is a circle picture
Don't say much nonsense, no picture no truth, First:
Original:


After turning into a circle:


The following code:

    public static Bitmap Makeroundcorner (Bitmap Bitmap) {int width = bitmap.getwidth ();        int height = bitmap.getheight ();        int left = 0, top = 0, right = width, bottom = height;        float roundpx = HEIGHT/2;            if (width > height) {left = (width-height)/2;            top = 0;            right = left + height;        bottom = height;            } else if (height > width) {left = 0;            top = (height-width)/2;            right = width;            Bottom = top + width;        ROUNDPX = WIDTH/2;        } zlog.i (TAG, "PS:" + Left + "," + Top + "," + Right + "," + Bottom ");        Bitmap output = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);        Canvas canvas = new canvas (output);        int color = 0xff424242;        Paint paint = new paint ();        Rect rect = new Rect (left, top, right, bottom);        RECTF RECTF = new RECTF (rect);        Paint.setantialias (TRUE); Canvas.drawargb (0, 0, 0, 0);        Paint.setcolor (color);        Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);        Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN));        Canvas.drawbitmap (Bitmap, rect, rect, paint);    return output; }


explained below:  
Because the picture is a rectangle, so the width of the picture, the height will certainly have one side to be smaller than the other side, to create a square has the smallest side as the benchmark, and then to cut the other side of the display range  
As for the radius of the fillet is half the width of the square, using the CSS will know that the circle is convenient  border-radius set to 50% can be, are a reason  
Android UI is too cumbersome  
The rectangle draws a rounded corner of the code:  

    public static Bitmap Makeroundcorner (Bitmap Bitmap, int px)    {        int width = bitmap.getwidth ();        int height = bitmap.getheight ();        Bitmap output = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888);        Canvas canvas = new canvas (output);        int color = 0xff424242;        Paint paint = new paint ();        Rect rect = new Rect (0, 0, width, height);        RECTF RECTF = new RECTF (rect);        Paint.setantialias (true);        Canvas.drawargb (0, 0, 0, 0);        Paint.setcolor (color);        Canvas.drawroundrect (RECTF, px, px, paint);        Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN));        Canvas.drawbitmap (Bitmap, rect, rect, paint);        return output;    }

The Android rectangle picture generates a circle, and a rectangular picture creates a rounded corner

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.