清除画布图像或在android中绘画

时间:2013-12-11 07:45:20

标签: android android-canvas

我在这里搜索了很多找到的答案,但没有人像1一样工作。Deleting a path from a canvas - Android 2。How to erase path area from canvas (Android)

我的编码是:

mClear.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            drawView.clear();
            }
    });

public void init() {
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.GREEN);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(3);
}

class DrawingView extends View {
    Path path;

    Bitmap mBitmap;

    Canvas mCanvas;

    public DrawingView(Context context) {
        super(context);
        path = new Path();
        mBitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        this.setBackgroundColor(Color.WHITE);
    }

    public void clear() {
         path.reset();
         invalidate();
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mCanvas.drawPath(path, mPaint);
        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());
        }
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, mPaint);
    }
}

我尝试清除其他一些如下:

mBitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);

我需要一些帮助..请给出一些删除想法

1 个答案:

答案 0 :(得分:1)

在你的尝试中尝试这样的事情 清算方法如:

((YourDrawingClass)YourView).clear();