public class Mycirleimageview extends ImageView {
private static int RADIUS = 4; The width of the default fillet is 8dip, you can set the parameter structure, pass in the required value
Public Mycirleimageview (context context, AttributeSet Attrs) {
Super (context, attrs);
}
@Override
protected void OnDraw (canvas canvas) {
/** Get drawable*/
Bitmapdrawable drawable = (bitmapdrawable) getdrawable ();
if (drawable = = null) {return;}
if (getwidth () = = 0 | | getheight () = = 0) {return;}
/** get corresponding bitmap*/
Bitmap Fullbitmap = Drawable.getbitmap ();
/** gets the size of view in parent view
* GetWidth get the actual size of a view. Getmeasuredwidth is the size that a view wants to occupy in the parent view
*/
int scaledwidth = Getmeasuredwidth ();
int scaledheight = Getmeasuredheight ();
Bitmap Mscaledbitmap;
if (Scaledwidth = = Fullbitmap.getwidth () && scaledheight = = Fullbitmap.getheight ()) {
Mscaledbitmap = Fullbitmap;
} else {
Mscaledbitmap = Bitmap.createscaledbitmap (Fullbitmap, Scaledwidth, Scaledheight, true);
}
try {
Bitmap Roundbitmap = Getroundedcornerbitmap (GetContext (), Mscaledbitmap, RADIUS, Scaledwidth, Scaledheight, False, False, False, false);
Canvas.drawbitmap (roundbitmap, 0, 0, NULL);
} catch (Exception e) {
Super.ondraw (canvas);
}
}
/** Get fillet picture */
public static Bitmap Getroundedcornerbitmap (context context, Bitmap input, int pixels, int w, int h, Boolean squaretl, Boo Lean Squaretr, Boolean Squarebl, Boolean Squarebr) {
Bitmap output = Bitmap.createbitmap (W, H, config.argb_8888);
Canvas canvas = new canvas (output);
Final float mulitdensity = context.getresources (). Getdisplaymetrics (). density;
final int color = 0xff424242;
Final Paint paint = new paint ();
Final rect rect = new Rect (0, 0, W, h);
Final RECTF RECTF = new RECTF (rect);
Final float roundpx = pixels * mulitdensity;
Paint.setantialias (TRUE);
Canvas.drawargb (0, 0, 0, 0);
Paint.setcolor (color);
Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);
/** The following several judgments are implemented: whether to retain the corresponding right angle */
if (squaretl) {
Canvas.drawrect (0, 0, W/2, H/2, paint);
}
if (SQUARETR) {
Canvas.drawrect (W/2, 0, W, H/2, paint);
}
if (SQUAREBL) {
Canvas.drawrect (0, H/2, W/2, h, paint);
}
if (SQUAREBR) {
Canvas.drawrect (W/2, H/2, W, h, paint);
}
/** sets the mode at which two pictures intersect.
* Under normal circumstances, drawing on an existing image will add a new layer of shape on top of it. If the new paint is completely opaque, it will completely cover the paint below, and if it is partially transparent, it will be stained with the following color.
* and Setxfermode can solve this problem.
* Paint.setxfermode (New Porterduffxfermode (mode.src_in));
* CANVAS.DRAWBITMAP (Srcbitmap, 0f, 0f, paint); Canvas's original picture can be understood as the background, is DST, the new picture can be understood as the foreground, is SRC.
* */
Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN));
Canvas.drawbitmap (input, 0, 0, paint);
return output;
}
}
----------------------- use Mycirleimageview as ImageView in the layout file to-----------------------------------
Android Custom rounded Corners ImageView