【问题标题】:Toggling coordinates on ACTION_MOVE在 ACTION_MOVE 上切换坐标
【发布时间】: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


    【解决方案1】:

    我没有用你的代码测试过,但我过去也遇到过类似的事情。

    getX() 和 getY() 函数返回与您的 Button 相关的值。由于您正在更改按钮在 onTouchListener 中的位置,因此您也更改了 getX() 和 getY() 所基于的坐标系。

    尝试改用 getRawX() 和 getRawY()。这些函数与 Button 的位置无关,不应为您提供交替值。

    【讨论】:

    • 很高兴能帮上忙!获得赏金是否足够有用?
    • 当然,我快疯了。我很久以前就发布了这个问题,但没有人能指出我正确的方向。非常感谢您的帮助。
    • 我想你误会了我。你接受了我的回答,但你没有奖励赏金。您必须在赏金到期之前手动授予赏金(这与接受答案是分开的)。然后我将获得 +50 声望,您将获得恩人徽章。每个人都赢了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多