Scroller译为“滚动器”,是ViewGroup类中原生支持的一个功能。在Android中,如果一个控件需要实现滚动的功能,就需要用到Scroller类。在Android系统的控件中,比如ListView、ViewPager等都用到了。这篇博客就来学习一下Scroller类,并使用Scroller类和事件的分发写一个实例。
在说Scroller类之前,先说两组相关的API:
1.invalidate()和postInvalidate()
重载方法invalidate(int l, int t, int r, int b)、invalidate(Rectdirty)和postInvalidate(int left, int top, int right, int bottom)
invalidate()方法用于重绘组件,不带参数表示重绘整个视图区域,带参数表示重绘指定的区域。调用View的invalidate()方法就相当于调用了onDraw()方法,而onDraw()方法中就是我们编写的绘图代码。需要注意的是invalidate()方法只能在UI线程中调用,如果需要在子线程中刷新组件,那就需要调用View类另一组名为postInvalidate()的方法。
了解invalidate()方法实现重新绘制界面的过程,可以查看《invalidate()和requestLayout()方法调用过程》这篇博客
2.scrollTo()和ScrollBy()
这两个方法是在View类中定义的,也就说明了在Android中所有的空间都是可以滚动的,但是这两个方法有什么区别呢?不解释,我们先来看看这两个方法在View类中是怎样实现的。
public void scrollTo(int x, int y) {
if (mScrollX != x || mScrollY != y) {
int oldX = mScrollX;
int oldY = mScrollY;
mScrollX = x;
mScrollY = y;
invalidateParentCaches();
onScrollChanged(mScrollX, mScrollY, oldX, oldY);
if (!awakenScrollBars()) {
postInvalidateOnAnimation();
}
}
}
public void scrollBy(int x, int y) {
scrollTo(mScrollX + x, mScrollY + y);
}
通过对源码的查看,两个方法的区别就很容易发现了:
scrollTo(int x, int y)方法中,参数x、y是目标位置,该方法先判断新的滚动位置是否发生了变化,如果是,先保存上一次的位置,再应用新位置(x,y),接着调用onScrollChanged()方法,并调用postInvalidateOnAnimation()方法(该方法和invalidate()方法效果一样,只是postInvalidateOnAnimation()更加流畅,不会失帧)刷新View组件。scrollTo()方法表示滚动到指定位置。
scrollBy(int x, int y)方法则不同,是要原来的基础上水平方向滚动x个距离,垂直方向滚动y个距离,最终还是调用了scrollTo(int x, int y)方法。scrollBy()方法表示从某一点开始滚动指定距离。
下面用一个实例来展示一下他们两个方法的区别:
首先是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ScrollTo" />
<Button
android:id="@+id/bt_scrollto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
</LinearLayout>
<LinearLayout
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ScrollBy" />
<Button
android:id="@+id/bt_scrollby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"