美文网首页
Android滚动工具类——Scroller

Android滚动工具类——Scroller

作者: WangSins | 来源:发表于2019-03-01 15:52 被阅读0次

滚动的基础

任何一个控件都可以通过View类当中的scrollTo()和scrollBy()这两个方法实现滚动。
scrollTo:滚动的基础。

/**
 * Set the scrolled position of your view. This will cause a call to
 * {@link #onScrollChanged(int, int, int, int)} and the view will be
 * invalidated.
 * @param x the x position to scroll to
 * @param y the y position to scroll to
 */
 //视图滚动到的位置
public void scrollTo(int x, int y) {
    // 判断x和y轴是否发生滚动
    if (mScrollX != x || mScrollY != y) {
        // 记录起始x轴位置
        int oldX = mScrollX;
        // 记录起始y轴位置
        int oldY = mScrollY;
        // 要滚动到的x轴位置
        mScrollX = x;
        // 要滚动到的y轴位置 
        mScrollY = y;
        // 强制清空父View缓存,并不会调用invalidate()
        invalidateParentCaches();
        // 调用此放法位置发生变化
        onScrollChanged(mScrollX, mScrollY, oldX, oldY);
        // 判断滚动动画是否启动,是true,否false
        // 如果动画启动会调用invalidate()
        if (!awakenScrollBars()) {
            // 动画发生无效
            postInvalidateOnAnimation();
        }
    }
}

scrollBy:网上有一句话叫做万物基于MIUI,通过scrollBy中的scrollTo可以看到Android所有滚动基于scrollTo。

/**
 * Move the scrolled position of your view. This will cause a call to
 * {@link #onScrollChanged(int, int, int, int)} and the view will be
 * invalidated.
 * @param x the amount of pixels to scroll by horizontally
 * @param y the amount of pixels to scroll by vertically
 */
public void scrollBy(int x, int y) {
    scrollTo(mScrollX + x, mScrollY + y);
}

Scroller是什么?

Scroller是一个专门用于处理滚动效果的工具类,ViewPager、ListView等控件在内部都是使用Scroller来实现的,下面有一段来自官方的介绍...

This class encapsulates scrolling. You can use scrollers ({@link Scroller}or {@link OverScroller}) to collect the data you need to produce a scrolling animation—for example, in response to a fling gesture. Scrollers track scroll offsets for you over time, but they don't automatically apply those positions to your view. It's your responsibility to get and apply new coordinates at a rate that will make the scrolling animation look smooth.

使用案例

官方案例

private Scroller mScroller = new Scroller(context);
...
public void zoomIn() {
    // 还原正在进行的任何动画
    mScroller.forceFinished(true);
    // 提供X和Y轴的起点和滑动距离开始滚动
    // startScroll有一个5个参数的重载方法,最后一个参数是滚动持续时间
    mScroller.startScroll(0, 0, 100, 0);
    // 请求重绘
    invalidate();
}

基本用法

private Scroller mScroller = new Scroller(mContext);

private void smoothScroll(int destX, int destY) {
    // 获取当前x轴位置
    int scrollX = getScrollX();
    // 计算x轴相对于当前位置发生的偏移量
    int deltaX = destX - scrollX;
    // 发生滚动,x轴从当前位置滚动的位移量为deltaX
    mScroller.startScroll(scrollX, 0, deltaX, 0, 500);
    // 强制重绘
    invalidate();
}

@Override
public void computeScroll() {
    super.computeScroll();
    // 滑动是否终止
    if (mScroller.computeScrollOffset()) {
    // 重置x和y轴位置
        scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
        // 强制重绘
        invalidate();
    }
}

computeScroll:准备滑动在View中是空实现,需要我们自己去重写这个方法重置x和y坐标


/**
 * Called by a parent to request that a child update its values for mScrollX
 * and mScrollY if necessary. This will typically be done if the child is
 * animating a scroll using a {@link android.widget.Scroller Scroller}
 * object.
 */
public void computeScroll() {
}

当我们要跟踪X/y轴偏移量时

To track the changing positions of the x/y coordinates, use
{@link #computeScrollOffset}. The method returns a boolean to indicate
whether the scroller is finished. If it isn't, it means that a fling or
programmatic pan operation is still in progress. You can use this method to
find the current offsets of the x and y coordinates, for example:

// 判断滚动是否完成,完成返回true
if (mScroller.computeScrollOffset()) {
    // 获取当前的X坐标
    int currX = mScroller.getCurrX();
    // 获取当前的Y坐标
    int currY = mScroller.getCurrY();
    ...
}

相关文章

  • 初识Scroller

    Scroller Scroller是一个专门用于处理滚动效果的工具类,例如ViewPager,ListView等控...

  • Android滚动工具类——Scroller

    滚动的基础 任何一个控件都可以通过View类当中的scrollTo()和scrollBy()这两个方法实现滚动。s...

  • 翻滚吧!Scroller

    引言:Scroller类是一个比较有趣的Android原生类,可译为“滚动器”。我们经常会在App中使用到滚动的功...

  • 安卓自定义View之滑动

    Scroller的基础知识 Scroller类其实并不负责“滚动”这个动作,“滚动”的动作[1]是由基类View的...

  • Scroller源码分析

    作用成员方法使用注意替补选手 OverScroller 1. 作用 Scroller是一个滚动的计算工具类,他根据...

  • 高级UI<第四十四篇>:Android Scroller详解

    滚动视图的方法有两种:scrollTo和scrollBy,而Scroller就是它们的辅助工具类,所以Scroll...

  • Scroller的使用及解析

    Android开发中View的滑动处理源码,该怎么看? Android 带你从源码的角度解析Scroller的滚动...

  • android 的scroller的使用

    理解 我把scroller理解为滚动数据携带器,他只是一个记录滚动数据的工具,并不显示view的滚动效果,其实这点...

  • Android 使用 Scroller 实现平滑滚动

    记录使用Scroller实现平滑滚动,效果图如下: 一、自定义View中实现View的平滑滚动 Scroller其...

  • 深入解析Scroller滚动原理

    博文出处:深入解析Scroller滚动原理,欢迎大家关注我的博客,谢谢! 最近在看《Android开发艺术探索》这...

网友评论

      本文标题:Android滚动工具类——Scroller

      本文链接:https://www.haomeiwen.com/subject/ddgduqtx.html