图解Android View的scrollTo(),scrollBy(),getScrollX(), getScrollY()
2015-06-02 21:47
591 查看
Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向想反。通过提供的api如getLeft , getTop, getBottom, getRight可以获得控件在parent中的相对位置。同时,也可以获得控件在屏幕中的绝对位置,详细用法可参考android应用程序中获取view的位置
当我们编写一些自定义的滑动控件时,会用到一些api如scrollTo(),scrollBy(),getScrollX(), getScrollY()。由于常常会对函数getScrollX(), getScrollY()返回的值的含义产生混淆,尤其是正负关系,因此本文将使用几幅图来对这些函数进行讲解以方便大家记忆。
注意:调用View的scrollTo()和scrollBy()是用于滑动View中的内容,而不是把某个View的位置进行改变。如果想改变莫个View在屏幕中的位置,可以使用如下的方法。
调用public void offsetLeftAndRight(int offset)用于左右移动方法或public void offsetTopAndBottom(int
offset)用于上下移动。
如:button.offsetLeftAndRignt(300)表示将button控件向左移动300个像素。
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)源码如下所示.
[java] view
plaincopy
/**
* 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();
}
}
}
[java] view
plaincopy
/* @param y the amount of pixels to scroll by vertically */
[java] view
plaincopy
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()。这两个函数的源码如下所示:
[java] view
plaincopy
/**
* 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;
}
[java] view
plaincopy
/**
* 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;
}
——————————————————————————————————————————————————————————
(1)关键:scrollTo(int x, int y) 是将View中内容滑动到相应的位置,参考的坐标系原点为parent
View的左上角
(2)垂直上下滑的关系
当我们编写一些自定义的滑动控件时,会用到一些api如scrollTo(),scrollBy(),getScrollX(), getScrollY()。由于常常会对函数getScrollX(), getScrollY()返回的值的含义产生混淆,尤其是正负关系,因此本文将使用几幅图来对这些函数进行讲解以方便大家记忆。
注意:调用View的scrollTo()和scrollBy()是用于滑动View中的内容,而不是把某个View的位置进行改变。如果想改变莫个View在屏幕中的位置,可以使用如下的方法。
调用public void offsetLeftAndRight(int offset)用于左右移动方法或public void offsetTopAndBottom(int
offset)用于上下移动。
如:button.offsetLeftAndRignt(300)表示将button控件向左移动300个像素。
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)源码如下所示.
[java] view
plaincopy
/**
* 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();
}
}
}
[java] view
plaincopy
/* @param y the amount of pixels to scroll by vertically */
[java] view
plaincopy
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()。这两个函数的源码如下所示:
[java] view
plaincopy
/**
* 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;
}
[java] view
plaincopy
/**
* 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;
}
——————————————————————————————————————————————————————————
(1)关键:scrollTo(int x, int y) 是将View中内容滑动到相应的位置,参考的坐标系原点为parent
View的左上角
(2)垂直上下滑的关系
/** * 根据用户滑动,判断用户的意图是否是滚动到下一页 * * @return */ private boolean wantScrollToNext() { return mScrollEnd > mScrollStart; }
/** * 根据滚动距离判断是否能够滚动到上一页 * * @return */ private boolean shouldScrollToPre() { return -mScrollEnd + mScrollStart > mScreenHeight / 2 || Math.abs(getVelocity()) > 600; }
相关文章推荐
- java4android(5)面向对象、类的定义、生成对象的方法、使用对象的方法、生成多个对象、构造函数super()、函数重载、static、继承
- Android手机分辨率基础知识(DPI,DIP计算)三
- Android Volley完全解析(四),带你从源码的角度理解Volley
- Android手机分辨率基础知识(DPI,DIP计算)二
- Android Volley完全解析(一),初识Volley的基本用法
- android 流量监测
- Android漂亮的对话框项目sweet-alert-dialog
- Android漂亮的对话框项目sweet-alert-dialog
- Android+Sqlite 实现古诗阅读应用(三)
- Android(java)学习笔记97:Scanner类使用
- Android与MVC设计模式
- android apk 反破解手段
- Android的ProgressBar进度条-android学习之旅(三十一)
- android 带清除功能的EditText
- Android的ProgressBar进度条-android学习之旅(三十一)
- Android的ProgressBar进度条-android学习之旅(三十一)
- Android中,控件下按效果的两种实现方式!
- 关于goole IO大会发布的android M和android studio1.3的更新
- Android底层驱动开发(一)
- Android之NDK开发