Android clipping picture for circular picture realization principle and code _android

Source: Internet
Author: User
Previously found in the EoE forum to crop the picture as a circular picture method, but the effect is not very ideal, these days because of the company's business requirements, the need to cut the head to a round way to display, this method is based on the image of the passing of the height (height) and breadth (width) of the decision, if it is Width <= height, the crop will be cropped, the area is the width of the height from the top to the width of length; if width > height, then crop width, crop area is the height unchanged, width is the width of the picture is the center of the area, However, different business requirements, to crop the picture requirements are not the same, you can tailor the area according to the needs of the business.

Okay, no more, just go to the code.
Copy Code code as follows:

/**
* Convert the picture into a circle
* @param bitmap Incoming Bitmap object
* @return
*/
Public Bitmap Toroundbitmap (Bitmap Bitmap) {
int width = bitmap.getwidth ();
int height = bitmap.getheight ();
float ROUNDPX;
float Left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;
if (width <= height) {
ROUNDPX = WIDTH/2;
top = 0;
bottom = width;
left = 0;
right = width;
Height = width;
Dst_left = 0;
dst_top = 0;
Dst_right = width;
Dst_bottom = width;
} else {
ROUNDPX = HEIGHT/2;
Float clip = (width-height)/2;
left = clip;
right = Width-clip;
top = 0;
bottom = height;
width = height;
Dst_left = 0;
dst_top = 0;
Dst_right = height;
Dst_bottom = height;
}
Bitmap output = Bitmap.createbitmap (width,
Height, config.argb_8888);
Canvas Canvas = new Canvas (output);
final int color = 0xff424242;
Final Paint Paint = new Paint ();
Final Rect src = new Rect (int) left, (int) top, (int) right, (int) bottom);
Final Rect DST = new Rect (int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
Final RECTF RECTF = new RECTF (DST);
Paint.setantialias (TRUE);
Canvas.drawargb (0, 0, 0, 0);
Paint.setcolor (color);
Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);
Paint.setxfermode (New Porterduffxfermode (mode.src_in));
Canvas.drawbitmap (bitmap, SRC, DST, 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.