我已经使用类似以下的代码在Android上实现了一个简单的可滚动画廊类型视图:
private int mPos; // The current scroll position.
private int mLastMostionPos; // Previous touch position.
@Override
public boolean onTouchEvent(MotionEvent ev)
{
final int action = ev.getAction();
Log.d(TAG, "Mouse mLastMotionPos: " + mLastMotionPos + " ev.getX(): " + ev.getX() + " mPos: " + mPos + " ev.getPointerId(0): " + ev.getPointerId(0)
+ " ev.getEventTime(): " + ev.getEventTime());
switch (action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
// Remember where the motion event started
mLastMotionPos = (int)ev.getX();
break;
case MotionEvent.ACTION_MOVE:
final int pos = (int)ev.getX(activePointerIndex);
// Scroll to follow the motion event
int delta = mLastMotionPos - pos;
mLastMotionPos = pos;
mPos += delta;
if (mPos < mMinPos)
mPos = mMinPos;
if (mPos > mMaxPos)
mPos = mMaxPos;
break;
并且它有效,但我注意到有时它会突然“跳跃”一点。实际上,只有在滚动到一端时才值得注意。如果您已经一直向右滚动,并且始终将手指向右移动,则有时事物将向左跳一帧。
我在上面添加了日志记录行并获得了此输出(为便于阅读而进行了编辑:]
50.913: Mouse mLastMotionPos: 304 ev.getX(): 379.0 mPos: 0 ev.getPointerId(0): 0 ev.getEventTime(): 111690077
50.928: Mouse mLastMotionPos: 379 ev.getX(): 369.0 mPos: 0 ev.getPointerId(0): 0 ev.getEventTime(): 111690093
50.943: Mouse mLastMotionPos: 369 ev.getX(): 384.0 mPos: 10 ev.getPointerId(0): 0 ev.getEventTime(): 111690109
50.958: Mouse mLastMotionPos: 384 ev.getX(): 391.0 mPos: 0 ev.getPointerId(0): 0 ev.getEventTime(): 111690125
50.983: Mouse mLastMotionPos: 391 ev.getX(): 399.0 mPos: 0 ev.getPointerId(0): 0 ev.getEventTime(): 111690141
51.023: Mouse mLastMotionPos: 399 ev.getX(): 399.0 mPos: 0 ev.getPointerId(0): 0 ev.getEventTime(): 111690186
请注意,它跳了10个像素!我发誓我只是将手指向右拖动,并且对此深思熟虑。起初我以为Android可能会无序提供事件,但事件时间是单调的。
知道为什么会这样吗?我想这可能是三星的错(我有Galaxy S2)。它有a retarded driver-imposed touch threshold,所以写输入驱动程序的人显然都不知道他们在做什么。不过,10像素似乎是一个巨大的飞跃。有时甚至更大!
我已经使用类似以下的代码在Android上实现了一个简单的可滚动画廊类型视图:private int mPos; //当前滚动位置。 private int mLastMostionPos; //先前的触摸位置。...