Android Custom rounded Corners ImageView

Source: Internet
Author: User


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

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.