In the project, we often use a circular picture, but Android itself is not provided, then I can only do it ourselves.
The first way , custom Circleimageview:
public class Circleimageview extends ImageView {private static final scaletype Scale_type = Scaletype.center_crop;
private static final Bitmap.config bitmap_config = Bitmap.Config.ARGB_8888;
private static final int colordrawable_dimension = 2;
The thickness default value for a circular border.
If 0, there is no border of sky-blue gradients.
private static final int default_border_width = 0;
private static final int default_border_color = Color.Black;
Private final RECTF mdrawablerect = new RECTF ();
Private final RECTF mborderrect = new RECTF ();
Private final Matrix Mshadermatrix = new Matrix ();
Private final Paint mbitmappaint = new Paint ();
Private final Paint mborderpaint = new Paint ();
private int mbordercolor = Default_border_color;
private int mborderwidth = Default_border_width;
Private Bitmap Mbitmap;
Private Bitmapshader Mbitmapshader;
private int mbitmapwidth;
private int mbitmapheight;
private float Mdrawableradius;
private float Mborderradius;
Private Boolean Mready; PriVate Boolean msetuppending;
Private final Paint mflagbackgroundpaint = new Paint ();
Private final Textpaint mflagtextpaint = new Textpaint ();
Private String Mflagtext;
Private Boolean mshowflag = false;
Private Rect mflagtextbounds = new Rect ();
Shader msweepgradient = null;
Public Circleimageview {Super (context);
Init ();
Public Circleimageview (context, AttributeSet attrs) {This (context, attrs, 0);
Public Circleimageview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Init ();
private void Init () {super.setscaletype (scale_type);
Mready = true;
if (msetuppending) {setup ();
Msetuppending = false;
@Override public ScaleType Getscaletype () {return scale_type; @Override public void Setscaletype (ScaleType scaletype) {if (ScaleType!= scale_type) {throw new Illegala Rgumentexception (String.Format ("ScaletypE%s not supported. ", ScaleType)); @Override public void Setadjustviewbounds (Boolean adjustviewbounds) {if (adjustviewbounds) {throw new
IllegalArgumentException ("Adjustviewbounds not supported.");
}} @Override protected void OnDraw (Canvas Canvas) {if (getdrawable () = null) {return;
Canvas.drawcircle (GetWidth ()/2, GetHeight ()/2, Mdrawableradius, mbitmappaint);
if (mborderwidth!= 0) {canvas.save ();
Canvas.rotate (GetWidth ()/2, GetHeight ()/2);
Canvas.drawcircle (GetWidth ()/2, GetHeight ()/2, Mborderradius, mborderpaint);
Canvas.restore (); } if (Mshowflag && mflagtext!= null) {Canvas.drawarc (Mborderrect, MB, false, Mflagbackgroundpaint)
;
Mflagtextpaint.gettextbounds (mflagtext, 0, Mflagtext.length (), mflagtextbounds); Canvas.drawtext (Mflagtext, GetWidth ()/2, (float) (3 + math.cos ((float) (Math.PI * 5/18)) * Gethei Ght () /4 + mflagtextbounds.height ()/3), mflagtextpaint); } @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW
, OLDH);
Setup ();
public int Getbordercolor () {return mbordercolor;
public void setBorderColor (int bordercolor) {if (bordercolor = = Mbordercolor) {return;
} Mbordercolor = bordercolor;
Mborderpaint.setcolor (Mbordercolor);
Invalidate ();
public int getborderwidth () {return mborderwidth;
/** * @param borderwidth * rounded border thickness.
*/public void setborderwidth (int borderwidth) {if (BorderWidth = = mborderwidth) {return;
} mborderwidth = BorderWidth;
Setup ();
@Override public void Setimagebitmap (Bitmap bm) {Super.setimagebitmap (BM);
Mbitmap = BM;
Setup ();
@Override public void setimagedrawable (drawable drawable) {super.setimagedrawable (drawable); Mbitmap = getbitmapfromdrawable (drawable);
Setup ();
@Override public void Setimageresource (int resid) {super.setimageresource (RESID);
Mbitmap = Getbitmapfromdrawable (getdrawable ());
Setup ();
@Override public void Setimageuri (Uri uri) {Super.setimageuri (URI);
Mbitmap = Getbitmapfromdrawable (getdrawable ());
Setup ();
Private Bitmap getbitmapfromdrawable (drawable drawable) {if (drawable = null) {return null;
} if (drawable instanceof bitmapdrawable) {return (bitmapdrawable) drawable). Getbitmap ();
try {Bitmap Bitmap; if (drawable instanceof colordrawable) {bitmap = Bitmap.createbitmap (Colordrawable_dimension, Colordrawable_d
Imension, Bitmap_config); else {bitmap = Bitmap.createbitmap (Drawable.getintrinsicwidth (), Drawable.getintrinsicheight (), bitmap_conf
IG);
} Canvas Canvas = new Canvas (bitmap);
Drawable.setbounds (0, 0, canvas.getwidth (), Canvas.getheight ()); Drawable.draw (Canvas);
return bitmap;
catch (OutOfMemoryError e) {return null;
} private void Setup () {if (!mready) {msetuppending = true;
Return
} if (Mbitmap = = null) {return;
} Mbitmapshader = new Bitmapshader (Mbitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Mbitmappaint.setantialias (TRUE);
Mbitmappaint.setshader (Mbitmapshader);
Mborderpaint.setstyle (Paint.Style.STROKE);
Mborderpaint.setantialias (TRUE);
Mborderpaint.setcolor (Mbordercolor);
Mborderpaint.setstrokewidth (Mborderwidth);
Mbitmapheight = Mbitmap.getheight ();
Mbitmapwidth = Mbitmap.getwidth ();
Mborderrect.set (0, 0, getwidth (), getheight ());
Mborderradius = Math.min (Mborderrect.height ()-mborderwidth)/2, (Mborderrect.width ()-mborderwidth)/2); Mdrawablerect.set (Mborderwidth, Mborderwidth, Mborderrect.width ()-Mborderwidth, Mborderrect.height ()-MBorderWidth
); Mdrawableradius = Math.min (Mdrawablerect.height ()/ 2, Mdrawablerect.width ()/2);
Mflagbackgroundpaint.setcolor (Color.Black & 0X66FFFFFF);
Mflagbackgroundpaint.setflags (Textpaint.anti_alias_flag);
Mflagtextpaint.setflags (Textpaint.anti_alias_flag);
Mflagtextpaint.settextalign (Align.center);
Mflagtextpaint.setcolor (Color.White);
Mflagtextpaint. Settextsize (Getresources (). Getdisplaymetrics (). density * 18); Msweepgradient = new Sweepgradient (GetWidth ()/2, GetHeight ()/2, new int[] {Color.rgb (255, 255, 255), Color.rgb (1
, 209, 255)}, NULL);
Mborderpaint.setshader (msweepgradient);
Updateshadermatrix ();
Invalidate ();
private void Updateshadermatrix () {float scale;
float dx = 0;
float dy = 0;
Mshadermatrix.set (NULL); if (Mbitmapwidth * mdrawablerect.height () > Mdrawablerect.width () * mbitmapheight) {scale = Mdrawablerect.hei
Ght ()/(float) mbitmapheight;
DX = (mdrawablerect.width ()-mbitmapwidth * scale) * 0.5f; } else {sCale = Mdrawablerect.width ()/(float) mbitmapwidth;
DY = (mdrawablerect.height ()-mbitmapheight * scale) * 0.5f;
} mshadermatrix.setscale (scale, scale);
Mshadermatrix.posttranslate ((int) (dx + 0.5f) + mborderwidth, (int) (dy + 0.5f) + mborderwidth);
Mbitmapshader.setlocalmatrix (Mshadermatrix);
public void Setshowflag (Boolean show) {Mshowflag = Show;
Invalidate ();
public void Setflagtext (String text) {mflagtext = text;
Invalidate ();
}
}
Use, remember not to quote into the V4 newspaper of the circleimageview!!
Another article to share the second way: to trim the picture into a circle, which gives us the visual effect is the same
We often use the circular picture, then, is the picture to cut, or custom round imageview, if there is no special requirements, we certainly choose the former, the reason is very simple, because simple!! Directly on the source code, is so rude willful ~ ~ ~
/**
* Trim picture to circle
/public
static Bitmap createcircleimage (Bitmap source) {
int length = Source.getwidth ( ) < Source.getheight ()? Source.getwidth (): Source.getheight ();
Paint Paint = new Paint ();
Paint.setantialias (true);
Bitmap target = bitmap.createbitmap (length, length, Bitmap.Config.ARGB_8888);
Canvas Canvas = new Canvas (target);
Canvas.drawcircle (LENGTH/2, LENGTH/2, LENGTH/2, paint);
Paint.setxfermode (New Porterduffxfermode (PorterDuff.Mode.SRC_IN));
Canvas.drawbitmap (source, 0, 0, paint);
return target;
}
Just type in the bitmap, and then return the rounded bitmap to you and set it to the ImageView, is not very simple.
In addition, if you take out the bitmap from the ImageView, you need only one line of code:
From ImageView Bitmap
Bitmap BM = (bitmapdrawable) (ImageView). getdrawable ()). Getbitmap ();
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.