如何擦除画布上绘制的线条?

时间:2013-03-11 05:55:48

标签: android canvas draw erase

我正在使用Canvas类在画布上绘制线条。现在我想用与使用橡皮擦的笔记本中类似的方式擦除画布中绘制的线条。我经历了几个例子,但对我来说没有任何作用。

如果有人知道这个问题的解决方案,请帮我解决这个问题。

Java代码

public class DrawView extends View implements OnTouchListener 
{
        private Canvas      m_Canvas;

        private Path        m_Path;

        private Paint       m_Paint;

        ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();

        ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>(); 

        private float mX, mY;

        private static final float TOUCH_TOLERANCE = 4;

        private Bitmap          bitmapToCanvas;

        private CanvasManager   m_CanvasManagerObject;

        private Paint   mBitmapPaint;

        public DrawView(Context context)
        {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);      
            this.setOnTouchListener(this);

            onCanvasInitialization();
        }    

        public void onCanvasInitialization()
        {
            m_Paint = new Paint(Paint.DITHER_FLAG);
            m_Paint.setAntiAlias(true);
            m_Paint.setDither(true);
            m_Paint.setColor(Color.parseColor("#37A1D1"));
            m_Paint.setStyle(Paint.Style.STROKE);
            m_Paint.setStrokeJoin(Paint.Join.ROUND);
            m_Paint.setStrokeCap(Paint.Cap.ROUND);
            m_Paint.setStrokeWidth(2);                          
            m_Path = new Path();

            mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) 
        {       
            super.onSizeChanged(w, h, oldw, oldh);

            bitmapToCanvas = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
            m_Canvas = new Canvas(bitmapToCanvas);
        }

        @Override
        protected void onDraw(Canvas canvas)
        {           
            canvas.drawBitmap(bitmapToCanvas, 0f, 0f, mBitmapPaint);    
            canvas.drawPath(m_Path, m_Paint);
        }

        public boolean onTouch(View arg0, MotionEvent event) 
        {
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) 
            {
                case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
                case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
                case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
            }

            return true;
        }

        private void touch_start(float x, float y) 
        {
            m_Path.reset();
            m_Path.moveTo(x, y);
            mX = x;
            mY = y;
        }

        private void touch_move(float x, float y) 
        {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) 
            {
                m_Path.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            }
        }

        private void touch_up() 
        {
            m_Path.lineTo(mX, mY);

            // commit the path to our offscreen
            m_Canvas.drawPath(m_Path, m_Paint);

            // kill this so we don't double draw                    
            Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
            paths.add(new Pair<Path, Paint>(m_Path, newPaint));
            m_Path = new Path();
        }

        public void onClickEraser() 
        { 

        }

}

4 个答案:

答案 0 :(得分:6)

如果您有纯色背景,则需要将Paint颜色设置为背景颜色。例如,如果您有白色背景,则可以执行以下操作:

paint.setColor(Color.White);

但是,如果您需要删除具有透明背景的线条,请尝试以下操作:

为了使用透明色绘制,您必须使用Paint setXfermode,这只有在您为画布设置位图时才有效。如果您按照以下步骤操作,则应获得所需的结果。

  1. 创建画布并设置其位图。

    mCanvas = new Canvas();
    mBitmap= Bitmap.createBitmap(scrw, scrh, Config.ARGB_8888);
    mCanvas.setBitmap(mBitmap);
    
  2. 当您想要删除某些内容时,您只需要使用setXfermode。

    public void onClickEraser() 
    { 
       if (isEraserOn)
          mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
       else
          mPaint.setXfermode(null);
    }
    
  3. 现在您应该可以使用以下方法绘制透明色:

    mCanvas.drawPath(path, mPaint);

答案 1 :(得分:2)

与Daniel Albert的答案相同, 使用后:

public void onClickEraser() 
{ 
   if (isEraserOn)
      mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
   else
      mPaint.setXfermode(null);
}

...你应该在touch_move方法上提交每一个颜色以避免固体路径。如果drawpath

,也会停用isErase = true

答案 2 :(得分:1)

为了擦除,除刷子颜色外,还需要设置背景颜色。 请想象一下,你正在使用mspaint,橡皮擦本身正在“绘制”canva上的背景颜色。

如果你的背景是000 然后delbrush就像

delPaint = new Paint();
delPaint.setColor(0x00000000);
delPaint.setXfermode(clear);
delPaint.setAlpha(0x00);
delPaint.setAntiAlias(true);
delPaint.setDither(true);
delPaint.setStyle(Paint.Style.STROKE);
delPaint.setStrokeJoin(Paint.Join.ROUND);
delPaint.setStrokeCap(Paint.Cap.ROUND);  

答案 3 :(得分:0)

 relativeLayout = (RelativeLayout) findViewById(R.id.relativelayout1);

    button = (Button)findViewById(R.id.button);
    view = new SketchSheetView(slate.this);
    paint = new Paint();
    path2 = new Path();

    relativeLayout.addView(view, new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT,
    RelativeLayout.LayoutParams.MATCH_PARENT));

    paint.setDither(true);

    paint.setColor(Color.BLACK);

    paint.setStyle(Paint.Style.STROKE);

    paint.setStrokeJoin(Paint.Join.ROUND);

    paint.setStrokeCap(Paint.Cap.ROUND);

    paint.setStrokeWidth(5);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            path2.reset();
            relativeLayout.removeAllViewsInLayout();
            view = new SketchSheetView(slate.this);
            relativeLayout.addView(view, new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.MATCH_PARENT));
        }
    });
}


private class SketchSheetView extends View {
    public SketchSheetView(slate slate) {
        super(slate);
        bitmap = Bitmap.createBitmap(820, 480, Bitmap.Config.ARGB_4444);

        canvas = new Canvas(bitmap);

        this.setBackgroundColor(Color.WHITE);
    }
    ArrayList<DrawingClass> DrawingClassArrayList= new ArrayList<DrawingClass>();



    @Override
    public boolean onTouchEvent(MotionEvent event) {

        DrawingClass pathWithPaint = new DrawingClass();

        canvas.drawPath(path2, paint);

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            path2.moveTo(event.getX(), event.getY());

            path2.lineTo(event.getX(), event.getY());
        }
        else if (event.getAction() == MotionEvent.ACTION_MOVE) {

            path2.lineTo(event.getX(), event.getY());

            pathWithPaint.setPath(path2);

            pathWithPaint.setPaint(paint);

            DrawingClassArrayList.add(pathWithPaint);

        }

        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (DrawingClassArrayList.size() > 0) {

            canvas.drawPath(
                    DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPath(),

                    DrawingClassArrayList.get(DrawingClassArrayList.size() - 1).getPaint());
        }
    }
}

public class DrawingClass {

    Path DrawingClassPath;
    Paint DrawingClassPaint;

    public Path getPath() {
        return DrawingClassPath;
    }

    public void setPath(Path path) {
        this.DrawingClassPath = path;
    }


    public Paint getPaint() {
        return DrawingClassPaint;
    }

    public void setPaint(Paint paint) {
        this.DrawingClassPaint = paint;
    }
}
相关问题