我有一个 Scroller 用于投掷。我用初始速度和一些限制对其进行初始化。当我稍后获得新的 Y 值时,getCurrY 返回传递给 fling 的 Y 限制。
这是高度简化的代码:
public class SimpleList extends ScrollView implements Runnable
{
private VelocityTracker velocityTracker = null; // initialization redacted
private Scroller scroller = null;
@Override
public boolean onTouchEvent(MotionEvent ev)
{
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
velocityTracker.addMovement (ev);
return true;
case MotionEvent.ACTION_MOVE:
{
velocityTracker.addMovement (ev);
return true;
}
case MotionEvent.ACTION_UP:
velocityTracker.computeCurrentVelocity (1000);
int index = ev.getActionIndex();
int pointerId = ev.getPointerId(index);
int velY = (int) VelocityTrackerCompat.getYVelocity (velocityTracker, pointerId);
velocityTracker.recycle();
scroller = new Scroller (getContext());
scroller.fling (0, 0, 0, velY,
Integer.MIN_VALUE, Integer.MIN_VALUE,
Integer.MAX_VALUE, Integer.MAX_VALUE);
postDelayed (this, 100);
return true;
}
return false;
}
public void run()
{
if (scroller.isFinished())
return;
if (scroller.computeScrollOffset())
{
System.out.println ("curVel " + scroller.getCurrVelocity());
System.out.println ("newY " + scroller.getCurrY());
}
}
}
初始速度似乎是合理的 - 大约 1000 像素/秒。记录的速度要少一些,这似乎也是合理的。然而,“newY”值是传递给 fling 的 MAX_VALUE。
有任何想法吗?有任何想法吗?