码迷,mamicode.com
首页 > 移动开发 > 详细

图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()

时间:2017-04-24 21:09:51      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:理解   编写   jsb   nts   ace   ons   lin   cache   use   

Android系统手机屏幕的左上角为坐标系,同一时候y轴方向与笛卡尔坐标系的y轴方向想反。通过提供的api如getLeft , getTopgetBottomgetRight能够获得控件在parent中的相对位置。同一时候。也能够获得控件在屏幕中的绝对位置,具体使用方法可參考android应用程序中获取view的位置

当我们编写一些自己定义的滑动控件时,会用到一些api如scrollTo(),scrollBy(),getScrollX(), getScrollY()。因为经常会对函数getScrollX(), getScrollY()返回的值的含义产生混淆,尤其是正负关系,因此本文将使用几幅图来对这些函数进行解说以方便大家记忆。

注意:调用View的scrollTo()和scrollBy()是用于滑动View中的内容。而不是把某个View的位置进行改变。假设想改变莫个View在屏幕中的位置,能够使用例如以下的方法。

调用public void offsetLeftAndRight(int offset)用于左右移动方法或public void 


scrollTo(int x, int y) 是将View中内容滑动到对应的位置。參考的坐标系原点为parent View的左上角。

       调用scrollTo(100, 0)表示将View中的内容移动到x = 100, y = 0的位置,例如以下图所看到的。注意。图中黄色矩形区域表示的是一个parent View,绿色虚线矩形为parent view中的内容。

普通情况下两者的大小一致,本文为了显示方便。将虚线框画小了一点。图中的黄色区域的位置始终不变。发生位置变化的是显示的内容。

技术分享

 同理,scrollTo(0, 100)的效果例如以下图所看到的:

技术分享

scrollTo(100, 100)的效果图例如以下:


技术分享

若函数中參数为负值。则子View的移动方向将相反。

技术分享

scrollBy(int x, int y)事实上是对scrollTo的包装,移动的是相当位置。 scrollTo(int x, int y)的源代码和scrollBy(int x, int y)源代码例如以下所看到的.

    /**
     * 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<pre name="code" class="java">    /**
     * 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) {
        if (mScrollX != x || mScrollY != y) {
            int oldX = mScrollX;
            int oldY = mScrollY;
            mScrollX = x;
            mScrollY = y;
            invalidateParentCaches();
            onScrollChanged(mScrollX, mScrollY, oldX, oldY);
            if (!awakenScrollBars()) {
                postInvalidateOnAnimation();
            }
        }
    }

/* @param y the amount of pixels to scroll by vertically */ 
public void scrollBy(int x, int y) { scrollTo(mScrollX + x, mScrollY + y); }



可见,mScrollX和mScrollY是View类中专门用于记录滑动位置的变量。这两个函数终于调用onScrollChanged()函数,感兴趣者能够參考他们的源码。

理解了scrollTo(int x, int y)和scrollBy(int x, int y)的使用方法。就不难理解getScrollX() 和getScrollY()。这两个函数的源代码例如以下所看到的:

    /**
     * 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;
    }


 

图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()

标签:理解   编写   jsb   nts   ace   ons   lin   cache   use   

原文地址:http://www.cnblogs.com/brucemengbm/p/6758885.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!