Sometimes we use the circular avatar in the application, the following is the use of circleimageview to achieve the circular head of the demo, the following effect and code, the effect is as shown
The implementation is also relatively simple, first in the project to build a Circleimageview package used to store Circleimageview class, will be directly to the Circleimageview class copied into the package can be used
Then, build a attrs.xml with a fairly simple code that defines the width and color of the round border
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= " Circleimageview ">
<attr name=" Border_width "format=" Dimension "/> <attr" name= border_color "
format= "Color"/>
</declare-styleable>
</resources>
Then you use Circleimageview in the layout file of your application's activity.
<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://" Schemas.android.com/apk/res/android "
xmlns:tools=" Http://schemas.android.com/tools "
android:layout_ Width= "Match_parent"
android:layout_height= "match_parent"
tools:context= " Com.example.icontest.MainActivity ">
<circleimageview. Circleimageview
xmlns:app= "Http://schemas.android.com/apk/res-auto"
android:id= "@+id/circleimageview"
android:layout_width= "100DP"
android:layout_height= "100DP"
android:layout_centerhorizontal= " True "
android:src=" "@mipmap/ic_launcher"
app:border_width= "2DP"
app:border_color= "#ccc"
/ >
</RelativeLayout>
The note is to write the Circleimageview. Circleimageview this full path, but also to specify
xmlns:app= "Http://schemas.android.com/apk/res-auto"
This is based on the entire layout.
Xmlns:android= "Http://schemas.android.com/apk/res/android"
, change the res/android to Res-auto, and then specify Border_width and Border_color two properties.
And then you can find it directly using the ID in the activity.
Circleimageview icon;
icon= (Circleimageview) Findviewbyid (R.id.circleimageview);
Finally is the code of the Circleimageview class, this online can be found, but here or post it
Package Circleimageview;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapShader;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.graphics.RectF;
Import Android.graphics.Shader;
Import android.graphics.drawable.BitmapDrawable;
Import android.graphics.drawable.ColorDrawable;
Import android.graphics.drawable.Drawable;
Import Android.net.Uri;
Import Android.util.AttributeSet;
Import Android.widget.ImageView;
Import COM.EXAMPLE.ICONTEST.R;
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;
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;
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);
TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.circleimageview, Defstyle, 0); Mborderwidth = A.getdimenSionpixelsize (R.styleable.circleimageview_border_width, default_border_width);
Mbordercolor = A.getcolor (R.styleable.circleimageview_border_color, Default_border_color);
A.recycle ();
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 Illegalar
Gumentexception (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.drawcircle (GetWidth ()/2, GetHeight ()/2, Mborderradius, mborderpaint); } @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OL
DH);
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;
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_dimensi
On, Bitmap_config);
else {bitmap = Bitmap.createbitmap (Drawable.getintrinsicwidth (), Drawable.getintrinsicheight (), bitmap_config);
} 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); 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.height (
)/(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); }
}