【问题标题】:How to erase line drawn on a canvas?如何擦除画布上绘制的线条?
【发布时间】:2013-03-11 05:55:47
【问题描述】:

我正在使用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() 
        { 
            
        }
                
}

【问题讨论】:

    标签: android canvas draw erase


    【解决方案1】:

    如果您有纯色背景,您只需将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);

    【讨论】:

      【解决方案2】:

      与 Daniel Albert 的回答相提并论, 使用后:

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

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

      【讨论】:

        【解决方案3】:

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

        如果你的背景是 000 那么毛刷可能是这样的

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

        【讨论】:

          【解决方案4】:
           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;
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-03-29
            • 2021-12-14
            • 1970-01-01
            • 2014-07-19
            • 1970-01-01
            • 1970-01-01
            • 2019-11-11
            相关资源
            最近更新 更多