如何在类似Paint的应用程序中实现擦除功能?

时间:2016-03-29 12:22:33

标签: android android-custom-view android-bitmap

我有一个自定义视图DrawingPanel,并且在我使用它来制作一个可以让你在图像上绘制的绘画应用程序。我现在想要添加删除功能以使图像透明。我已经尝试过了使用mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));但是它在触摸时给我黑色,我无法改变这种颜色。如何改善擦除位图功能?

public DrawingPanel(Context context, int color) {
                super(context);
                this.color = color;
                setFocusable(true);
                setFocusableInTouchMode(true);

                this.setOnTouchListener(this);

                mBitmapPaint = new Paint(Paint.DITHER_FLAG);
                mPaint = new Paint();
                mPaint.setAntiAlias(true);
                mPaint.setDither(true);
                mPaint.setColor(color);
                mPaint.setStyle(Paint.Style.STROKE);
                mPaint.setStrokeJoin(Paint.Join.ROUND);
                mPaint.setStrokeCap(Paint.Cap.ROUND);
                mPaint.setStrokeWidth(3);
                mPaint.setTextSize(30);

                mPath = new Path();
                paths.add(new PathPoints(mPath, color, false));
                mCanvas = new Canvas();
            }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
            for (PathPoints p : paths) {
                mPaint.setColor(p.getColor());
                Log.v("", "Color code : " + p.getColor());
                if (p.isTextToDraw()) {
                    canvas.drawText(p.textToDraw, p.x, p.y, mPaint);
                } else {
                    canvas.drawPath(p.getPath(), mPaint);
                }
            }
        }

0 个答案:

没有答案