2010-08-16 60 views
1

我有我自己的自定義TextView這個奇怪的問題。我試圖通過用手指拖動視圖來移動視圖,所以我得到觸摸位置,做一些數學運算,並設置相應的leftMargin和topMargin。它有效,但我有一些非常奇怪的行爲。 leftMargin部分完美地工作,但topMargin很跳躍。它好像在正確的位置和它下面25個像素的位置之間擺動。當我只點擊視圖而不是連續拖動視圖時,它每次觸摸都會向下移動25個像素。有沒有人有任何想法,爲什麼這可能是?相關的代碼在這裏:奇怪的b​​ug與設置LayoutParams頁邊距

 case MotionEvent.ACTION_MOVE : 
{  
    final float x = event.getX(); 
    final float y = event.getY(); 

    final float newMarginX; 
    final float newMarginY; 

    positionX = x; 
    positionY = y; 

    newMarginX = oldMarginX - (lastTouchX - positionX); 
    newMarginY = oldMarginY - (lastTouchY - positionY); 

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(this.getWidth(), this.getHeight()); 
    params.leftMargin = (int) newMarginX; 
    params.topMargin = (int) newMarginY; 
    this.setLayoutParams(params); 
    this.setText(Float.toString(y)); 

    lastTouchX = positionX; 
    lastTouchY = positionY; 

    oldMarginX = newMarginX; 
    oldMarginY = newMarginY; 

    break;  
} 
+0

沒有想法?請,我一直在瘋狂試圖弄清楚這一點 – dima1109 2010-08-18 06:22:11

回答

0

確保設置重力。