public class PatMirrorCoverView extends View {
Paint mPaint;
private Paint mPaintLine;
private float leftX, leftY, rightX, rightY, leftX1, leftY1, rightX1, rightY1;
Path path = new Path();
public PatMirrorCoverView(Context context) {
super(context);
init();
}
/**
* * Desc:
* *
* * Author: [lianyg]
* * Date: 2018-10-31
* *
* * @param context
* * @param attrs
*
*/
public PatMirrorCoverView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
/**
* * Desc:
* *
* * Author: [lianyg]
* * Date: 2018-10-31
* *
* * @param context
* * @param attrs
* * @param defStyleAttr
*
*/
public PatMirrorCoverView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
/**
* * Desc:
* *
* * Author: [lianyg]
* * Date: 2018-10-31
* *
* * @param context
* * @param attrs
* * @param defStyleAttr
* * @param defStyleRes
*
*/
public PatMirrorCoverView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
/**
* Desc:
* <p>
* Author: [lianyg]
* Date: 2018-10-31
*/
public void init() {
mPaintLine = new Paint();
mPaintLine.setStyle(Paint.Style.FILL);
mPaintLine.setColor(Color.RED);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(1);
mPaint.setColor(getResources().getColor(R.color.pat_color_80000000));
mPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawListRect(canvas);
}
/**
* Desc:
* <p>
* Author: [lianyg]
* Date: 2018-10-31
*
* @param leftX
* @param leftY
* @param rightX
* @param rightY
* @param leftX1
* @param leftY1
* @param rightX1
* @param rightY1
* @param isTranslate
*/
public void setRect(float leftX, float leftY, float rightX, float rightY, float leftX1, float leftY1, float rightX1, float rightY1, boolean isTranslate) {
this.leftX = leftX;
this.leftY = leftY;
this.rightX = rightX;
this.rightY = rightY;
this.leftX1 = leftX1;
this.leftY1 = leftY1;
this.rightX1 = rightX1;
this.rightY1 = rightY1;
if (isTranslate) {
mPaint.setColor(Color.parseColor("#00000000"));
invalidate();
} else {
mPaint.setColor(Color.parseColor("#80000000"));
invalidate();
}
}
/**
*
*/
List<PatMirrorCoverBean> listRect;
/**
* Desc:
* <p>
* Author: [lianyg]
* Date: 2018-10-31
*
* @param listRect
*/
public void setRect(List<PatMirrorCoverBean> listRect) {
this.listRect = listRect;
postInvalidate();
}
/**
* Desc:绘制不规则矩形
* <p>
* Author: [lianyg]
* Date: 2018-10-31
*/
private void drawListRect(Canvas canvas) {
for (int i = 0; i < listRect.size(); i++) {
PatMirrorCoverBean patMirrorCoverBean = listRect.get(i);
if (patMirrorCoverBean.isTranslate()) {
mPaint.setColor(getResources().getColor(R.color.pat_color_effect_no));
} else {
mPaint.setColor(getResources().getColor(R.color.color_80000000));
}
float[] rect = patMirrorCoverBean.getRect();
path.reset();//这里必须要reset。否则会出现每次渲染后颜色都是第一次初始化的数据
path.moveTo(rect[0], rect[1]);
path.lineTo(rect[2], rect[3]);
path.lineTo(rect[6], rect[7]);
path.lineTo(rect[4], rect[5]);
path.close();
canvas.drawPath(path, mPaint);
}
}
}
当前绘制效果比较简单。app内部效果,是点击按钮的时候动态绘制指定坐标显示对应的区域,目前是可以显示不规则的四边形,如果需要修改其他形状,可以直接在drawListRect函数进行path.lineTo的添加等,需要记住的是path.lineTo的顺序执行。
使用方法直接,调用setRect(List<PatMirrorCoverBean>);
public class PatMirrorCoverBean implements Serializable {
private float[] rect; //坐标系
public float[] getRect() {
return rect;
}
public void setRect(float[] rect) {
this.rect = rect;
}
public boolean isTranslate() {
return isTranslate;
}
public void setTranslate(boolean translate) {
isTranslate = translate;
}
private boolean isTranslate;//颜色是否透明
}
好了。有啥问题可以直接留意与我沟通 611540954723_.pic.jpg 621540954724_.pic.jpg
网友评论