按钮单击清除画布

时间:2013-06-11 11:31:43

标签: android android-canvas

我使用以下代码绘制。如果单击清除按钮,我想清除以前绘制的线条。

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

谢谢。

4 个答案:

答案 0 :(得分:2)

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

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

您的clear方法错误。

public void clear(View v) {

    ((DrawingView)mView).clearView();
}

答案 1 :(得分:1)

在“绘图”视图中添加以下内容

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

同时添加以下内容

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

在你的onDraw中添加以下

    canvas.drawBitmap(mBitmap, 0, 0, null);

要清除按钮,请单击

 mView.clear();  

答案 2 :(得分:0)

此代码适用于我

clearbtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        mBitmap.eraseColor(Color.TRANSPARENT);
        mPath.reset();
        mView.invalidate();
        }
        });

答案 3 :(得分:0)

这种方法对我有用,

public void Clear()
{
mBitmap.eraseColor(Color.TRANSPARENT);
mPath.reset();
invalidate();
}