Android里Scroller类是为了实现View平滑滚动的一个Helper类。通常在自定义的View时使用,在View中定义一个私有成员mScroller = new Scroller(context)。设置mScroller滚动的位置时,并不会导致View的滚动,通常是用mScroller记录/计算View滚动的位置,再重写View的computeScroll(),完成实际的滚动。 相关API介绍如下
OverScroller
- mScroller.getCurrX()
- mScroller.getCurrY()
- mScroller.getFinalX()
- mScroller.getFinalY()
- mScroller.setFinalX(int newX)
- mScroller.setFinalY(int newY)
-
-
- mScroller.startScroll(int startX, int startY, int dx, int dy)
- mScroller.startScroll(int startX, int startY, int dx, int dy, int duration)
-
- mScroller.computeScrollOffset()
举例说明,自定义一个CustomView,使用Scroller实现滚动:
[java] view plain copy 在CODE上查看代码片派生到我的代码片
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Scroller;
public class CustomView extends LinearLayout {
private static final String TAG = "Scroller";
private OverScroller mScroller;
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
mScroller = new OverScroller(context);
}
//调用此方法滚动到目标位置
public void smoothScrollTo(int fx, int fy) {
int dx = fx - mScroller.getFinalX();
int dy = fy - mScroller.getFinalY();
smoothScrollBy(dx, dy);
}
//调用此方法设置滚动的相对偏移
public void smoothScrollBy(int dx, int dy) {
//设置mScroller的滚动偏移量
mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);
invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果
}
@Override
public void computeScroll() {
//先判断mScroller滚动是否完成
if (mScroller.computeScrollOffset()) {
//这里调用View的scrollTo()完成实际的滚动
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
//必须调用该方法,否则不一定能看到滚动效果
postInvalidate();
}
super.computeScroll();
}
}
VelocityTracker
VelocityTracker是一个什么东西呢,查看VelocityTracker源代码发现有如下注释:
[html] view plain
copy
print ?
- /**
- * Helper for tracking the velocity of touch events, for implementing
- * flinging and other such gestures.
- *
- * Use {@link #obtain} to retrieve a new instance of the class when you are going
- * to begin tracking. Put the motion events you receive into it with
- * {@link #addMovement(android.view.MotionEvent)}. When you want to determine the velocity call
- * {@link #computeCurrentVelocity(int)} and then call {@link #getXVelocity(int)}
- * and {@link #getYVelocity(int)} to retrieve the velocity for each pointer id.
- *
- * 追踪触摸事件速率,实现flinging和其他手势的帮助类
- *
- * 1、当开始追踪的时候,使用obtain来获取VelocityTracker类的实例
- * 2、把接收到的MotionEvent放入到addMovement(android.view.MotionEvent)中
- * 3、当要确定速度时调用computeCurrentVelocity(int),
- * 使用getXVelocity(int)和getYVelocity(int)来检测每个触摸点id的速率
- */
VelocityTracker是一个帮助追踪触摸事件速率的追踪器,可以追踪fliinging和其他触摸手势。
如何使用VelocityTracker呢,注释中提到如下步骤:
[html] view plain
copy
print ?
- 1、当开始追踪的时候,使用obtain来获取VelocityTracker类的实例
- 2、把接受到的MotionEvent放入到addMovement(android.view.MotionEvent)中
- 3、当要确定速度时调用computeCurrentVelocity(int),
- 使用getXVelocity(int)和getYVelocity(int)来检测每个触摸点id的速率
既然VelocityTracker是追踪触摸事件的速度追踪器,当然需要与触摸事件结合使用。
第一步:当你想要追踪触摸事件的速度时,使用private VelocityTracker mVelocityTracker = VelocityTracker.obtain();来获取一个实例,
obtain()方法的源代码:
[java] view plain
copy
print ?
-
-
-
-
-
-
-
-
-
-
-
-
- static public VelocityTracker obtain() {
- VelocityTracker instance = sPool.acquire();
- return (instance != null) ? instance : new VelocityTracker(null);
- }
第二步:把接收到的MotionEvent放入到addMovement(MotionEvent event)方法中,
在初始化MotionEvent的ACTION_DOWN时调用addMovement(MotionEvent event)
然后在ACTION_MOVE和ACTION_UP动作中就可以检测到速度了。
[java] view plain
copy
print ?
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void addMovement(MotionEvent event) {
- if (event == null) {
- throw new IllegalArgumentException("event must not be null");
- }
- nativeAddMovement(mPtr, event);
- }
第三步:想要确定速度的时候调用computeCurrentVelocity(int units)方法。有两个同样的方法,一个带有最大值参数,默认为Float.MAX_VALUE
[java] view plain
copy
print ?
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void computeCurrentVelocity(int units, float maxVelocity) {
- nativeComputeCurrentVelocity(mPtr, units, maxVelocity);
- }
[java] view plain
copy
print ?
-
-
-
-
-
-
-
-
- public void computeCurrentVelocity(int units) {
- nativeComputeCurrentVelocity(mPtr, units, Float.MAX_VALUE);
- }
第四步:使用getXVelocity()和getYVelocity()方法获取检测到的速度
获取X轴方向的速度:
[java] view plain
copy
print ?
-
-
-
-
-
-
-
- public float getXVelocity() {
- return nativeGetXVelocity(mPtr, ACTIVE_POINTER_ID);
- }
获取Y轴方向上的速度:
[java] view plain
copy
print ?
-
-
-
-
-
-
-
- public float getYVelocity() {
- return nativeGetYVelocity(mPtr, ACTIVE_POINTER_ID);
- }
第五步:回收VelocityTracker实例
[java] view plain
copy
print ?
-
-
-
-
-
-
-
- public void recycle() {
- if (mStrategy == null) {
- clear();
- sPool.release(this);
- }
- }
小实例:
[java] view plain
copy
print ?
- package com.zwc.admin.scrollerdemo;
-
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.VelocityTracker;
-
-
- public class VelocityTrackerTestActivity extends ActionBarActivity {
-
- private static final String TAG = "VelocityTrackerTestActivity";
-
- private VelocityTracker mVelocityTracker;
- private int mPointerId;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_velocity_tracker_test);
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- int action = event.getAction();
- if (null == mVelocityTracker) {
- mVelocityTracker = VelocityTracker.obtain();
- }
- mVelocityTracker.addMovement(event);
-
- switch (action) {
- case MotionEvent.ACTION_DOWN:
-
- mPointerId = event.getPointerId(0);
- break;
-
- case MotionEvent.ACTION_MOVE:
- mVelocityTracker.computeCurrentVelocity(1000);
- float xVelocity = mVelocityTracker.getXVelocity(event.getPointerId(mPointerId));
- float yVelocity = mVelocityTracker.getYVelocity(event.getPointerId(mPointerId));
- Log.e(TAG, "xVelocity = " + xVelocity + ",yVelocity = " + yVelocity);
- break;
-
- case MotionEvent.ACTION_UP:
- case MotionEvent.ACTION_CANCEL:
- if (null != mVelocityTracker) {
- mVelocityTracker.recycle();
- }
- break;
- }
-
- return super.onTouchEvent(event);
- }
-
- }