鉴于下面的代码,我将如何清除我的画布?

时间:2011-12-07 03:26:37

标签: android ontouchevent

这是我的程序代码:

SingleTouchEvent是我班级的名字

我厌倦了使用canvas.onDraw,但它只改变了背景。我想要的是,清除另一幅画的画布。

public class SingleTouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();


public SingleTouchEventView(Context context, AttributeSet attrs) {
    super(context, attrs);

    paint.setAntiAlias(true);
    paint.setStrokeWidth(6f);
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);


}

它只会改变屏幕的颜色。它不会删除画布的先前内容。

@Override
protected void onDraw(Canvas canvas) {


    canvas.drawColor(Color.BLACK);
    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:
        path.lineTo(eventX, eventY);
        break;
    case MotionEvent.ACTION_UP:
        // nothing to do
        break;
    default:
        return false;
    }


    // Schedules a repaint.
invalidate();


return true;
}



}

2 个答案:

答案 0 :(得分:0)

调用invalidate();在onDraw函数结束时,它将继续调用onDraw,之后进行如下检查:

if(isClear)
{
    invalidate();//It'll draw nothing.
}
else
{
    do your drawing..
}

或点击此链接: How to clear finger paint?

答案 1 :(得分:0)

试一试:

canvas.drawColor(0, Mode.CLEAR);