android(单点触控)绘图应用程序撤消方法无法正常工作

2023-12-20

我正在开发一个绘图应用程序,但面临一些撤消问题。编码如下:

public class DoodleView extends View 
{
    Context context_new;

    private static final float TOUCH_TOLERANCE = 5;
    private Bitmap bitmap; // drawing area for display or saving
    private Canvas bitmapCanvas; // used to draw on bitmap
    private Paint paintScreen; // use to draw bitmap onto screen
    private Paint paintLine; // used to draw lines onto bitmap

    private Path mPath; 
    private ArrayList<Path> paths = new ArrayList<Path>();
    private ArrayList<Path> undonePaths = new ArrayList<Path>();
    private float mX, mY;

   // DoodleView constructor initializes the DoodleView
   public DoodleView(Context context, AttributeSet attrs) 
   {
       super(context, attrs); // pass context to View's constructor
       this.context_new=context;

       paintScreen = new Paint(); // used to display bitmap onto screen

       // set the initial display settings for the painted line
       paintLine = new Paint();
       paintLine.setAntiAlias(true); // smooth edges of drawn line
       paintLine.setColor(Color.BLACK); // default color is black
       paintLine.setStyle(Paint.Style.STROKE); // solid line

       mPath = new Path();
       paths.add(mPath);

   } // end DoodleView constructor

尺寸改变时:

   @Override
   public void onSizeChanged(int w, int h, int oldW, int oldH)
   {
      super.onSizeChanged(w, h, oldW, oldH);
      DoodlzViewWidth = w;     
      DoodlzViewHeight = h;

      bitmap = Bitmap.createBitmap(getWidth(), DoodlzViewHeight, Bitmap.Config.ARGB_8888);

      bitmapCanvas = new Canvas(bitmap);
      bitmap.eraseColor(Color.WHITE); // erase the BitMap with white 
   } 

onDraw:

   @Override
   protected void onDraw(Canvas canvas)  
   {
       canvas.drawBitmap(bitmap, 0, 0, paintScreen); 
       // for each path currently being drawn
       for (Path p : paths){canvas.drawPath(p, paintLine);}                
   } 

触摸事件:

   @Override
   public boolean onTouchEvent(MotionEvent event) 
   {          
          float x = event.getX();
          float y = event.getY();

          switch (event.getAction())
          {
              case MotionEvent.ACTION_DOWN:
                  touchStarted(x, y);
                  invalidate();
                  break;
              case MotionEvent.ACTION_MOVE:
                  touchMoved(x, y);
                  invalidate();
                  break;
              case MotionEvent.ACTION_UP:
                  touchEnded();
                  invalidate();
                  break;
          }
          return true;
    }

触摸开始:

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

触摸移动:

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

触摸结束:

