public class RoundImageView extends AppCompatImageView {
/**
* 边框颜色 默认值
*/
private int mBorderColor = 0;
private Paint paint;
private Paint paint2;
private int roundWidth = 15;
private int roundHeight = 15;
private Paint mBorderPaint;
public RoundImageView(Context context) {
super(context);
initData(context, null);
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
initData(context, attrs);
}
public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData(context, attrs);
}
private void initData(Context context, AttributeSet attrs) {
if (null != attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
if (null != typedArray) {
// 读取边框颜色
// Read the border color
mBorderColor = typedArray.getColor(R.styleable.RoundImageView_borderColor, mBorderColor);
}
}
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) {
try {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas2 = new Canvas(bitmap);
super.draw(canvas2);
roundWidth = getWidth() / 2;
roundHeight = getHeight() / 2;
drawLiftUp(canvas2);
drawRightUp(canvas2);
drawLiftDown(canvas2);
drawRightDown(canvas2);
canvas.drawBitmap(bitmap, 0, 0, paint2);
paintframe(canvas);
} catch (Exception e) {
e.printStackTrace();
}
}
private void paintframe(Canvas canvas) {
//短的二分之一作为半径
int radius = getHeight() > getWidth() ? roundWidth : roundHeight;
//给图形加边框
mBorderPaint = new Paint();
//抗锯齿
mBorderPaint.setAntiAlias(true);
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setStrokeWidth(2);
mBorderPaint.setColor(mBorderColor);
// canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius - 1, mBorderPaint);
float fNum = 2.5f;
RectF rectF = new RectF();
rectF.left = fNum;
rectF.top = fNum;
rectF.right = roundWidth * 2 - fNum;
rectF.bottom = roundHeight * 2 - fNum;
canvas.drawRoundRect(rectF, 20, 20, mBorderPaint);
}
/**
* 设置边框颜色
* Set the border color
* Exposure method
*
* @param borderColor
*/
public void setBorderColor(int borderColor) {
mBorderColor = borderColor;
this.invalidate();
}
//startAngle是开始度数
//sweepAngle指的是旋转的度数 - 正数(顺时针旋转)、负数(逆时针旋转)
private void drawLiftUp(Canvas canvas) {
//抗锯齿
paint.setAntiAlias(true);
Utils.LogD("----------左上----------");
Utils.LogD("roundWidth = " + roundWidth);
Utils.LogD("roundHeight = " + roundHeight);
Utils.LogD("getWidth() = " + getWidth());
Utils.LogD("getHeight() = " + getHeight());
Path path = new Path();
path.moveTo(0, roundHeight);
path.lineTo(0, 0);
path.lineTo(roundWidth, 0);
path.arcTo(new RectF(0, 0, roundWidth, roundHeight), -90, -90);
path.close();
canvas.drawPath(path, paint);
}
private void drawLiftDown(Canvas canvas) {
Utils.LogD("----------左下----------");
Utils.LogD("roundWidth = " + roundWidth);
Utils.LogD("roundHeight = " + roundHeight);
Utils.LogD("getWidth() = " + getWidth());
Utils.LogD("getHeight() = " + getHeight());
Path path = new Path();
path.moveTo(0, getHeight() - roundHeight);
path.lineTo(0, getHeight());
path.lineTo(roundWidth, getHeight());
path.arcTo(new RectF(0, getHeight() - roundHeight, 0 + roundWidth, getHeight()), 90, 90);
path.close();
canvas.drawPath(path, paint);
}
private void drawRightDown(Canvas canvas) {
Utils.LogD("----------右下----------");
Utils.LogD("roundWidth = " + roundWidth);
Utils.LogD("roundHeight = " + roundHeight);
Utils.LogD("getWidth() = " + getWidth());
Utils.LogD("getHeight() = " + getHeight());
Path path = new Path();
path.moveTo(getWidth() - roundWidth, getHeight());
path.lineTo(getWidth(), getHeight());
path.lineTo(getWidth(), getHeight() - roundHeight);
path.arcTo(new RectF(getWidth() - roundWidth, getHeight() - roundHeight, getWidth(), getHeight()), 0,
90);
path.close();
canvas.drawPath(path, paint);
}
private void drawRightUp(Canvas canvas) {
Utils.LogD("----------右上----------");
Utils.LogD("roundWidth = " + roundWidth);
Utils.LogD("roundHeight = " + roundHeight);
Utils.LogD("getWidth() = " + getWidth());
Utils.LogD("getHeight() = " + getHeight());
Path path = new Path();
path.moveTo(getWidth(), roundHeight);
path.lineTo(getWidth(), 0);
path.lineTo(getWidth() - roundWidth, 0);
path.arcTo(new RectF(getWidth() - roundWidth, 0, getWidth(), 0 + roundHeight), -90, 90);
path.close();
canvas.drawPath(path, paint);
}
}
arrts文件中
<declare-styleable name="RoundImageView">
<attr name="borderColor" format="color" /> <!--边框颜色-->
</declare-styleable>
使用
<com.example.simpledemo.demo2.RoundImageView
android:id="@+id/iv_tab_icon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher"
app:borderColor="@color/colorPrimary"
tools:ignore="MissingConstraints" />
画的4个角是带弧度的,在不相交的地方绘制目标图,实现了图片四角弧形效果
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));