SoFunction
Updated on 2025-03-02

Android rounded corners ImageView class can set radians (simple code)

I won’t say much nonsense, I will just post the code to you. The specific code is as follows:

public class RoundImageView extends ImageView {
private Paint paint;
private int roundWidth = 50;
private int roundHeight = 50;
private Paint paint2;
public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public void SetRoundValue(float roundValue) {
roundWidth = (int) roundValue;
roundHeight = (int) roundValue;
}
public RoundImageView(Context context) {
super(context);
init(context, null);
}
@SuppressLint("Recycle")
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = (attrs, );
roundWidth = (.RoundAngleImageView_roundWidth, roundWidth);
roundHeight = (.RoundAngleImageView_roundHeight, roundHeight);
} else {
float density = ().getDisplayMetrics().density;
roundWidth = (int) (roundWidth * density);
roundHeight = (int) (roundHeight * density);
}
paint = new Paint();
();
(true);
(new PorterDuffXfermode(.DST_OUT));
paint2 = new Paint();
(null);
}
@Override
public void draw(Canvas canvas) {
Bitmap bitmap = (getWidth(), getHeight(), Config.ARGB_8888);
Canvas canvas2 = new Canvas(bitmap);
(canvas2);
drawLiftUp(canvas2);
drawRightUp(canvas2);
drawLiftDown(canvas2);
drawRightDown(canvas2);
(bitmap, 0, 0, paint2);
();
bitmap = null;
}
private void drawLiftUp(Canvas canvas) {
Path path = new Path();
(0, roundHeight);
(0, 0);
(roundWidth, 0);
(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90);
();
(path, paint);
}
private void drawLiftDown(Canvas canvas) {
Path path = new Path();
(0, getHeight() - roundHeight);
(0, getHeight());
(roundWidth, getHeight());
(new RectF(0, getHeight() - roundHeight * 2, 0 + roundWidth * 2, getWidth()), 90, 90);
();
(path, paint);
}
private void drawRightDown(Canvas canvas) {
Path path = new Path();
(getWidth() - roundWidth, getHeight());
(getWidth(), getHeight());
(getWidth(), getHeight() - roundHeight);
(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), 0,
90);
();
(path, paint);
}
private void drawRightUp(Canvas canvas) {
Path path = new Path();
(getWidth(), roundHeight);
(getWidth(), 0);
(getWidth() - roundWidth, 0);
(new RectF(getWidth() - roundWidth * 2, 0, getWidth(), 0 + roundHeight * 2), -90, 90);
();
(path, paint);
}
}

Okay, the editor will introduce the content about the Android rounded corner ImageView class that can set the curves here, I hope it will be helpful to you!