1
我寫了一個代碼來移動拖動按鈕。我正在更新基於當前鼠標座標的按鈕X和Y座標,但是當我拖動按鈕時,即使拖動速度很慢,鼠標座標值也會在低值和高值之間切換。在ACTION_MOVE上切換座標
當我記錄的座標值顯示:
(70, 24) - (15, 36) - (86, 51) - (20, 48) - (90, 54) - (32, 60) - (102, 66) ...
正如你可以看到,他們是高值和低值之間切換,甚至當我在一個方向上非常緩慢拖動。誰能告訴我爲什麼?
這裏是我的代碼:
public class MyFrames extends Activity implements View.OnTouchListener {
public Button moveable;
// public TextView log;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//changer = (Button) findViewById(R.id.btn_changer);
moveable = (Button) findViewById(R.id.btn_moveable);
moveable.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_MOVE:
int x = (int)event.getX();
int y = (int)event.getY();
int w = moveable.getMeasuredWidth();
int h = moveable.getMeasuredHeight();
moveable.layout(x, y, x + w, y + h);
Log.v("Moveable", "X = " + x + " Y = " + y + "\n");
return true;
}
return false;
}
}
非常感謝!那樣做了! –
我很高興能幫上忙!是否有足夠的幫助來賺取賞金? – theisenp
當然,我瘋了。我很久以前就已經發布了這個問題,但沒有人能夠指出我正確的方向。非常感謝你的幫助。 –