There is often a situation in Android development that is to display the pictures uploaded by the user in a circular style, but the pictures uploaded by the user can have various uncertain styles such as right angles, rounded corners, squares, etc. At this time, a custom ImageView control is used to make all the received pictures displayed in a circular style on the Android client.
public class CircleImageView extends ImageView { private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP; private static final BITMAP_CONFIG = .ARGB_8888; private static final int COLORDRAWABLE_DIMENSION = 2; // The thickness default value of the circular border. // If it is 0, there is no sky blue gradient border. private static final int DEFAULT_BORDER_WIDTH = 0; private static final int DEFAULT_BORDER_COLOR = ; 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(Context context) { super(context); init(); } public CircleImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { (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 IllegalArgumentException(( "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; } (getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint); if (mBorderWidth != 0) { (); (20, getWidth() / 2, getHeight() / 2); (getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint); (); } if (mShowFlag && mFlagText != null) { (mBorderRect, 40, 100, false, mFlagBackgroundPaint); (mFlagText, 0, (), mFlagTextBounds); (mFlagText, getWidth() / 2, (float) ((3 + ((float) ( * 5 / 18))) * getHeight() / 4 + () / 3), mFlagTextPaint); } } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { (w, h, oldw, oldh); setup(); } public int getBorderColor() { return mBorderColor; } public void setBorderColor(int borderColor) { if (borderColor == mBorderColor) { return; } mBorderColor = borderColor; (mBorderColor); invalidate(); } public int getBorderWidth() { return mBorderWidth; } /** * @param borderWidth * Round border thickness. */ public void setBorderWidth(int borderWidth) { if (borderWidth == mBorderWidth) { return; } mBorderWidth = borderWidth; setup(); } @Override public void setImageBitmap(Bitmap bm) { (bm); mBitmap = bm; setup(); } @Override public void setImageDrawable(Drawable drawable) { (drawable); mBitmap = getBitmapFromDrawable(drawable); setup(); } @Override public void setImageResource(int resId) { (resId); mBitmap = getBitmapFromDrawable(getDrawable()); setup(); } @Override public void setImageURI(Uri uri) { (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 = (COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG); } else { bitmap = ((), (), BITMAP_CONFIG); } Canvas canvas = new Canvas(bitmap); (0, 0, (), ()); (canvas); return bitmap; } catch (OutOfMemoryError e) { return null; } } private void setup() { if (!mReady) { mSetupPending = true; return; } if (mBitmap == null) { return; } mBitmapShader = new BitmapShader(mBitmap, , ); (true); (mBitmapShader); (); (true); (mBorderColor); (mBorderWidth); mBitmapHeight = (); mBitmapWidth = (); (0, 0, getWidth(), getHeight()); mBorderRadius = ((() - mBorderWidth) / 2, (() - mBorderWidth) / 2); (mBorderWidth, mBorderWidth, () - mBorderWidth, () - mBorderWidth); mDrawableRadius = (() / 2, () / 2); ( & 0x66FFFFFF); (TextPaint.ANTI_ALIAS_FLAG); (TextPaint.ANTI_ALIAS_FLAG); (); (); mFlagTextPaint .setTextSize(getResources().getDisplayMetrics().density * 18); mSweepGradient = new SweepGradient(getWidth() / 2, getHeight() / 2, new int[] { (255, 255, 255), (1, 209, 255) }, null); (mSweepGradient); updateShaderMatrix(); invalidate(); } private void updateShaderMatrix() { float scale; float dx = 0; float dy = 0; (null); if (mBitmapWidth * () > () * mBitmapHeight) { scale = () / (float) mBitmapHeight; dx = (() - mBitmapWidth * scale) * 0.5f; } else { scale = () / (float) mBitmapWidth; dy = (() - mBitmapHeight * scale) * 0.5f; } (scale, scale); ((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth); (mShaderMatrix); } public void setShowFlag(boolean show) { mShowFlag = show; invalidate(); } public void setFlagText(String text) { mFlagText = text; invalidate(); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro xmlns:app="/apk/res-auto" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > < android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.