Scroller
https://developer.android.com/reference/android/widget/Scroller
java.lang.Object
↳ android.widget.Scroller
此类封装了滚动。您可以使用滚动条(Scroller或OverScroller)来收集生成滚动动画所需的数据,用来响应一个挥动手势。滚动条会随着时间的推移为您跟踪滚动偏移量,但不会自动将这些位置应用于您的视图。您有责任使用某个速率将视图平滑地滚动到新坐标。
abortAnimation ():停止动画。与forceFinished(boolean)相反,中止动画会导致滚动条移动到最终的x和y位置
forceFinished (boolean finished):将完成的字段强制为特定值。
computeScrollOffset ():当您想知道新的位置时调用。动画尚未完成时返回true。
getCurrX ():返回滚动中的当前X偏移量。新的X偏移量是距视图原点的绝对距离。
View
computeScroll ():由父级调用,以请求子级在必要时更新其mScrollX和mScrollY的值。通常在子级使用Scroller对象设置滚动动画(Scroller.startScroll)时会被调用。
invalidate ():使整个视图无效。如果该视图可见,则将来会在某个时候调用onDraw(android.graphics.Canvas)。必须从UI线程调用此方法。要从非UI线程进行调用,请调用postInvalidate()。
postInvalidate ():使用它可以使来自非UI线程的视图无效。仅当此View附加到窗口时,才可以从UI线程外部调用此方法。
getScrollX ():返回此视图的向左滚动位置。这是视图显示部分的左边缘。您无需在屏幕的最左边绘制任何像素,因为这些像素不在屏幕上显示。
getScrollY ():返回此视图的滚动顶部位置。这是视图显示部分的顶部边缘。您无需在其上方绘制任何像素,因为这些像素不在屏幕视图框架之内。
VelocityTracker
追踪滑动速度。
获取一个实例:
VelocityTracker valocityTracker = VelocityTracker.obtain();
使用:添加要追踪的事件,设置计算间隔,获取 x/y 方向速度
valocityTracker.addMovement(event); // 添加 MotionEvent 事件
valocityTracker.computeCurrentVelocity(1000); // 计算的时间间隔
float xVelocity = valocityTracker.getXVelocity(); // x 方向速度
float yVelocity = valocityTracker.getYVelocity(); // y 方向速度
每次使用后清除:
valocityTracker.clear();
不用时回收:
valocityTracker.recycle();
示例
public class HorizontalScrollView extends ViewGroup {
private int mChildrenSize;
private int mChildWidth;
private int mChildIndex; // 记录当前子项 Index
// 记录最近滑动的坐标
private int mLastX = 0;
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
public HorizontalScrollView(Context context) {
super(context);
init();
}
public HorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public HorizontalScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
// 初始化控件,获得 Scroller 和 VelocityTracker 实例
private void init() {
if (mScroller == null) {
mScroller = new Scroller(getContext());
mVelocityTracker = VelocityTracker.obtain();
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
boolean intercepted = false;
mVelocityTracker.addMovement(event); // 添加要追踪的事件
int x = (int) ev.getX();
mLastX = x; // 更新上次滑动坐标
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
intercepted = false;
if (!mScroller.isFinished()) { // 中止上次滑动未完成动画,直接移动到最终 x 和 y 的位置
mScroller.abortAnimation();
}
break;
case MotionEvent.ACTION_MOVE:
// 使用横向和纵向速度判断横向滑动还是纵向滑动
mVelocityTracker.computeCurrentVelocity(1000);
float xVelocity = mVelocityTracker.getXVelocity();
float yVelocity = mVelocityTracker.getYVelocity();
if (Math.abs(xVelocity) >= Math.abs(yVelocity)) { // 横向滑动
intercepted = true;
} else {
intercepted = false;
}
mVelocityTracker.clear();
break;
case MotionEvent.ACTION_UP:
intercepted = false;
break;
default:
break;
}
return intercepted;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mVelocityTracker.addMovement(event); // 添加要追踪的事件
// 此次滑动的坐标
int x = (int) event.getX();
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int deltaX = x - mLastX;
scrollBy(-deltaX, 0); // 滑动方向与deltaX反向
break;
case MotionEvent.ACTION_UP:
int scrollX = getScrollX(); // 此视图的向左滚动的位置,即左边界坐标
mVelocityTracker.computeCurrentVelocity(1000);
float xVelocity = mVelocityTracker.getXVelocity();
if (Math.abs(xVelocity) >= 50) { // 速度大于 50 认定为是快速切换,子项 Index 自增或自减
mChildIndex = xVelocity > 0? mChildIndex - 1: mChildIndex + 1;
} else { // 速度小于 50,则 scrollX % mChildWidth > 二分之 mChildWidth,即滑动距离超过子项视图宽度一半时才切换,Index 向下取整
mChildIndex = (scrollX + mChildWidth / 2) / mChildWidth;
}
mChildIndex = Math.max(0, Math.min(mChildIndex, mChildrenSize - 1)); // 防止 Index 溢出
mVelocityTracker.clear();
int dx = mChildIndex * mChildWidth - scrollX; // 弹性滑动,补充不足的滑动距离或返回滑动的距离到回原处
// 按提供的起点、行驶距离、滚动持续时间(以毫秒为单位)开始滚动
mScroller.startScroll(scrollX, 0, dx, 0, 500);
// 使视图无效以要求重画
invalidate();
break;
default:
break;
}
mLastX = x; // 更新滑动坐标
return true;
}
// 在本视图使用Scroller对象设置滚动动画时,会被父级调用来更新本视图 mScrollX 和 mScrollY 的值
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) { // 动画未结束时返回 true,可以获得当前计算后的 X/Y 坐标
scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); // 滑动到当前 X/Y 坐标
postInvalidate(); // 非 UI 线程调用来使视图无效重画
}
}
@Override
protected void onDetachedFromWindow() {
mVelocityTracker.recycle(); // VelocityTracker 对象的回收
super.onDetachedFromWindow();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measureWidth = 0;
int measureHeight = 0;
final int childCount = getChildCount();
measureChildren(widthMeasureSpec, heightMeasureSpec);
int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
if (childCount == 0) {
setMeasuredDimension(0, 0);
} else if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
final View childView = getChildAt(0);
measureWidth = childView.getMeasuredWidth() * childCount;
measureHeight = childView.getMeasuredHeight();
setMeasuredDimension(measureWidth, measureHeight);
} else if (widthSpecMode == MeasureSpec.AT_MOST) {
final View childView = getChildAt(0);
measureWidth = childView.getMeasuredWidth() * childCount;
setMeasuredDimension(measureWidth, heightMeasureSpec);
} else if (heightSpecMode == MeasureSpec.AT_MOST) {
final View childView = getChildAt(0);
measureHeight = childView.getMeasuredHeight();
setMeasuredDimension(widthMeasureSpec, measureHeight);
}
}
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
int childLeft = 0;
final int childCount = getChildCount();
mChildrenSize = childCount;
for (int j = 0; j < childCount; j++) {
final View childView = getChildAt(j);
if (childView.getVisibility() != View.GONE) {
final int childWidth = childView.getMeasuredWidth();
mChildWidth = childWidth;
// 从 0 开始,隔 childWidth 放置下一个 childView
childView.layout(childLeft, 0, childLeft + childWidth, childView.getMeasuredHeight());
childLeft += childWidth;
}
}
}
}