关于android中scroller类源码浅析

来源:互联网 发布:阿里云最便宜多少钱 编辑:程序博客网 时间:2024/06/02 04:40
首先,来看一看Google关于Scroller类的API是怎样描述的:

This class encapsulates scrolling.  The duration of the scroll can be passed in the constructor and specifies the maximum time that the scrolling animation should take.  Past this time, the scrolling is automatically moved to its final stage and computeScrollOffset() will always return false to indicate that scrolling is over.

从这段说明可以看出几点:

1、Scroller这个类是关于滑动的一个封装类。

2、滑动时间可以在构造里明确最大滑动时间,在这个时间内去执行动画。

3、超过最大时间,将自动滑动到最终状态,computeScrollOffset()这个方法将一直返回false,表示滑动已经结束。

接下来,看一看Scroller的三种构造函数:

第一种:构造时不指定,使用默认的时间和减速器。

[html] view plaincopy
  1. <span style="font-size:14px;">   /**  
  2.      * Create a Scroller with the default duration and interpolator.</span>  
[html] view plaincopy
  1. <span style="font-size:14px;">     */  
  2.     public Scroller(Context context) {  
  3.         this(context, null);  
  4.     }</span>  
第二种:构造时指定明确的减速器。如果传空,则使用默认的。注意:Flywheel效应应在至少API11才会起作用。
[html] view plaincopy
  1. <span style="font-size:14px;"> /**  
  2.      * Create a Scroller with the specified interpolator. If the interpolator is  
  3.      * null, the default (viscous) interpolator will be used. "Flywheel" behavior will  
  4.      * be in effect for apps targeting Honeycomb or newer.  
  5.      */  
  6.     public Scroller(Context context, Interpolator interpolator) {  
  7.         this(context, interpolator,  
  8.                 context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB);  
  9.     }</span>  

第三种:构造时指定明确的减速器。如果传空,则使用默认的。并且明确是否支持较新Flywheel效应。

[html] view plaincopy
  1. <span style="font-size:14px;">    /**  
  2.      * Create a Scroller with the specified interpolator. If the interpolator is  
  3.      * null, the default (viscous) interpolator will be used. Specify whether or  
  4.      * not to support progressive "flywheel" behavior in flinging.  
  5.      */  
  6.     public Scroller(Context context, Interpolator interpolator, boolean flywheel) {  
  7.         mFinished = true;  
  8.         mInterpolator = interpolator;  
  9.         mPpi = context.getResources().getDisplayMetrics().density * 160.0f;  
  10.         mDeceleration = computeDeceleration(ViewConfiguration.getScrollFriction());  
  11.         mFlywheel = flywheel;  
  12.     }</span>  

下面分析Scroller中的主要方法:

1、setFriction(float)

设置手指摩擦力相关,一般很少用到。

2、isFinished()

判断scroller是否结束了滑动

3、forceFinished(boolean finished)

强制设置是否结束标志为你想要的值,一般在你想限制滑动最大距离时调用

4、getDuration()

返回滑动时间所消耗的时间,单位毫秒

5、getCurrX(),getCurrY()

返回当前X,Y轴方向的偏移量

6、getStartX(),getStartY()

返回X,Y轴方向的初始偏移量

7、getFinalX(),getFinalY()

返回滑动在哪里结束,只对手指滑动有效

8、getCurrVelocity()

返回当前速率,如果出事速率小于减速器可能会返回负值

9、computeScrollOffset()

当你想知道滑动的新位置时调用这个方法。如果返回true,则表示动画还没有结束。新的位置将替代旧位置,即画面会滑动。

10、startScroll(int startX,int startY,int dx,int dy)

四个参数前两个是起始位置坐标,后两个是从起始坐标计算要移动的距离。如果dx和dy是负数,则会往左侧滑动。

通过四个参数滑动界面,持续时间使用默认值250毫秒

11、startScroll(int,int,int,int,int)

同10方法,只不过可以指定滑动时间

12、fling(int startX,int startY,int velocityX,int velocityY,int minX,int maxX,int minY,int maxY)

基于一个滑动手势开始滑动,滑动距离依赖于向前滑动的初始速率

startX,startY起始坐标

velocityX,velocityY根据每秒移动的像素点来计算出的初始速度

minX,maxX坐标轴X上的最小和最大值

minY,maxY坐标轴Y上的最小和最大值

13、abortAnimation()

停止活跃。和forceFinished方法不同,这个方法会设置是否完成标志位为true,然后移动到finalX和finalY位置,以结束滑动

14、extendDuration(int extend)

延长滑动动画时间,它允许一个正在滑动的动画滑动时间更长,当你调用setFinalX()和setFinalY()方法设置时。

15、timePassed()

返回从滑动开始的时候到现在所消耗的时间

16、setFinalX(int newX),setFinalY(int newY)

设置新的滑动的最终坐标


在Google的封装类中,Scroller类是一个较为简单的类,但非常的实用,想用好它应该对View类有更深刻的理解,由于水平有限,仅作参考,如有不足之处望指正。

原创粉丝点击