Scroller

 

  一、结构

    public class Scroller extends Object

 

    java.lang.Object

      android.widget.Scroller

 

  二、概述

    这个类封装了滚动操作。滚动的持续时间可以通过构造函数传递,并且可以指定滚动动作的持续的最长时间。经过这段时间,滚动会自动定位到最终位置,并且通过computeScrollOffset()会得到的返回值为false,表明滚动动作已经结束。

 

  三、构造函数

  public Scroller (Context context)

  使用缺省的持续持续时间和动画插入器创建一个Scroller。(译者注:interpolator这里翻译为动画插入器,见这里。)

 

  public Scroller (Context context, Interpolator interpolator)

  根据指定的动画插入器创建一个Scroller,如果指定的动画插入器为空,则会使用缺省的动画插入器(粘滞viscous)创建。

 

  四、公共方法

  public void abortAnimation ()

  停止动画。与forceFinished(boolean)相反,Scroller滚动到最终x与y位置时中止动画。

  参见

        forceFinished(boolean)

 

  public boolean computeScrollOffset ()

  当想要知道新的位置时,调用此函数。如果返回true,表示动画还没有结束。位置改变以提供一个新的位置。

 

  public void extendDuration (int extend)

  延长滚动动画时间。此函数允许当使用setFinalX(int) or setFinalY(int) 时,卷动动作持续更长时间并且卷动更长距离。

          参数

              extend 卷动事件延长的时间,以毫秒为单位

          参见

              setFinalX(int)

              setFinalY(int)

 

  public void fling (int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY)

  在fling(译者注:快滑,用户按下触摸屏、快速移动后松开)手势基础上开始滚动。滚动的距离取决于fling的初速度。

      参数

          startX 滚动起始点X坐标

  startY 滚动起始点Y坐标

  velocityX   当滑动屏幕时X方向初速度,以每秒像素数计算

  velocityY   当滑动屏幕时Y方向初速度,以每秒像素数计算

  minX    X方向的最小值,scroller不会滚过此点。

  maxX    X方向的最大值,scroller不会滚过此点。

  minY    Y方向的最小值,scroller不会滚过此点。

  maxY    Y方向的最大值,scroller不会滚过此点。

 

  public final void forceFinished (boolean finished)

   强制设置为scroll状态

 

  public final int getCurrX ()

  返回当前滚动X方向的偏移

      返回值

          距离原点X方向的绝对值

 

  public final int getCurrY ()

  返回当前滚动Y方向的偏移

      返回值

          距离原点Y方向的绝对值

 

  public final int getDuration ()

  返回滚动事件的持续时间,以毫秒计算。

      返回值

          滚动持续的毫秒数

 

  public final int getFinalX ()

  返回滚动结束位置。仅针对“fling”手势有效

      返回值

          最终位置X方向距离原点的绝对距离

 

  public final int getFinalY ()

  返回滚动结束位置。仅针对“fling”操作有效

      返回值

          最终位置Y方向距离原点的绝对距离

 

  public final int getStartX ()

  返回滚动起始点的X方向的偏移

      返回值

          起始点在X方向距离原点的绝对距离

 

  public final int getStartY ()

  返回滚动起始点的Y方向的偏移

      返回值

          起始点在Y方向距离原点的绝对距离

 

  public final boolean isFinished ()

  返回scroller是否已完成滚动。

      返回值

          停止滚动返回true,否则返回false

 

  public void setFinalX (int newX)

  设置scroller的X方向终止位置

      参数

          newX    新位置在X方向距离原点的绝对偏移。

      参见

          extendDuration(int)

          setFinalY(int)

 

  public void setFinalY (int newY)

  设置scroller的Y方向终止位置

      参数

          newY    新位置在Y方向距离原点的绝对偏移。

      参见

          extendDuration(int)

          setFinalY(int)

 

  public void startScroll (int startX, int startY, int dx, int dy)

  以提供的起始点和将要滑动的距离开始滚动。滚动会使用缺省值250ms作为持续时间。

      参数

          startX 水平方向滚动的偏移值,以像素为单位。正值表明滚动将向左滚动

  startY 垂直方向滚动的偏移值,以像素为单位。正值表明滚动将向上滚动

  dx 水平方向滑动的距离,正值会使滚动向左滚动

  dy 垂直方向滑动的距离,正值会使滚动向上滚动

 

  public void startScroll (int startX, int startY, int dx, int dy, int duration)

  以提供的起始点和将要滑动的距离开始滚动。

      参数

          startX 水平方向滚动的偏移值,以像素为单位。正值表明滚动将向左滚动

  startY 垂直方向滚动的偏移值,以像素为单位。正值表明滚动将向上滚动

  dx 水平方向滑动的距离,正值会使滚动向左滚动

  dy 垂直方向滑动的距离,正值会使滚动向上滚动

        duration    滚动持续时间,以毫秒计。

 

  public int timePassed ()

  返回自滚动开始经过的时间

      返回值

            经过时间以毫秒为单位

 

  五、补充

    文章精选

      Scroller 粗浅理解

      ScrollTextView - scrolling TextView for Android

    示例代码

      创建工程MyScroler,或者将下类名“MyScroler”改为自己创建的工程,将下面代码直接覆盖生成的.java文件运行即可:

