为Android绘图应用程序创建橡皮擦

时间:2014-08-10 23:23:57

标签: java android android-canvas eraser

我正在使用绘图应用程序并且非常接近发布,但我对应用程序的橡皮擦部分存在问题。我有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);
    }

1 个答案:

答案 0 :(得分:1)

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