Android uses the bitmap class to turn a rectangular picture into a circular method _android

Source: Internet
Author: User

Generally to do a circular picture, can only be based on the square can be achieved, otherwise it becomes an ellipse, the following say how to make a rectangular picture of a circular picture
Don't say much nonsense, no picture no truth, first on the map:
Original:

After turning into a positive 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; 
 }


Here's another explanation:
Because the picture is rectangular, so the picture of the width and height of the image must be smaller than the other side, to generate a square on the smallest side as the benchmark, and then trim to the other side of the display range
As for the radius of the rounded corner is a square width of half, with CSS to know, draw a circle is very convenient Border-radius set to 50% on it, is a truth
Android's UI is so tedious.

Rectangle to draw a rounded corner 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; 

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.