   private void touchEnded() 
   {
      mPath.lineTo(mX, mY);      
      bitmapCanvas.drawPath(mPath, paintLine);
      mPath = new Path();
      paths.add(mPath);
      Toast.makeText(getContext(), "touchEnded" + paths.size(), Toast.LENGTH_SHORT).show();
   }

Undo:

public void onClickUndo() 
{ 
   Toast.makeText(getContext(), "before undo button pressed " + paths.size(), Toast.LENGTH_SHORT).show();  
   if (paths.size()>0) 
    { 
       undonePaths.add(paths.remove(paths.size()-1));
       Toast.makeText(getContext(), "after undo button pressed " + paths.size(), Toast.LENGTH_SHORT).show();
       Log.i("UNDOING", "PREPARE INVALIDATE");
       invalidate();
       Log.i("UNDOING", "FINISH INVALIDATE");
    }      
   else Toast.makeText(getContext(), "nothing to undo" + paths.size(), Toast.LENGTH_SHORT).show();  
}

问题:

以上摘自网上搜索的其他例子。不知道为什么在实现时需要设置path.reset()touchStarted?

Q1.当我按下撤消按钮时,它会正确显示toast按下撤消按钮,并报告path.size()为 0,因此之前绘制的线为没有删除。我真的不知道为什么是0??它不是已经添加到路径数组中了吗?代码怎么修改呢?

** 在听取 android 开发人员的建议后修改了代码!谢谢!!现在它可以正确显示path.size()。抱歉,愚蠢的错过了!*但是之前画的线还是无法去掉? =(

Q2。虽然当手指在屏幕上移动并立即显示线条时应用程序运行正常,但当我按下撤消按钮时,除了上面未删除的上一行之外,按下直到手指抬起后按钮才会显示。

回答问题2:将以下两行从touchEnded() to touchStarted()

mPath = new Path();
paths.add(mPath);

.

   private void touchStarted(float x, float y) 
   {
       mPath.reset();
       mPath = new Path();
       paths.add(mPath);
       mPath.moveTo(x, y);
       mX = x;
       mY = y;
   }

   private void touchEnded() 
   {
       mPath.lineTo(mX, mY);      
       bitmapCanvas.drawPath(mPath, paintLine);// commit the path to our offscreen                   
       Toast.makeText(getContext(), "touchEnded" + paths.size(), Toast.LENGTH_SHORT).show();
   }

谢谢!!!


更新并工作:

  1. 删除了PaintpaintScreen;
  2. 在onDraw中不要放canvas.drawBitmap(bitmap, 0, 0, paintScreen); or canvas.drawBitmap(bitmap, 0, 0, paintLine);

    private Bitmap bitmap; // drawing area for display or saving
    private Canvas bitmapCanvas; // used to draw on bitmap
    private Paint paintLine; // used to draw lines onto bitmap   
    private Path mPath; 
    private ArrayList<Path> paths = new ArrayList<Path>();
    private ArrayList<Path> undonePaths = new ArrayList<Path>();
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;    
    
    // DoodleView constructor initializes the DoodleView
    
    public DoodleView(Context context, AttributeSet attrs) 
    {
       super(context, attrs); // pass context to View's constructor
       this.context_new=context;
       setFocusable(true);
       setFocusableInTouchMode(true);      
    
       // set the initial display settings for the painted line
       paintLine = new Paint();
       paintLine.setAntiAlias(true); // smooth edges of drawn line
       paintLine.setDither(true);
       paintLine.setColor(Color.BLACK); // default color is black
       paintLine.setStyle(Paint.Style.STROKE); // solid line
       paintLine.setStrokeJoin(Paint.Join.ROUND);
       paintLine.setStrokeWidth(5); // set the default line width
       paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
    
       bitmapCanvas = new Canvas();
       mPath = new Path();            
     } // end DoodleView constructor
    
     // Method onSizeChanged creates BitMap and Canvas after app displays
    
     @Override
     public void onSizeChanged(int w, int h, int oldW, int oldH)
     {
        super.onSizeChanged(w, h, oldW, oldH);
        DoodlzViewWidth = w;       
        DoodlzViewHeight = h;           
     } 
    
     @Override
    
     protected void onDraw(Canvas canvas) 
     {         
       for (Path p : paths){canvas.drawPath(p, paintLine);}  
       canvas.drawPath(mPath, paintLine);
       Log.i("OnDRAWING", "REACH ON DRAW");        
    } 
    

    // START TOUCH: 处理触摸事件 @覆盖

     public boolean onTouchEvent(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) 
     {
       undonePaths.clear();
       mPath.reset();
       mPath.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) 
        {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }  
    
    private void touch_up() 
    {
        mPath.lineTo(mX, mY);      
        bitmapCanvas.drawPath(mPath, paintLine);// commit the path to our offscreen  
        paths.add(mPath);
        mPath = new Path(); 
    }
    
    public void onClickUndo() 
    { 
       if (paths.size()>0) 
        { 
           undonePaths.add(paths.remove(paths.size()-1));
           invalidate();
        }      
       else Toast.makeText(getContext(), "nothing more to undo", Toast.LENGTH_SHORT).show();  
    }
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android(单点触控)绘图应用程序撤消方法无法正常工作 的相关文章

随机推荐