擦除画布上的位图内容

时间:2016-06-22 13:34:33

标签: java canvas graphics bitmap eraser

'我想删除画布上绘制的图像内容,如手指橡皮擦但drawCircle没有删除。这是我的代码 “

public class PaintView extends View {
Canvas c1;
Paint mPaintt;
Bitmap mBitmap;
Matrix mMatrix;
RectF mSrcRectF;
RectF mDestRectF;
boolean mPause;
Bitmap bit;

Path path;
int X = -100;
int Y = -100;
Paint mPaint;
Bitmap mBitmaps;


public PaintView(Context context, AttributeSet attributeSet){
    super(context,attributeSet);


    mPaintt=new Paint();
    mMatrix = new Matrix();
    mSrcRectF = new RectF();
    mDestRectF = new RectF();
    mPause = false;



    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
    mPaint.setAlpha(0);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    mPaint.setAntiAlias(true);
    mPaint.setColor(Color.TRANSPARENT);
    mPaint.setMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.NORMAL));





}

public void addBitmap(Bitmap bitmap){
    mBitmap = bitmap;
}

public Bitmap getBitmap(){
    return mBitmap;
}





@Override
public boolean onTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {

        case MotionEvent.ACTION_DOWN: {

            X = (int) ev.getX();
            Y = (int) ev.getY();
            invalidate();

            break;
        }

        case MotionEvent.ACTION_MOVE: {

            X = (int) ev.getX();
            Y = (int) ev.getY();
            invalidate();
            break;

        }

        case MotionEvent.ACTION_UP:

            break;

    }
    return true;
}




@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(!mPause){

        if(mBitmap!=null){
            canvas.drawColor(0, PorterDuff.Mode.CLEAR);
            //mPaint.setColor(Color.TRANSPARENT);
            // Setting size of Source Rect
            mSrcRectF.set(0, 0,mBitmap.getWidth(),mBitmap.getHeight());

            // Setting size of Destination Rect
            mDestRectF.set(0, 0, getWidth(), getHeight());

            // Scaling the bitmap to fit the PaintView
            mMatrix.setRectToRect( mSrcRectF , mDestRectF,        Matrix.ScaleToFit.CENTER);

            canvas.drawBitmap(mBitmap, mMatrix,mPaintt);
            canvas.drawCircle(X,Y,30,mPaint);

        }
        }
        // Redraw the canvas
        invalidate();
    }

//暂停或恢复onDraw方法     public void pause(boolean pause){         mPause =暂停;     } }

0 个答案:

没有答案