View 平滑移动

来源:互联网 发布:多线程编程书籍 编辑:程序博客网 时间:2024/05/16 13:46

实现View滑动有多种方式,比如使用动画,动态改变margin值等,本文暂时只讨论使用scrollTo/scrollBy方式,结合Scroller类,实现view的平滑移动。

View内部实现了定义了scrollTo,scrollBy方法

    /**     * 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     * 设置view的滚动位置,会调用onScroolChanded(),并使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();            }        }    }
/**     * 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);    }

scrollBy()内部会调用scrollTo();

在View.java中提供了了如下两个变量以及相应的属性方法去读取滚动值 ,如下: View.java类中

/**      * The offset, in pixels, by which the content of this view is scrolled      * horizontally.      * {@hide}      */      protected int mScrollX;   //该视图内容相当于视图起始坐标的偏移量   , X轴 方向      /**      * The offset, in pixels, by which the content of this view is scrolled      * vertically.      * {@hide}      */      protected int mScrollY;   //该视图内容相当于视图起始坐标的偏移量   , Y轴方向      /**      * Return the scrolled left position of this view. This is the left edge of      * the displayed part of your view. You do not need to draw any pixels      * farther left, since those are outside of the frame of your view on      * screen.      *      * @return The left edge of the displayed part of your view, in pixels.      */      public final int getScrollX() {          return mScrollX;      }      /**      * Return the scrolled top position of this view. This is the top edge of      * the displayed part of your view. You do not need to draw any pixels above      * it, since those are outside of the frame of your view on screen.      *      * @return The top edge of the displayed part of your view, in pixels.      */      public final int getScrollY() {          return mScrollY;      }  

public void scrollTo(int x, int y)
说明:在当前视图内容偏移至(x , y)坐标处,即显示(可视)区域位于(x , y)坐标处。

public void scrollBy(int x, int y)
说明:在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位。

平滑移动的实现:

1 自定义View MyView.java

public class MyView extends View

2 new Scroller对象

Scroller mScroller = new Scroller(getContext());

3 view中重写computeScroll()方法

    @Override    public void computeScroll() {        if (!mScroller.computeScrollOffset()){            scrollTo(mScroller.getCurrX(),mScroller.getCurrY());            postInvalidate();        }    }

4 实现smoothScrollBy(int destX, int destY);

    public void smothScrollBy(int destX, int destY){        mScroller.startScroll(getScrollX(), getScrollY(), destX, destY, 400);        invalidate();    }

需要注意的是,view 的 ScrollTo平移的是view的内容,不能改变view在父容器中的位置

0 1