![](/img/trans.png)
[英]How can I clear/reset the canvas with a counter or countdown in Processing?
[英]How can I clear the canvas screen?
这是我的主要活动代码
package com.example.dell_7560.experiment6;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TouchScreen(this,null));
}
}
这是触摸屏代码
package com.example.dell_7560.experiment6;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class TouchScreen extends View {
private Paint paint=new Paint();
private Path path=new Path();
public TouchScreen(Context context, AttributeSet attributeSet) {
super(context,attributeSet);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
protected void onDraw(Canvas canvas)
{
canvas.drawPath(path,paint);
}
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_UP:
break;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX,eventY);
break;
default:
return false;
}
invalidate();
return true;
}
}
现在,该应用程序的工作方式如下:触摸屏幕。 拖动手指(它会产生红灯)触摸屏幕,停止创建红线。 直到这里真酷。
我的疑问是,如何清除ACTION_UP事件中的内容? 即,在不触摸屏幕的情况下,屏幕应与新屏幕一样好。
您需要做的是告诉视图在ActionUp上无效(您可以执行此操作)。 然后它将自动再次调用onDraw。 诀窍就是不要画任何东西。 您需要做的只是清除路径或设置一个变量,该变量将导致绘制例程无法绘制。 无论如何,背景始终会清除,因此您需要做的就是在无效之后不绘制路径。
正如@Mike M所说,
path.reset()
在ACTION_UP
情况下有效当我举起手指/触摸时,它将清除屏幕,即画布。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.