search

Home  >  Q&A  >  body text

Android 图片涂鸦橡皮擦功能

最近在做一个画板功能,大致的不同颜色画笔、不同粗细已经实现。
参照的是该教程:android-drawing-app

现在要做的功能是,从相册或者相机导入图像,然后在上面涂涂画画,由于橡皮擦的功能其实是一个白色的paint来实现的。因此用了橡皮擦,底层的图像也被画上白色了。想要的效果是橡皮擦只对新画上去的线条有作用,并不影响导入的图像。

网上大致查找资料,好像可以使用两个图层来实现,导入的图像作为最底层,新画的线条在上一层,因此橡皮擦也不会影响到最底层的图像。

导入的图片(bitmap为导入的图片):

 drawCanvas.drawBitmap(bitmap, 0, 0, null);

橡皮擦功能:

public void setErase(boolean isErase) {
        this.isErase = isErase;
        if (isErase) {
            drawPaint.setColor(Color.WHITE);
//            drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        } else {
            drawPaint.setColor(tempColor);
            drawPaint.setXfermode(null);
        }

请问如何设置两个图层,并让后续的线条只操作在第二个图层上?这里有点卡住了。或者有类似功能的demo吗?
十分感谢

PHP中文网PHP中文网2771 days ago962

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:26:19

    Since you mentioned layers, why not draw the image in layers?
    1. I can create layers with overlapping views, process touch events in layers, and merge the bitmap data when exporting pictures.
    2. Similarly, I can also use only one view to save the image data of each layer. In the list, draw in order when drawing, and draw last on the top level. The top level is what you can modify. Finally, merge the data when exporting the image

    The so-called image processing is a data collection. If you want to process it hierarchically, you must store each piece of data independently in a different storage unit, so that if you modify the later data units, it will not affect the previous data. I have done this before. I can’t seem to find any examples after using this kind of drawing board, but that’s the idea, I wish you good luck

    reply
    0
  • Cancelreply