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;
}