您的位置:首页 > 移动开发 > Android开发

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

2015-03-27 14:53 603 查看
图解AndroidView的scrollTo(),scrollBy(),getScrollX(),getScrollY()-bigconvience的专栏-博客频道-CSDN.NET


Android系统手机屏幕的左上角为坐标系,同时y轴方向与笛卡尔坐标系的y轴方向想反。通过提供的api如getLeft,getTop,getBottom,getRight可以获得控件在parent中的相对位置。同时,也可以获得控件在屏幕中的绝对位置,详细用法可参考android应用程序中获取view的位置

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

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

调用publicvoidoffsetLeftAndRight(intoffset)用于左右移动方法或publicvoidoffsetTopAndBottom(intoffset)用于上下移动。

如:button.offsetLeftAndRignt(300)表示将button控件向左移动300个像素。

scrollTo(intx,inty)是将View中内容滑动到相应的位置,参考的坐标系原点为parentView的左上角。

调用scrollTo(100,0)表示将View中的内容移动到x=100,y=0的位置,如下图所示。注意,图中黄色矩形区域表示的是一个parentView,绿色虚线矩形为parentview中的内容。一般情况下两者的大小一致,本文为了显示方便,将虚线框画小了一点。图中的黄色区域的位置始终不变,发生位置变化的是显示的内容。



同理,scrollTo(0,100)的效果如下图所示:



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



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



scrollBy(intx,inty)其实是对scrollTo的包装,移动的是相当位置。scrollTo(intx,inty)的源码和scrollBy(intx,inty)源码如下所示.

[java]viewplaincopyprint?





/**

*Movethescrolledpositionofyourview.Thiswillcauseacallto

*{@link#onScrollChanged(int,int,int,int)}andtheviewwillbe

*invalidated.

*@paramxtheamountofpixelstoscrollbyhorizontally<prename="code"class="java">/**

*Setthescrolledpositionofyourview.Thiswillcauseacallto

*{@link#onScrollChanged(int,int,int,int)}andtheviewwillbe

*invalidated.

*@paramxthexpositiontoscrollto

*@paramytheypositiontoscrollto

*/

publicvoidscrollTo(intx,inty){

if(mScrollX!=x||mScrollY!=y){

intoldX=mScrollX;

intoldY=mScrollY;

mScrollX=x;

mScrollY=y;

invalidateParentCaches();

onScrollChanged(mScrollX,mScrollY,oldX,oldY);

if(!awakenScrollBars()){

postInvalidateOnAnimation();

}

}

}

/**
*Movethescrolledpositionofyourview.Thiswillcauseacallto
*{@link#onScrollChanged(int,int,int,int)}andtheviewwillbe
*invalidated.
*@paramxtheamountofpixelstoscrollbyhorizontally<prename="code"class="java">/**
*Setthescrolledpositionofyourview.Thiswillcauseacallto
*{@link#onScrollChanged(int,int,int,int)}andtheviewwillbe
*invalidated.
*@paramxthexpositiontoscrollto
*@paramytheypositiontoscrollto
*/
publicvoidscrollTo(intx,inty){
if(mScrollX!=x||mScrollY!=y){
intoldX=mScrollX;
intoldY=mScrollY;
mScrollX=x;
mScrollY=y;
invalidateParentCaches();
onScrollChanged(mScrollX,mScrollY,oldX,oldY);
if(!awakenScrollBars()){
postInvalidateOnAnimation();
}
}
}


[java]viewplaincopyprint?





/*@paramytheamountofpixelstoscrollbyvertically*/

/*@paramytheamountofpixelstoscrollbyvertically*/


[java]viewplaincopyprint?





publicvoidscrollBy(intx,inty){scrollTo(mScrollX+x,mScrollY+y);}

publicvoidscrollBy(intx,inty){scrollTo(mScrollX+x,mScrollY+y);}



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

理解了scrollTo(intx,inty)和scrollBy(intx,inty)的用法,就不难理解getScrollX()和getScrollY()。这两个函数的源码如下所示:

[java]viewplaincopyprint?





/**

*Returnthescrolledleftpositionofthisview.Thisistheleftedgeof

*thedisplayedpartofyourview.Youdonotneedtodrawanypixels

*fartherleft,sincethoseareoutsideoftheframeofyourviewon

*screen.

*

*@returnTheleftedgeofthedisplayedpartofyourview,inpixels.

*/

publicfinalintgetScrollX(){

returnmScrollX;

}

/**
*Returnthescrolledleftpositionofthisview.Thisistheleftedgeof
*thedisplayedpartofyourview.Youdonotneedtodrawanypixels
*fartherleft,sincethoseareoutsideoftheframeofyourviewon
*screen.
*
*@returnTheleftedgeofthedisplayedpartofyourview,inpixels.
*/
publicfinalintgetScrollX(){
returnmScrollX;
}


[java]viewplaincopyprint?





/**

*Returnthescrolledtoppositionofthisview.Thisisthetopedgeof

*thedisplayedpartofyourview.Youdonotneedtodrawanypixelsabove

*it,sincethoseareoutsideoftheframeofyourviewonscreen.

*

*@returnThetopedgeofthedisplayedpartofyourview,inpixels.

*/

publicfinalintgetScrollY(){

returnmScrollY;

}

/**
*Returnthescrolledtoppositionofthisview.Thisisthetopedgeof
*thedisplayedpartofyourview.Youdonotneedtodrawanypixelsabove
*it,sincethoseareoutsideoftheframeofyourviewonscreen.
*
*@returnThetopedgeofthedisplayedpartofyourview,inpixels.
*/
publicfinalintgetScrollY(){
returnmScrollY;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
章节导航