简体   繁体   English

如何清除使用按钮单击时使用自定义视图创建的画布

[英]how to clear canvas created using custom view on button click

I have created an canvas using custom view and it is working fine. 我已经使用自定义视图创建了一个画布,并且工作正常。 I am able to write draw on it using mouse. 我可以使用鼠标在上面写画。 But i want to clear the painting drawn on canvas using a button click. 但我想清除使用按钮单击在画布上绘制的画。 I have tried my best but not able to do that. 我已尽力而为,但未能做到。 plz help 请帮助

THIS MY MAIN ACTIVITY 我的主要活动

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 这是我的SingleTouchEventView类别

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

AND THIS IS MY LAYOUT FILE 这是我的布局文件

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

try this code 试试这个代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM