翻译自:http://developer.android.com/reference/android/widget/Scroller.html

android.widget.Scroller是用于模拟scrolling行为,它是scrolling行为的一个帮助类。我们通常通过它的 startScroll(intstartX, intstartY, intdx, 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代表起点, intvelocityX, int velocityY代表初速度,intminX, int maxX, intminY, 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开始到现在已经失去的时间
示例代码 示例程序来自:http://mengsina.iteye.com/blog/1123339 创建工程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);     } }
结束!

更多相关文章

  1. 中:Android(安卓)startActivity原理分析(基于Android(安卓)8.1 AO
  2. Android(安卓)MediaScanner 详尽分析
  3. android-JNI
  4. Android(安卓)TextView 设置删除线
  5. Android(安卓)studio或者idea: marketplace plugins are not load
  6. android 垂直方向上下滑动阻尼效果
  7. android下实现程序不操作一段时间,执行另一个程序
  8. 学习笔记|Android中的setRequestorientation
  9. android 日期 时间

随机推荐

  1. android下res目录 资源定义及使用
  2. Android短信彩信收发流程(应用层)
  3. android事件处理总结--dispatchTouchEven
  4. Android中6.0及以上悬浮窗申请权限
  5. Appium -GitHub
  6. listview 设置数组为空
  7. Android(安卓)顶部灰条标题栏不显示的方
  8. Android(安卓)LayoutInflater.inflate()
  9. 彻底解决andorid h5交互!浅谈h5交互和js
  10. Android重写HorizontalScrollView仿ViewP