【发布时间】:2011-06-24 07:16:30
【问题描述】:
我编写了一个代码来在拖动按钮时移动它。我正在根据当前鼠标坐标更新按钮 X 和 Y 坐标,但是当我拖动按钮时,鼠标坐标值在低值和高值之间切换,即使我拖动非常慢也是如此。
当我记录坐标时,值显示为:
(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;
}
}
【问题讨论】:
标签: java android android-layout android-widget