?
Ce document utilise Manuel du site Web PHP chinois Libérer
Scroller
版本:Android 2.3 r1
?
public class Scroller extends Object
?
java.lang.Object
?? ? android.widget.Scrolle
?
類概述
這個類封裝了滾動操作。滾動的持續(xù)時間可以通過構(gòu)造函數(shù)傳遞,并且可以指定滾動動作的持續(xù)的最長時間。經(jīng)過這段時間,滾動會自動定位到最終位置,并且通過computeScrollOffset()會得到的返回值為false,表明滾動動作已經(jīng)結(jié)束。
?
構(gòu)造函數(shù)
public Scroller (Context context)
使用缺省的持續(xù)持續(xù)時間和動畫插入器創(chuàng)建一個Scroller。(譯者注:interpolator這里翻譯為動畫插入器,見這里。)
?
public Scroller (Context context, Interpolator interpolator)
根據(jù)指定的動畫插入器創(chuàng)建一個Scroller,如果指定的動畫插入器為空,則會使用缺省的動畫插入器(粘滯viscous)創(chuàng)建。
?
公共方法
public void abortAnimation ()
停止動畫。與forceFinished(boolean)相反,Scroller滾動到最終x與y位置時中止動畫。
參見
forceFinished(boolean)
?
public boolean computeScrollOffset ()
當(dāng)想要知道新的位置時,調(diào)用此函數(shù)。如果返回true,表示動畫還沒有結(jié)束。位置改變以提供一個新的位置。
?
public void extendDuration (int extend)
延長滾動動畫時間。此函數(shù)允許當(dāng)使用setFinalX(int) or setFinalY(int) 時,卷動動作持續(xù)更長時間并且卷動更長距離。
?????????????????? 參數(shù)
??????????????????????????? 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(譯者注:快滑,用戶按下觸摸屏、快速移動后松開)手勢基礎(chǔ)上開始滾動。滾動的距離取決于fling的初速度。
???????? 參數(shù)
?????????????????? startX?????? 滾動起始點X坐標(biāo)
startY?????? 滾動起始點Y坐標(biāo)
velocityX? 當(dāng)滑動屏幕時X方向初速度,以每秒像素數(shù)計算
velocityY? 當(dāng)滑動屏幕時Y方向初速度,以每秒像素數(shù)計算
minX???????? X方向的最小值,scroller不會滾過此點。
maxX??????? X方向的最大值,scroller不會滾過此點。
minY???????? Y方向的最小值,scroller不會滾過此點。
maxY??????? Y方向的最大值,scroller不會滾過此點。
?
public final void forceFinished (boolean finished)
強制終止的字段到特定值。(譯者注:立即停止?jié)L動?)
???????? 參數(shù)
?????????????????? finished??? 新的結(jié)束值
?
public final int getCurrX ()
返回當(dāng)前滾動X方向的偏移
???????? 返回值
?????????????????? 距離原點X方向的絕對值
?
public final int getCurrY ()
返回當(dāng)前滾動Y方向的偏移
???????? 返回值
?????????????????? 距離原點Y方向的絕對值
?
public final int getDuration ()
返回滾動事件的持續(xù)時間,以毫秒計算。
???????? 返回值
?????????????????? 滾動持續(xù)的毫秒數(shù)
?
public final int getFinalX ()
返回滾動結(jié)束位置。僅針對“fling”手勢有效
???????? 返回值
?????????????????? 最終位置X方向距離原點的絕對距離
?
public final int getFinalY ()
返回滾動結(jié)束位置。僅針對“fling”操作有效
???????? 返回值
?????????????????? 最終位置Y方向距離原點的絕對距離
?
public final int getStartX ()
返回滾動起始點的X方向的偏移
???????? 返回值
?????????????????? 起始點在X方向距離原點的絕對距離
?
public final int getStartY ()
返回滾動起始點的Y方向的偏移
???????? 返回值
?????????????????? 起始點在Y方向距離原點的絕對距離
?
public final boolean isFinished ()
返回scroller是否已完成滾動。
???????? 返回值
?????????????????? 停止?jié)L動返回true,否則返回false
?
public void setFinalX (int newX)
設(shè)置scroller的X方向終止位置
???????? 參數(shù)
?????????????????? newX??????? 新位置在X方向距離原點的絕對偏移。
???????? 參見
??????? extendDuration(int)
??????? setFinalY(int)
?
public void setFinalY (int newY)
設(shè)置scroller的Y方向終止位置
???????? 參數(shù)
?????????????????? newY??????? 新位置在Y方向距離原點的絕對偏移。
??? 參見
??????? extendDuration(int)
??????? setFinalY(int)
?
public void startScroll (int startX, int startY, int dx, int dy)
以提供的起始點和將要滑動的距離開始滾動。滾動會使用缺省值250ms作為持續(xù)時間。
??? 參數(shù)
?????????????????? startX?????? 水平方向滾動的偏移值,以像素為單位。正值表明滾動將向左滾動
startY?????? 垂直方向滾動的偏移值,以像素為單位。正值表明滾動將向上滾動
dx???? 水平方向滑動的距離,正值會使?jié)L動向左滾動
dy???? 垂直方向滑動的距離,正值會使?jié)L動向上滾動
?
public void startScroll (int startX, int startY, int dx, int dy, int duration)
以提供的起始點和將要滑動的距離開始滾動。
??? 參數(shù)
?????????????????? startX?????? 水平方向滾動的偏移值,以像素為單位。正值表明滾動將向左滾動
startY?????? 垂直方向滾動的偏移值,以像素為單位。正值表明滾動將向上滾動
dx???? 水平方向滑動的距離,正值會使?jié)L動向左滾動
dy???? 垂直方向滑動的距離,正值會使?jié)L動向上滾動
duration?? 滾動持續(xù)時間,以毫秒計。
?
public int timePassed ()
返回自滾動開始經(jīng)過的時間
??? 返回值
??????? 經(jīng)過時間以毫秒為單位
?
補充
??? 文章精選
??????? Scroller 粗淺理解
??????? ScrollTextView - scrolling TextView for Android
??? 示例代碼
創(chuàng)建工程MyScroler,或者將下類名“MyScroler”改為自己創(chuàng)建的工程,將下面代碼直接覆蓋生成的.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);
??? }
}