/**
* @ClassName: RoundAngleImageView
* @Description:
* <RoundCornerImageView
* android:layout_width="300dp"
* android:layout_height="187dp"
* android:layout_gravity="center_horizontal"
* android:layout_marginTop="50dp"
* android:src="@mipmap/timg"
* app:leftBottom="true" //单独设置左下角为圆角
* app:leftTop="true" // 单独设置左上角为圆角
* app:rightBottom="true" // 单独设置右下角为圆角
* app:rightTop="true" // 单独设置右上角为圆角
* app:roundHeight="3dp" // 设置圆角的高度圆角
* app:roundWidth="3dp" // 设置圆角的宽度圆角
* app:round_angle="true" //设置所有角度都是圆角,设置了之后单独设置角度失效
* />
*
* @CreateDate: 2020/11/17 下午4:31
*/
public class RoundCornerImageView extends ImageView {
private Paint paint;
/**
* 个人理解是
* <p>
* 这两个都是画圆的半径
*/
private int roundWidth = 20;
private int roundHeight = 20;
//左上角圆角
private boolean leftTop = false;
//右上角圆角
private boolean rightTop = false;
//右下角圆角
private boolean rightBottom = false;
//左下角圆角
private boolean leftBottom = false;
//四个角都是圆角
private boolean roundAngle = false;
private Paint paint2;
public RoundCornerImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public RoundCornerImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public RoundCornerImageView(Context context) {
super(context);
init(context, null);
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerImageView);
roundWidth = a.getDimensionPixelSize(R.styleable.RoundCornerImageView_roundAngleWidth, roundWidth);
roundHeight = a.getDimensionPixelSize(R.styleable.RoundCornerImageView_roundAngleHeight, roundHeight);
leftTop = a.getBoolean(R.styleable.RoundCornerImageView_leftTop, false);
rightTop = a.getBoolean(R.styleable.RoundCornerImageView_rightTop, false);
rightBottom = a.getBoolean(R.styleable.RoundCornerImageView_rightBottom, false);
leftBottom = a.getBoolean(R.styleable.RoundCornerImageView_leftBottom, false);
roundAngle = a.getBoolean(R.styleable.RoundCornerImageView_round_angle, false);
a.recycle();
} else {
//给予默认值
float density = context.getResources().getDisplayMetrics().density;
roundWidth = (int) (roundWidth * density);
roundHeight = (int) (roundHeight * density);
leftTop = false;
rightTop = false;
rightBottom = false;
leftBottom = false;
roundAngle = false;
}
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
paint2 = new Paint();
paint2.setXfermode(null);
}
@Override
public void draw(Canvas canvas) {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas2 = new Canvas(bitmap);
super.draw(canvas2);
if (roundAngle) { //四个角都是圆角
drawLiftUp(canvas2);
drawRightUp(canvas2);
drawRightDown(canvas2);
drawLiftDown(canvas2);
} else {
if (leftTop) { //绘制左上角为圆角
drawLiftUp(canvas2);
}
if (rightTop) {//设置右上角为圆角
drawRightUp(canvas2);
}
if (rightBottom) {//设置右下角为圆角
drawRightDown(canvas2);
}
if (leftBottom) {//设置左下角为圆角
drawLiftDown(canvas2);
}
}
canvas.drawBitmap(bitmap, 0, 0, paint2);
bitmap.recycle();
}
//左上角
private void drawLiftUp(Canvas canvas) {
Path path = new Path();
path.moveTo(0, roundHeight);
path.lineTo(0, 0);
path.lineTo(roundWidth, 0);
path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90);
path.close();
canvas.drawPath(path, paint);
}
//左下角
private void drawLiftDown(Canvas canvas) {
Path path = new Path();
path.moveTo(0, getHeight() - roundHeight);
path.lineTo(0, getHeight());
path.lineTo(roundWidth, getHeight());
path.arcTo(new RectF(0, getHeight() - roundHeight * 2, roundWidth * 2, getHeight()), 90, 90);
path.close();
canvas.drawPath(path, paint);
}
//右下角
private void drawRightDown(Canvas canvas) {
Path path = new Path();
path.moveTo(getWidth() - roundWidth, getHeight());
path.lineTo(getWidth(), getHeight());
path.lineTo(getWidth(), getHeight() - roundHeight);
path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), -0, 90);
path.close();
canvas.drawPath(path, paint);
}
//右上角
private void drawRightUp(Canvas canvas) {
Path path = new Path();
path.moveTo(getWidth(), roundHeight);
path.lineTo(getWidth(), 0);
path.lineTo(getWidth() - roundWidth, 0);
path.arcTo(new RectF(getWidth() - roundWidth * 2, 0, getWidth(), 0 + roundHeight * 2), -90, 90);
path.close();
canvas.drawPath(path, paint);
}
}
在文件夹values下新建attrs.xml 文件,设置自定义属性:
<declare-styleable name="RoundCornerImageView">
<attr name="roundAngleWidth" format="dimension" />
<attr name="roundAngleHeight" format="dimension" />
<attr name="leftTop" format="boolean" />
<attr name="rightTop" format="boolean" />
<attr name="rightBottom" format="boolean" />
<attr name="leftBottom" format="boolean" />
<attr name="round_angle" format="boolean" />
</declare-styleable>