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

发布于 2024-12-01 02:16:51 字数 1586 浏览 0 评论 0原文

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

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

Can any body tell me to erase the paint on the image , in my application i was prepared the finger painting on image, if i want erase the paint it,s getting black color on image instead of erasing the image. my code is

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

for paint erase

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年已逝 2024-12-08 02:16:51

您应该在放置在原始位图上的透明自定义视图上进行绘制,而不是修改原始位图。这会让事情变得简单。
为此,您可以执行以下操作:

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

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

希望这有帮助。

You should draw on a transparent custom view placed over the original bitmap instead of modifying the orignal. That will keep it simple.
For that you can do

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

For getting the modified bitmap call the getDrawingCache() method on that RelativeLayout. That will give you the combined bitmap image.

Hope this helps.

得不到的就毁灭 2024-12-08 02:16:51

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

define a temporary bitmap and canvas, then draw canvas on that temporary bitmap and pass that bitmap to onDraw your work will be done,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文