删除手指后,android路径移动

时间:2013-02-17 14:50:14

标签: android canvas path bitmap

我正在使用绘图应用程序,并允许用户导入图像以进一步绘制它。然后缩小大于绘图区域的图像,以便满足最大屏幕宽度或屏幕高度。

导入的图像将使用canvas.drawBitmap(bitmap, x_adjustment, y_adjustment, paintScreen);

放置在drawingView的中心

这样,导入图像的左侧,右侧或顶部会有空白区域。调整将从(0,0)

开始计算

x_adjustmenty_adjustment

编码:

的onDraw

   @Override
   protected void onDraw(Canvas canvas)   
   {
       canvas.drawBitmap(bitmap, x_adjustment, y_adjustment, paintScreen); 

      for (Integer key : pathMap.keySet()) 
         canvas.drawPath(pathMap.get(key), paintLine); // draw line
   } 

touchStarted:

   private void touchStarted(float x, float y, int lineID) 
   {      
      Path path; // used to store the path for the given touch id
      Point point; // used to store the last point in path

         path = new Path(); // create a new Path
         pathMap.put(lineID, path); // add the Path to Map
         point = new Point();
         previousPointMap.put(lineID, point);

      path.moveTo(x, y);
      point.x = (int) x;  
      point.y = (int) y;         
   } 

touchMoved:

   // called when the user drags along the screen
   private void touchMoved(MotionEvent event) 
   {
      // for each of the pointers in the given MotionEvent
      for (int i = 0; i < event.getPointerCount(); i++) 
      {
         // get the pointer ID and pointer index
         int pointerID = event.getPointerId(i);
         int pointerIndex = event.findPointerIndex(pointerID);

         if (pathMap.containsKey(pointerID)) 
         {
            // get the new coordinates for the pointer
            float newX = event.getX(pointerIndex);
            float newY = event.getY(pointerIndex);

            // get the Path and previous Point associated with this pointer
            Path path = pathMap.get(pointerID);
            Point point = previousPointMap.get(pointerID);

            float deltaX = Math.abs(newX - point.x);
            float deltaY = Math.abs(newY - point.y);

            if (deltaX >= TOUCH_TOLERANCE || deltaY >= TOUCH_TOLERANCE) 
            {
               path.quadTo(point.x, point.y, ((newX + point.x)/2),((newY + point.y)/2));

               // store the new coordinates
               point.x = (int) newX ;
               point.y = (int) newY ;                   
            }
         } 
      }          
   } 

touchEnded:

   private void touchEnded(int lineID)
   {
      Path path = pathMap.get(lineID); 
      bitmapCanvas.drawPath(path, paintLine); 
      path.reset();               
   }

问题:

由于导入的图像位于中心而不是(0,0),因为对于每一行绘制,当它在绘图和触摸屏时正确显示时,当用户移开手指时,即触摸结束,最终的行将被x_adjustment和y_adjustment移位。

e.g。如果缩放图像宽度<1。 screenwidth,左边和右边有空白区域,绘制线条时显示正确,但当手指被移除时,线条将错误地立即转移到右侧{ {1}};

我知道这是因为调整导致错误。我知道这是通过x,y shift保存路径的坐标。但我不知道如何修改代码,我试图添加调整路径,但仍然失败。有人可以帮忙给我一些指导吗?非常感谢!

0 个答案:

没有答案