2012-09-28 227 views
3

我制作了一个自定义的视图,它是从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> 
+0

drawColor对我来说似乎是个好开始 – njzk2

回答

5

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

因此,如果您使用全局变量,请在您的按钮单击方法中将其设置为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();其他postInvalidate();人们应该工作。

postInvalidate()适用于当您在非UI线程上运行时。

+0

PorterDuff.Mode.CLEAR中的PorterDuff;给出了一个错误。这是什么PorterDuff。 – Tanvir

+0

我通过这个新的CustomView(CustomViewActivity.this,null).clearCanvas()从activity的onclik方法调用了method-clearCanvas();即使在调用invalidate()时,它甚至不会再调用onDraw()。 \t \t } – Tanvir

+0

你可以试试'postInvalidate()'。如果它的有效,只使用'Mode.CLEAR'。我不知道你的结构是什么样的,但我的'onDraw'在游戏循环中被调用。无效无效的事实是另一个问题。你可能会得到更好的答案,打开一个新的。 – Doomsknight