package my.Scroller;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.Scroller;

 

public class MyScroler extends Activity {

    /** Called when the activity is first created. */

    LinearLayout lay1,lay2,lay;

     private Scroller mScroller;

     private boolean s1,s2;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mScroller = new Scroller(this);

         lay1 = new LinearLayout(this){

             @Override 

             public void computeScroll() { 

                 if (mScroller.computeScrollOffset()) { 

                     scrollTo(mScroller.getCurrX(), 0); 

                     postInvalidate(); 

                 } 

             } 

         };

         lay2 = new LinearLayout(this){

             @Override 

             public void computeScroll() { 

                 if (mScroller.computeScrollOffset()) { 

                    // mScrollX = mScroller.getCurrX(); 

                     scrollTo(mScroller.getCurrX(), 0); 

                     postInvalidate(); 

                 } 

             } 

         };

      lay1.setBackgroundColor(this.getResources().getColor(android.R.color.darker_gray));

        lay2.setBackgroundColor(this.getResources().getColor(android.R.color.white));

        lay = new LinearLayout(this);

        lay.setOrientation(LinearLayout.VERTICAL);

        LinearLayout.LayoutParams p0 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);     

        this.setContentView(lay, p0);

        

        LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);     

        p1.weight=1;

        lay.addView(lay1,p1);

        LinearLayout.LayoutParams p2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);     

        p2.weight=1;

        lay.addView(lay2,p2);

        Button tx = new Button(this);

        Button tx2 = new Button(this);

        tx.setText("Button1");  

        tx2.setText("Button2");

        tx.setOnClickListener(new OnClickListener(){

            @Override

            public void onClick(View v) {

                if(!s1){

                    mScroller.startScroll(0, 0, 5, 10, 10);

                    s1 = true;

                }else{

                    mScroller.startScroll(0, 0, -50, -10,10);

                    s1 = false;

                }

            }

            

        });

        tx2.setOnClickListener(new OnClickListener(){

            @Override

            public void onClick(View v) {

                if(!s2){

                    mScroller.startScroll(0, 0, 5, 20,10);

                    s2=true;

                }else{

                    mScroller.startScroll(20, 20, -50, -20,10);

                    s2=false;

                }

            }

        });

        lay1.addView(tx);

        lay2.addView(tx2);

    }

}

 

 

 

 

 

 

 

 


 

android.widget.Scroller是用于模拟scrolling行为,它是scrolling行为的一个帮助类。我们通常通过它的startScroll(intstartX, int startY, int dx, int dy, int duration)函数来设置一个scrolling行为模型,即在 int duration(单位为毫秒)时间的内从int startX, int startY,这个点起向X和Y方向分别滚动 int dx和 int dy个像素。然后我们可以调用 computeScrollOffset()计算此时scroll到的位置,并调用 getCurrX()和 getCurrY()得到到此时在X和Y方向的位置。

另外我们通常通过它的fling(int startX, int startY, int velocityX,int velocityY, int minX, int maxX, int minY, int maxY)函数来设置一个fling行为(特殊的scroll)模型,即在在nt startX, int startY,这个点起向X和Y方向分别以int velocityX和int velocityY个像素的速度进行滚动。然后我们可以调用computeScrollOffset()计算此时scroll到的位置,并调用getCurrX()和getCurrY()得到到此时在X和Y方向的位置。

