2014-11-15 101 views
0

我正在尝试为手指绘画制作Android绘画应用程序,并且在移动绘制的线条时遇到了问题。如何在Android中的画布上移动路径?

我试图做的是在ACTION_MOVE期间通过初始手指按下坐标和OnTouchEvent中当前坐标之间的差异来偏移当前选定行的路径。

case MotionEvent.ACTION_MOVE: 
selectline.getLine().offset(x - otherx, y - othery); 

otherx和othery在ACTION_MOVE期间设置为x和y坐标,x和y是当前光标坐标。我的线条作为独立的类存储,包含路径,颜色,厚度和边界框。

我得到的是从我的手指方向飞出屏幕的形状,没有停下一丝动作。我尝试使用矩阵来移动路径,但结果是一样的。

当我试图插入一个“do while”来检查当前坐标是否与路径的.computeBounds()矩形中心匹配,但是一旦我移动手指,程序就会崩溃。

任何帮助,将不胜感激,谢谢。

回答

0

很有可能您没有使用正确的比例尺作为坐标。

来源:Get Canvas coordinates after scaling up/down or dragging in android

float px = ev.getX()/mScaleFactor + rect.left; 
float py = ev.getY()/mScaleFactor + rect.top; 
// where mScaleFactor is the scale use in canvas and rect.left and rect.top is the coordinate of top and left boundary of canvas respectively 
+0

感谢您的答复,但坐标似乎是罚款。我向ACTION_UP中添加了“selectline.getLine()。offset(x - otherx,y - othery)”,并将形状移动到我想要的位置。我的问题是,我希望它在ACTION_MOVE期间实时跟踪我的手指。 – Vodyara

+0

您可能在ACTION_DOWN期间没有正确设置坐标。这里是类似问题的链接:http://stackoverflow.com/questions/9398057/android-move-a-view-on-touch-move-action-move – user3806339

0

它的晚了一些,但它可能会解决其他问题。我解决了这个问题,这样, 得到初步的X,Y位置上onLongPress

public void onLongPress(MotionEvent motionEvent) { 

     try { 
      shapeDrag = true; 
      SmEventX = getReletiveX(motionEvent); 
      SmEventY = getReletiveY(motionEvent); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

,然后onToucn(MotionEvent事件)

case MotionEvent.ACTION_MOVE: { 
        actionMoveEvent(motionEvent); 
        try { 

         if (shapeDrag) { 
          StylePath sp = alStylePaths 
            .get(alStylePaths.size() - 1); 
          Path mpath = sp.getPath(); 


          float tempX = getReletiveX(motionEvent) - SmEventX; 
          float tempY = getReletiveY(motionEvent) - SmEventY; 

          mpath.offset(tempX, tempY); 
          SmEventX = getReletiveX(motionEvent); 
          SmEventY = getReletiveY(motionEvent); 


         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 

        break; 
       } 
    }