Android 清除画布区域

Android 清除画布区域,android,Android,我已经做了一个自定义视图,它是从xml布局中引用的。我添加了一个清除视图的按钮。现在我想在单击后清除画布区域。我在xml布局文件中添加了一个onClick事件?我刚刚添加了几部分代码。(这不是在清理任何东西)。我已经按如下顺序添加了我的活动、视图和布局文件 public class CustomViewActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) {

我已经做了一个自定义视图,它是从xml布局中引用的。我添加了一个清除视图的按钮。现在我想在单击后清除画布区域。我在xml布局文件中添加了一个onClick事件?我刚刚添加了几部分代码。(这不是在清理任何东西)。我已经按如下顺序添加了我的活动、视图和布局文件

public class CustomViewActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    public void clearLine(View v) {

    new CustomView(CustomViewActivity.this, null).clearCanvas();        
  } 

}

public class CustomView extends View {

    private Paint paint = new Paint();
      private Path path = new Path();
      public Boolean clearCanvas = false;

      public CustomView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);         
        }

    public CustomView(Context context,AttributeSet attrs ) {
        super(context,attrs);
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE);
        paint.setTextSize(20);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(5f);
    }

    protected void onDraw(Canvas canvas) {
    if(clearCanvas)
        {  // Choose the colour you want to clear with.
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            //canvas.drawColor(0, Mode.CLEAR);
            clearCanvas = false;            
        }

        super.onDraw(canvas);
        canvas.drawText("Hello World", 5, 30, paint);
        canvas.drawPath(path, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

    //int action = event.getAction() & MotionEvent.ACTION_MASK;

       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;

    }
    public void clearCanvas(){

            clearCanvas = true;
            postInvalidate();
            //canvas.drawColor(0, Mode.CLEAR);

        }

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


           <com.example.CustomViewEvent.CustomView
               android:id="@+id/customView"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" />

           <Button
               android:id="@+id/button1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true"
               android:layout_centerHorizontal="true"
               android:layout_marginBottom="28dp"
               android:onClick="clearLine"
               android:text="CLEAR" />

</RelativeLayout>
公共类CustomViewActivity扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
公共空地净空线(视图五){
新建CustomView(CustomViewActivity.this,null).clearCanvas();
} 
}
公共类CustomView扩展了视图{
私人油漆=新油漆();
私有路径路径=新路径();
公共布尔clearCanvas=false;
公共自定义视图(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
}
公共自定义视图(上下文、属性集属性){
超级(上下文,attrs);
paint.setAntiAlias(真);
油漆。设置颜色(颜色。蓝色);
油漆.尺寸(20);
绘制.设置样式(绘制.样式.笔划);
绘制.设置行程连接(绘制.连接.圆形);
油漆。设置行程宽度(5f);
}
受保护的void onDraw(画布){
如果(clearCanvas)
{//选择要清除的颜色。
canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR);
//canvas.drawColor(0,Mode.CLEAR);
clearCanvas=false;
}
super.onDraw(帆布);
画布.drawText(“Hello World”,5,30,paint);
画布.绘制路径(路径,绘制);
}
@凌驾
公共布尔onTouchEvent(运动事件){
//int action=event.getAction()&MotionEvent.action\u掩码;
float eventX=event.getX();
float eventY=event.getY();
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
path.moveTo(eventX,eventY);
返回true;
case MotionEvent.ACTION\u移动:
lineTo(eventX,eventY);
打破
case MotionEvent.ACTION\u UP:
//无事可做
打破
违约:
返回false;
}
//安排重画。
使无效();
返回true;
}
公共无效clearCanvas(){
clearCanvas=true;
后验证();
//canvas.drawColor(0,Mode.CLEAR);
}
}

您需要做的是在onDraw方法中访问画布

因此,如果使用全局变量,请在button click方法中,将其设置为true。在OnDraw中,您可以检查其状态并在必要时清除画布。(然后将其设置回false,这样它不会每次都这样做)

有关用法,请参见下面的代码

public Boolean clearCanvas = false;

protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(clearCanvas)
        {  // Choose the colour you want to clear with.
            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
            clearCanvas = false;
        }
        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;

    }
// this is the method which will be invoked from main activity class for clearing whatever //is in the view/canvas 
         public void clearCanvas(){

            //canvas.drawColor(0, Mode.CLEAR);

            clearCanvas = true;
            invalidate();
        }

}
编辑: 看看你的新代码,我发现了一些问题

我认为这是因为你没有澄清正确的观点

首先,获取现有视图的实例。然后你可以清除它。而不是错误的不存在的实例

 CustomView cv = (CustomView)findViewById(R.id.customView); 
 cv.clearCanvas();   
尝试
invalidate()
else
postInvalidate()一个人应该工作


postInvalidate()
适用于在非UI线程上运行的情况。

drawColor似乎是在PorterDuff.Mode.CLEAR中使用mePorterDuff的良好开端);这是一个错误。这是什么PorterDuff。我通过这个-new CustomView(CustomViewActivity.this,null).clearCanvas()从活动的onclik方法调用了方法-clearCanvas();但它甚至不会在调用invalidate()时再次调用onDraw()。}您可以尝试
postInvalidate()
。仅使用
模式。如果有效,请清除
。我不确定你的结构是什么样的,但是我的
onDraw
是在一个游戏循环中调用的。无效不起作用的事实是另一个问题。打开一个新的,你可能会得到更好的答案。请你再看看上面的代码。我编辑了我的帖子/请看上面。非常抱歉回复太晚。(我病了好几天)。现在又回去工作了。谢谢你的答复。修改后的代码并没有清除画布区域,而是使画布的背景变黑,而绘制的线条仍然存在。(我的默认背景是白色)。塔克斯