[英]Clear canvas in button click
I used following code to draw. 我使用以下代码进行绘制。 I want to clear the previously drawn lines if the clear button is clicked. 如果要单击清除按钮,我想清除以前绘制的线条。
public class MainActivity extends Activity {
private ArrayList<Path> _graphics = new ArrayList<Path>();
private Paint mPaint;
Activity activity;
View mView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
mView = new DrawingView(this);
activity.addContentView(mView, new LayoutParams(500,
LinearLayout.LayoutParams.WRAP_CONTENT));
init();
}
public void clear(View v) {
new DrawingView(activity).clearView();
}
private void init() {
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(0xFFFFFF00);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
}
class DrawingView extends View {
private Path path;
public DrawingView(Context context) {
super(context);
path = new Path();
this.setBackgroundColor(Color.BLACK);
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
path.lineTo(event.getX(), event.getY());
} else if (event.getAction() == MotionEvent.ACTION_UP) {
path.lineTo(event.getX(), event.getY());
_graphics.add(path);
}
invalidate();
return true;
}
@Override
public void onDraw(Canvas canvas) {
for (Path path : _graphics) {
canvas.drawPath(path, mPaint);
}
}
public void clearView() {
path = new Path();
invalidate();
}
}
}
Thank you. 谢谢。
import android.graphics.Color;
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT);
for (Path path : _graphics) {
canvas.drawPath(path, mPaint);
}
}
or 要么
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawPaint(mPaint);
mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC));
for (Path path : _graphics) {
canvas.drawPath(path, mPaint);
}
}
See this Doc for more info. 有关更多信息,请参阅此文档。
Your clear
method is wrong. 您的clear
方法是错误的。
public void clear(View v) {
((DrawingView)mView).clearView();
}
Add the below in your Drawing view 在您的工程图视图中添加以下内容
Bitmap mBitmap;
Paint mPaint;
Canvas mCanvas;
int width,height;
public void clear()
{
_graphics.removeAll(_graphics);
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
path = new Path();
invalidate();
}
Also add the below 同时添加以下内容
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
In your onDraw add the below 在您的onDraw中添加以下内容
canvas.drawBitmap(mBitmap, 0, 0, null);
To clear on button click 要清除按钮,请单击
mView.clear();
This code works for me 该代码对我有用
clearbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
mView.invalidate();
}
});
this method work for me, 这种方法对我有用
public void Clear()
{
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
invalidate();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.