2

我正在使用一个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

4 回答 4

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);

于 2014-03-06T22:23:07.350 回答
2

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

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

…你应该在你的touch_move方法上提交每一幅画,以避免坚实的路径。drawpath如果也禁用isErase = true

于 2016-02-11T01:21:44.793 回答
1

为了擦除,除了画笔颜色之外,您还需要设置背景颜色。请想象您正在使用 mspaint,橡皮擦本身正在画布上“绘制”背景颜色。

如果您的背景是 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);  
于 2013-03-11T06:01:55.003 回答
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;
    }
}
于 2017-02-28T09:55:00.737 回答