View的scrollTo(),scrollBy()以及Scroller,OverScroller

来源:互联网 发布:java高并发高可用 编辑:程序博客网 时间:2024/05/16 05:00

  在View中有scrollTo()和scrollBy()方法,这两个方法其实是一样的,只是scrollTo是直接滑动到制定的位置,而scrollBy是滑动相对的距离。这两个方法可以实现View内容的移动,而不是位置。

要想滚动位置,需要结合Scroller或者OverScroller,配合起来实现动态可控的滑动效果。

Scroller和OverScroller,这两个是AndroidUI框架下实现滚动效果的最关键的类,ScrollView内部的实现也是使用OverScroller。

<span style="color:#333333;">     /**     * Start scrolling by providing a starting point and the distance to travel.     * The scroll will use the default value of 250 milliseconds for the     * duration.     *     * @param startX Starting horizontal scroll offset in pixels. Positive     *        numbers will scroll the content to the left.     * @param startY Starting vertical scroll offset in pixels. Positive numbers     *        will scroll the content up.     * @param dx Horizontal </span><span style="color:#ff0000;">distance </span><span style="color:#333333;">to travel. Positive numbers will scroll the     *        content to the left.     * @param dy Vertical </span><span style="color:#ff0000;">distance </span><span style="color:#333333;">to travel. Positive numbers will scroll the     *        content up.     */    public void startScroll(int startX, int startY, int dx, int dy) {        startScroll(startX, startY, dx, dy, DEFAULT_DURATION);    }</span>
注意这里的dx和dy,不要误以为是目标位置,它们表示的都是距离,这里的距离也是有方向的,和getScrollX的方向一致。

最后说说computeScroll,当调用startScroll方法后,scroller并不是直接包办一切,它只是帮助你计算每次移动的偏移量,你需要重写computeScroll方法,并在里面处理移动。

0 0
原创粉丝点击