Android implements circular images
Not to mention nonsense. First, go:
Layout file:
This com. example. circleview. CircleImageView is a custom control.
Implementation Method JAVA code
Package com. example. circleview; 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; im Port android. graphics. drawable. colorDrawable; import android. graphics. drawable. drawable; import android. util. attributeSet; import android. widget. imageView; public class CircleImageView extends ImageView {/*** image size and quality */private static final int COLORDRAWABLE_DIMENSION = 1; private static final Bitmap. config BITMAP_CONFIG = Bitmap. config. ARGB_8888;/*** circular image filling type */private static final ScaleType SCALE_TY PE = ScaleType. CENTER_CROP;/*** default border size and Color */private static final int DEFAULT_BORDER_WIDTH = 0; private static final int DEFAULT_BORDER_COLOR = Color. WHITE;/*** actual border size and color */private int mBorderWidth; private int mBorderColor;/*** Image Rendering Method */private BitmapShader mBitmapShader; private Paint mBitmapPaint = new Paint (); private Paint mBorderPaint = new Paint ();/*** the image to be drawn and its own width and height */private Bitmap mBitmap; Private int mBitmapHeight; private int mBitmapWidth; private RectF mBorderRect = new RectF (); private float mBorderRadius; private RectF mDrawableRect = new RectF (); private float mDrawableRadius; private Matrix mShaderMatrix = new Matrix (); private boolean mReady; private boolean mSetupPending; public CircleImageView (Context context) {super (context);} public CircleImageView (Context context, AttributeSet Ttrs) {this (context, attrs, 0);} public CircleImageView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); super. setScaleType (SCALE_TYPE); TypedArray arr = context. obtainStyledAttributes (attrs, R. styleable. circleImageView, defStyle, 0); mBorderWidth = arr. getDimensionPixelSize (R. styleable. circleImageView_border_width, DEFAULT_BORDER_WIDTH); mBorderColor = arr. getColor (R. Styleable. circleImageView_border_color, DEFAULT_BORDER_COLOR); arr. recycle (); mReady = true; if (mSetupPending) {setup (); mSetupPending = false ;}@ Overridepublic ScaleType getScaleType () {return SCALE_TYPE ;} @ Overridepublic void setScaleType (ScaleType scaleType) {if (scaleType! = SCALE_TYPE) {throw new IllegalArgumentException (String. format (ScaleType % s not supported ., scaleType) ;}@ Overrideprotected void onDraw (Canvas canvas) {if (getDrawable () = null) {return;} canvas. drawCircle (getWidth ()/2, getHeight ()/2, mDrawableRadius, mBitmapPaint); canvas. drawCircle (getWidth ()/2, getHeight ()/2, mBorderRadius, mBorderPaint);} @ Overrideprotected void onSizeChanged (int w, int h, Int oldw, int oldh) {super. onSizeChanged (w, h, oldw, oldh); setup () ;}@ Overridepublic void setImageBitmap (Bitmap bm) {super. setImageBitmap (bm); mBitmap = bm; setup () ;}@ Overridepublic void setImageDrawable (Drawable drawable) {super. setImageDrawable (drawable); mBitmap = getBitmapFromDrawable (drawable); setup () ;}@ Overridepublic void setImageResource (int resId) {super. setImageResource (resId); mBitmap = get BitmapFromDrawable (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 ;}} public void setBorderColor (int borderColor) {if (borderColor = mBorderColor) {return ;} mBorderColor = borderColor; mBorderPaint. setC Olor (mBorderColor); invalidate ();} public int getBorderWidth () {return mBorderWidth;} public void setBorderWidth (int borderWidth) {if (borderWidth = mBorderWidth) {return ;} mBorderWidth = borderWidth; setup ();} 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); // whether the image has a sawtooth mBitmapPaint. setShader (mBitmapShader); mBitmapHeight = mBitmap. getHeight (); mBitmapWidth = mBitmap. getWidth (); mBorderPaint. setStyle (Paint. style. STROKE); mBorderPaint. setAntiAlias (true); mBorderPaint. setColor (mBorderColor); mBorderPaint. setStrokeWidth (mBorderWidth); 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 );}}