Android中的Scroller类

Scroller在Android中用于实现平滑滚动效果,它与invalidate()、scrollTo()和scrollBy()配合使用。博客介绍了如何利用Scroller启动滚动动作、计算过渡位置和判断滚动结束,以及如何在自定义View中结合computeScroll()方法实现滚动动画。同时,强调了在使用scrollTo()和scrollBy()时的注意事项,例如它们对父容器和子控件的影响以及滚动方向的参数设置。
摘要由CSDN通过智能技术生成

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"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值