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)
強制終止的字段到特定值。(譯者注:立即停止滾動?)
參數
finished 新的結束值
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 {
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);
}
}