当前位置 : 主页 > 手机开发 > android >

如何在按钮单击时清除android中的画布

来源:互联网 收集:自由互联 发布时间:2021-06-11
import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.util.
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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 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(18f);
  paint.setColor(Color.LTGRAY);
  paint.setStyle(Paint.Style.STROKE);
  paint.setStrokeJoin(Paint.Join.ROUND);
  paint.setStrokeCap(Paint.Cap.ROUND);
}

@Override
protected void onDraw(Canvas canvas) {
    if(cc)
    {
        Bitmap back = BitmapFactory.decodeResource(getResources(), R.drawable.black_square);
        Bitmap cb = Bitmap.createScaledBitmap(back, 0, 0, false);
        canvas.drawBitmap(cb,0,0,null);
        cc = false;

    }
  canvas.drawPath(path, paint);
}

public void clearCanvas()
{
  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;
  }
}

上面的文件是我的SingleTouchEventView.Java
这是我的MainActivity.java

public class MainActivity extends Activity {

    Button reset;;
LinearLayout canvasAlphabets;
    SingleTouchEventView myView; 


   @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);


    reset = (Button)findViewById(R.id.reset_canvas);

    myView = new SingleTouchEventView(this, null);
    canvasAlphabets = (LinearLayout)findViewById(R.id.canvas_Alphabets);
    canvasAlphabets.addView(myView);


           reset.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


        }
        });

      }

  }

我的问题是我应该在重置按钮中使用什么代码来删除画布的所有内容.
请帮我
我试过实现myView.clearCanvas(),但这没有用.如果我添加此代码以重置buutons on Click它会导致FC

谢谢

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

我用上面的代码修复了它

网友评论