zoukankan      html  css  js  c++  java
  • View:Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()的理解

    Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向想反。提供了

    getLeft(), 
    getTop(), 
    getBottom(), 
    getRight()

    这些API来获取控件在Parent中的相对位置

    同时也提供了

    getLocalVisibleRect()
    getGlobalVisibleRect()
    getLocationOnScreen()
    getLocationInWindow()

    这些API来获取控件在屏幕中的绝对位置。详情可参考:android应用程序中获取view的位置

    如果要将View中的内容滚动到相应到指定位置,可以使用这些API

    scrollTo()
    scrollBy()

    如果要改变整个View在屏幕中的位置,可以使用下列API:

    offsetLeftAndRight(int offset) // 用于左右移动
    offsetTopAndBottom(int offset) // 用于上下移动

    下面简要总结一下scrollTo(),scrollBy(),getScrollX(), getScrollY()这些方法

    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的移动方向将相反。

     

    关于scrollTo()方法中参数值为正,却向左移动,参数值为负,却向右移动(这地方确实很怪)的一些理解

    scrollTo()方法本身滚动的是View的内容,View本身位置不变。可以将该View想象成一个带滚动条的窗体,我们以滚动条作为参照物

    当水平滚动条向右移动时,原本窗体显示的内容向左移动,比方说水平滚动条向右移动了100的距离,同样的窗体显示的内容就向左移动了100的距离,这个时候也就是scrollTo(100, 0);

    当滚动条向下移动时,原本窗体显示的内容应向上移动,比方说垂直滚动条向下移动了100的距离,同样的窗体显示的内容就向上移动了100的距离,这个时候也就是scrollTo(0, 100);

    这也就解释了为什么scrollTo()方法中参数大于0,View向左移动,参数小于0,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();
                }
            }
        }
    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;
        }
  • 相关阅读:
    TCP拥塞控制机制
    C/S 与 B/S 区别
    ext4文件系统新特性
    关于TCP慢启动和拥塞避免的题
    [信息图表]移动学习 Go Study
    [时间线]Android市场的发展
    学习,只有勤奋是不够的……
    UNIX域套接字实例
    程序员的本质
    淘宝Web服务器Tengine正式开源
  • 原文地址:https://www.cnblogs.com/yongdaimi/p/9685549.html
Copyright © 2011-2022 走看看