Circleimageview to achieve circular head code sharing, for your reference, the specific content as follows
First, create a property file (Attrs.xml)
Specific actions:
1, under the project Values file to create a new property file, file name is attrs:new->xml->values XML file:
2, supplemental Attrs.xml Code:
<?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>
Ii. Add implementation class (Circleimageview.java)
Circleimageview.java (can be directly quoted, do not understand and nothing):
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.util.AttributeSet;
Import Android.widget.ImageView;
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 = 1;
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);
Public Circleimageview (context, AttributeSet attrs) {This (context, attrs, 0);
Public Circleimageview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Super.setscaletype (Scale_type);
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 ();
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 Illegalargumente
Xception (String.Format ("ScaleType%s not supported.", ScaleType));
}} @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, 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;
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 ();
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_dimension, 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); }
}
Third, the use of Circleimageview
Specific actions:
1, first copy the picture you want to the res->drawable under the project:
2, directly in the layout file reference:
<com.example.suqh.drop.circleimageview
android:layout_width= "match_parent"
android:layout_height= " Match_parent "
android:src=" @drawable/img3 "/>
3, compile the program:
Referencing the new class will have the following problem, just click Build to compile the program:
4, or not. Recompile the program:
5, the effect chart:
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.