公共构造函数

 

 

Public Constructors
 

Scroller(Contextcontext)

Create a Scroller with the default duration and interpolator.

 

 

 

 

 

 

 

 

 

 

 

Scroller(Contextcontext, Interpolatorinterpolator)

Create a Scroller with the specified interpolator.

interpolator参数只是在computeScrollOffset()函数中用于对我们的流逝的时间(通过timePassed()取得)这值进行重新解析

 

 

 

 

 

 

 

 

 

 

 

Scroller(Contextcontext, Interpolatorinterpolator, boolean flywheel)

Create a Scroller with the specified interpolator.

interpolator只是在computeScrollOffset()函数中用于对我们的流逝的时间(通过timePassed()取得)这值进行重新解析

 

 

 

 

 

 

 

 

 

 

 

 

公共函数

 

Public Methods

void

abortAnimation()

Stops the animation.

停止scroll

 

 

 

 

 

 

 

 

 

 

boolean

computeScrollOffset()

Call this when you want to know the new location.

计算scroll的情况

 

 

 

 

 

 

 

 

 

 

void

extendDuration(int extend)

Extend the scroll animation.

增加scroll的时间

 

 

 

 

 

 

 

 

 

 

void

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

Start scrolling based on a fling gesture.

模拟fling形式的scroll行为。

int startX, int startY代表起点,int velocityX, intvelocityY代表初速度,int minX,int maxX,int minY, int maxY代表终点的范围

 

 

 

 

 

 

 

 

 

 

final void

forceFinished(boolean finished)

Force the finished field to a particular value.

强制设置为scroll状态

 

 

 

 

 

 

 

 

 

 

float

getCurrVelocity()

Returns the current velocity.

得到当前速度。该值是X方向和Y方向的合成值

 

 

 

 

 

 

 

 

 

 

final int

getCurrX()

Returns the current X offset in the scroll.

得到当前的X坐标

 

 

 

 

 

 

 

 

 

 

final int

getCurrY()

Returns the current Y offset in the scroll.

得到当前的Y坐标

 

 

 

 

 

 

 

 

 

 

final int

getDuration()

Returns how long the scroll event will take, in milliseconds.

得到设置的scroll行为的总时间值

 

 

 

 

 

 

 

 

 

 

final int

getFinalX()

Returns where the scroll will end.

得到scroll行为终点的X值

 

 

 

 

 

 

 

 

 

 

final int

getFinalY()

Returns where the scroll will end.

得到scroll行为终点的Y值

 

 

 

 

 

 

 

 

 

 

final int

getStartX()

Returns the start X offset in the scroll.

得到scroll行为起点的X值

 

 

 

 

 

 

 

 

 

 

final int

getStartY()

Returns the start Y offset in the scroll.

得到scroll行为起点的Y值

 

 

 

 

 

 

 

 

 

 

final boolean

isFinished()

Returns whether the scroller has finished scrolling.

返回scroll行为是否结束

 

 

 

 

 

 

 

 

 

 

void

setFinalX(int newX)

Sets the final position (X) for this scroller.

设置scroll行为的终点的X值

 

 

 

 

 

 

 

 

 

 

void

setFinalY(int newY)

Sets the final position (Y) for this scroller.

设置scroll行为的终点的Y值

 

 

 

 

 

 

 

 

 

 

final void

setFriction(float friction)

The amount of friction applied to flings.

 

 

 

 

 

 

 

 

 

 

void

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

Start scrolling by providing a starting point and the distance to travel.

设置一个scrolling行为模型,即在int duration(单位为毫秒)时间的内从int startX, int startY,这个点起向X和Y方向分别滚动int dx和int dy个像素

 

 

 

 

 

 

 

 

 

 

void

startScroll(int startX, int startY, int dx, int dy, int duration)

Start scrolling by providing a starting point and the distance to travel.

设置一个scrolling行为模型,即在默认时间(250毫秒)内从int startX, int startY,这个点起向X和Y方向分别滚动int dx和int dy个像素

 

 

 

 

 

 

 

 

 

 

int

timePassed()

Returns the time elapsed since the beginning of the scrolling.

取得从scroll开始到现在已经失去的时间

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值