如何用手指在图像上擦除油漆?

时间:2011-08-25 05:02:43

标签: android paint

任何身体都可以告诉我擦除图像上的油漆,在我的应用程序中我准备了手指画在图像上,如果我想擦除它的颜色,在图像上获得黑色而不是擦除图像。我的代码是

    public class MyView extends View {
    int bh = originalBitmap.getHeight();
    int bw = originalBitmap.getWidth();
    public MyView(Context c)  {
        super(c);
        //mBitmap = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);
        mBitmap = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    }
    public MyView (Context c, int color)  {  
        super(c);

        mBitmap = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)) ;
        mCanvas.drawColor(color);
    } 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);           
            /*mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);*/
    }
    @Override 
    protected void onDraw(Canvas canvas) {   
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }

用于删除油漆

 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

2 个答案:

答案 0 :(得分:6)

您应该在放置在原始位图上的透明自定义视图上绘制,而不是修改orignal。这将保持简单。 为此你可以做到

<RelativeLayout ....>
      <ImageView ......set original bitmap to this/>
      <CustomView ...... draw on this, you can erase too./>
</RelativeLayout>

要获取修改后的位图,请在getDrawingCache()上调用RelativeLayout方法。这将为您提供组合的位图图像。

希望这有帮助。

答案 1 :(得分:0)

定义一个临时位图和画布,然后在该临时位图上绘制画布并将该位图传递给onDraw,您的工作将完成,