2012-07-12 47 views
1

所以我試圖製作一個「磁性詩歌」類型的應用程序。用戶將在屏幕上移動各種Button小部件。我使用的是一個Button小部件,因爲它最接近磁鐵的外觀,儘管我對其他選項開放!拖放舊版Android不精確

的對象不正確地與我的手指移動。他們很近,但他們並不完全符合我的手指。我手機上的X座標似乎很好,但是Y座標已關閉。這可能是由於標題欄?

private final static int DRAGGING_OFF = 0; 
private final static int DRAGGING_ON = 1; 

private int dragStatus; 

private GestureDetector mGestureDetector; 
private int mOffsetX; 
private int mOffsetY; 
private LayoutParams mOldParams; 
private RelativeLayout.LayoutParams lp; 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if(!mGestureDetector.onTouchEvent(event)) { 
     if(event.getAction() == MotionEvent.ACTION_MOVE) { 
      if(dragStatus == DRAGGING_ON) { 
       Log.e(VIEW_LOG_TAG, "Dragging! RAW -- " + event.getRawX() + " : " + event.getRawY() + " NOT RAW -- " + event.getX() + " : " + event.getY()); 
       int rawX, rawY; 
       int finalX, finalY; 

       rawX = (int) event.getRawX(); 
       rawY = (int) event.getRawY(); 

       finalX = rawX - mOffsetX; 
       finalY = rawY - mOffsetY; 

       lp.setMargins(finalX, finalY, 0, 0); 
       this.setLayoutParams(lp); 
      } 

      return true; 
     } else if(event.getAction() == MotionEvent.ACTION_UP) { 
      if(dragStatus == DRAGGING_ON) { 
       dragStatus = DRAGGING_OFF; 
       Log.e(VIEW_LOG_TAG, "Stopped dragging!"); 
      } 

      return true; 
     } else { 
      return super.onTouchEvent(event); 
     } 
    } else { 
     return true; 
    } 
} 

private final GestureDetector.SimpleOnGestureListener mListener = new GestureDetector.SimpleOnGestureListener() { 
    @Override 
    public boolean onDown(MotionEvent e) {   
     int[] location = new int[2]; 
     Log.e(VIEW_LOG_TAG, "Down! RAW -- " + e.getRawX() + " : " + e.getRawY() + " NOT RAW -- " + e.getX() + " : " + e.getY()); 
     dragStatus = DRAGGING_ON; 

     // Sets the current location of the View in the int[] passed to it; x then y. 
     getLocationInWindow(location); 
     Log.e(VIEW_LOG_TAG, "Down location: " + location[0] + " " + location[1]); 

     mOffsetX = (int) e.getRawX() - location[0]; 
     mOffsetY = (int) e.getRawY() - location[1]; 
     mOldParams = getLayoutParams(); 

     return true; 
    } 
}; 

回答

0

我認爲這個問題是,你沒有得到當前觸摸點的Y和緩存,最後觸摸點y的DELTA。你需要像這樣的東西:

@Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     final TextView textView = (TextView) findViewById(R.id.text); 
     final ImageView image = (ImageView) findViewById(R.id.image); 
     matrix.setTranslate(1f, 1f); 
     image.setImageMatrix(matrix); 
     image.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       matrix.getValues(m); //copy matrix values into array 

       PointF currentXYTouchPoint = new PointF(event.getX(), event.getY()); 

       switch(event.getAction()) { 
        case MotionEvent.ACTION_DOWN: //start of pressed gesture 
         lastXYTouchPoint.set(event.getX(), event.getY()); //save the last touchpoint 
         mode = DRAG; 
         break; 
        case MotionEvent.ACTION_MOVE: 
         //calculate the change in finger position 
         float deltaX = currentXYTouchPoint.x - lastXYTouchPoint.x; 
         float deltaY = currentXYTouchPoint.y - lastXYTouchPoint.y; 
         matrix.postTranslate(deltaX, deltaY); //move the entire image by the deltas 
         image.setImageMatrix(matrix); 

         // save this last starting touchpoint 
         lastXYTouchPoint.set(currentXYTouchPoint.x, currentXYTouchPoint.y); 
         break; 
       } 

       textView.setText("TouchPoint started at " + currentXYTouchPoint.x + ", " + currentXYTouchPoint.y + " & the matrix is now " + Arrays.toString(m)); 
       return true; 
      } 
     }); 

    } 

適應它你自己的目的,但基本的想法是存在的。

+0

這似乎是一個可行的方法。我只需要對getLocationInWindow()進行測試,以確保我從那裏得到的數字可以直接用來做數學並傳回到LayoutParams.setMargins() – tomwardiii 2012-07-12 19:21:01

+0

因此事實證明,我遇到的問題不是與使用我的方法或使用您建議的delta方法有關。它似乎與標題欄和狀態欄高度更相關。我得到的數字是相對於屏幕的,但是我設置的位置信息應該是相對於窗口的,所以它在Y座標上是關閉的。有人爲我找到了這個:http://andmobidev.blogspot.com/2010/01/getting-height-of-status-and-title-bar.html – tomwardiii 2012-07-13 15:01:53