我正在使用这段代码让用户用手指画线:
public class DrawingView extends View {
private Paint paint;
private Path path;
public DrawingView(Context context , AttributeSet attrs) {
super(context, attrs);
this.paint = new Paint();
this.paint.setAntiAlias(true);
this.paint.setColor(Color.BLACK);
this.paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(5f);
this.path = new Path();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
path.lineTo(eventX, eventY);
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
public void clear() {
path.reset();
invalidate();
}
public void setPaintColor(int color) {
paint.setColor(color);
}
public int getCurrentPaintColor() {
return paint.getColor();
}
}
使用方法 setPaintColor() 我正在改变油漆的颜色。但是当我改变颜色时,整个绘图都会改变(甚至是我之前画的线条)。如何更改油漆的颜色并使以前的图纸保持不变?我尝试创建新路径,但之前的绘图消失了。