0

我正在开发一个绘图应用程序,并且非常接近发布,但我遇到了应用程序的橡皮擦部分的问题。我有 2 个主屏幕(片段),其中一个只是一个空白的白色画布,用户可以在上面绘制一些选项等等。另一个是笔记片段。这个笔记片段看起来像笔记本纸。所以为了擦除绘图片段,我可以简单地使用画布的背景,用户不会知道区别。在笔记片段上,虽然我不能这样做,因为我需要保持背景完好。我研究了 PorterDuffer 模式并尝试了清晰版本并尝试绘制到单独的位图上,但没有任何效果。如果有一种方法可以控制在什么之上绘制什么,那将很有用。我愿意接受任何建议,我似乎无法得到任何工作。

我还玩过在擦除之前启用绘图缓存,但这不起作用。此外,我尝试了硬件启用,这使我的自定义视图表现得很奇怪。下面是相关代码。我的绘制方法经历了很多路径,因为我正在查询它们以允许其他一些功能。

@Override
    protected void onDraw(Canvas canvas) {

        //draw the backgroumd type
        if(mDrawBackground) {
            //draw the background
            //if the bitmap is not null draw it as the background, otherwise we are in a note view
            if(mBackgroundBitmap != null) {
                canvas.drawBitmap(mBackgroundBitmap, 0, 0, backPaint);
            } else {
                drawBackgroundType(mBackgroundType, canvas);
            }
        }

        for (int i = 0; i < paths.size(); i++ ) {
            //Log.i("DRAW", "On draw: " + i);
            //draw each previous path.
            mDrawPaint.setStrokeWidth(strokeSizes.get(i));
            mDrawPaint.setColor(colors.get(i));
            canvas.drawPath(paths.get(i), mDrawPaint);
        }
        //set paint attributes to the current value
        mDrawPaint.setStrokeWidth(strokeSize);
        mDrawPaint.setColor(mDrawColor);
        canvas.drawPath(mPath, mDrawPaint);

    }

和我的绘制背景方法

/**
     * Method that actually draws the notebook paper background
     * @param canvas the {@code Canvas} to draw on.
     */
    private void drawNoteBookPaperBackground(Canvas canvas) {

        //create bitmap for the background and a temporary canvas.
        mBackgroundBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBackgroundBitmap);
        //set the color to white.
        mBackgroundBitmap.eraseColor(Color.WHITE);

        //get the height and width of the view minus padding.
        int height = getHeight() - getPaddingTop() - getPaddingBottom();
        int width = getWidth() - getPaddingLeft() - getPaddingRight();

        //figure out how many lines we can draw given a certain line width.
        int lineWidth = 50;
        int numOfLines = Math.round(height / lineWidth);
        Log.i("DRAWVIEW", "" + numOfLines);


        //iterate through the number of lines and draw them.
        for(int i = 0; i < numOfLines * lineWidth; i+=lineWidth) {
            mCanvas.drawLine(0+getPaddingLeft(), i+getPaddingTop(), width, i+getPaddingTop(), mNoteBookPaperLinePaint);
        }

        //now we need to draw the vertical lines on the left side of the view.
        float startPoint = 30;
        //set the color to be red.
        mNoteBookPaperLinePaint.setColor(getResources().getColor(R.color.notebook_paper_vertical_line_color));

        //draw first line
        mCanvas.drawLine(startPoint, 0, startPoint, getHeight(), mNoteBookPaperLinePaint);
        //space the second line next to the first one.
        startPoint+=20;
        //draw the second line
        mCanvas.drawLine(startPoint, 0, startPoint, getHeight(), mNoteBookPaperLinePaint);

        //reset the paint color.
        mNoteBookPaperLinePaint.setColor(getResources().getColor(R.color.notebook_paper_horizontal_line_color));

        canvas.drawBitmap(mBackgroundBitmap, 0, 0, backPaint);
    }
4

1 回答 1

1

对于所有看到这个问题的人,我想我会补充一下我是如何解决这个问题的。我正在做的是在我的自定义视图中创建一个背景位图,然后将其传递给我的托管片段。然后片段将该位图设置为其包含视图组的背景,这样当我使用 PorterDuff.CLEAR Xfermode 时,绘制的路径会被清除,但片段父级中的背景保持不变。

于 2014-08-11T03:18:22.350 回答