【发布时间】:2020-03-22 17:20:26
【问题描述】:
我正在画布上正确绘图并将其保存到位图中。 但是,我想通过单击按钮将画布重置为白色。
这是我的代码:
public class Canvas extends View {
Paint paint;
Path path;
boolean cc = false;
public Canvas(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
path = new Path();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5f);
}
@Override
protected void onDraw(android.graphics.Canvas canvas) {
super.onDraw(canvas);
if (!cc) {
canvas.drawPath(path, paint);
}
else {
canvas.drawColor(Color.WHITE);
cc = false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float xPos = event.getX();
float yPos = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(xPos, yPos);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(xPos, yPos);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
invalidate();
return true;
}
public void clear() {
cc = true;
invalidate();
}
我的 clear() 函数将 cc 设置为“true”,然后 invalidate() 调用 onDraw() 函数。但似乎在 onDraw() 内部无法识别“cc”,或者它内部始终具有相同的值。 我尝试了 path.reset() 没有结果。
调用 clear() 不会返回任何错误。
【问题讨论】: