0

我已經創建了一個使用自定義視圖的畫布,它工作正常。我可以用鼠標在它上面畫畫。但我想清除使用按鈕單擊在畫布上繪製的繪畫。我盡力而爲,但無法做到這一點。 plz幫助如何清除使用自定義視圖創建的畫布按鈕單擊

這是我的主要活動

package com.example.paint; 
public class MainActivity extends Activity 
{ 

    LinearLayout canvasAlphabets; 
    SingleTouchEventView myView; 

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

     Button btnClear = (Button)findViewById(R.id.btnSearchNameWise); 
     myView = new SingleTouchEventView(this, null); 
     canvasAlphabets = (LinearLayout)findViewById(R.id.First); 
     canvasAlphabets.addView(myView); 

     btnClear.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) 
      { 
       myView.clearCanvas(); 
       Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show(); 

      } 
     });  
     } 

} 

THIS IS MY SingleTouchEventView CLASS

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


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

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

     @Override 
     protected void onDraw(Canvas canvas) 
     { 
      if(cc) 
      { 

       path = new Path(); 
      Paint clearPaint = new Paint(); 
      clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
      canvas.drawRect(0, 0, 0, 0, clearPaint); 

      cc = false; 

      } 
      else 

     canvas.drawPath(path, paint); 

     } 

     public void clearCanvas() 
     { 
      Toast.makeText(getContext(), "hello in canvas", Toast.LENGTH_LONG).show(); 
     cc =true; 
     invalidate(); 
     } 

     @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; 
     } 
    } 

,這是我的佈局文件

<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" > 

    <LinearLayout 
     android:id="@+id/First" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="5dip" 
     android:paddingTop="10dip" 
     android:orientation="vertical" 
     android:weightSum="1" 
     android:background="#aabbcc"> 


     <com.example.paint.SingleTouchEventView 
      android:id="@+id/cusView" 
      android:layout_width="match_parent" 
     android:layout_height="wrap_content" 

     /> 


     </LinearLayout> 

    <LinearLayout 
     android:id="@+id/Second" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:orientation="vertical" 
     android:paddingBottom="50dip" 
     android:weightSum="1" 
     android:background="#ffaacc" > 

     <Button 
      android:id="@+id/btnSearchNameWise" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingLeft="3dip" 
      android:paddingRight="3dip" 
      android:text="Search Name Wise" /> 

      </LinearLayout> 

</RelativeLayout> 

回答

1

試試這個代碼

Paint clear = new Paint(); 
clear.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
canvas.drawRect(0, 0, 0, 0, clear); 
+0

plz告訴在哪裏寫這段代碼,因爲我很困惑 – pks

+0

在btn_clear中你必須寫 –