我需要初始的 x 和 y 值(从第一次触摸ACTION_DOWN
开始移动的点),以便计算deltaX
和{ {1}}如下;
deltaY
有没有办法可以从private float mInitialTouchX = Float.NaN; // # First touch temp var defined globally
...
someView.setOnTouchListener(new OnTouchListener() {
@Override public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_MOVE:
MoveViewHorizontally(v, event.getX(), mInitialTouchX);
break;
case MotionEvent.ACTION_DOWN:
mInitialTouchX = event.getX(); // # Set initial touch point
break;
case MotionEvent.ACTION_UP:
mInitialTouchX = Float.NaN; // # Reset initial touch point
break;
default: break;
}
return false;
}
});
private void MoveViewHorizontally(View v, float x, float initialTouchX)
{
int deltaX = (int)(x - initialTouchX);
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
layoutParams.leftMargin += deltaX;
v.setLayoutParams(layoutParams);
}
或某种其他方式获取初始触摸值,避免使用全局变量event
?