正文
一、結構
java.lang.Object
android.widget.Scroller
二、概述
這個類封裝了滾動操作。滾動的持續時間可以通過構造函數傳遞,并且可以指定滾動動作的持續的最長時間。經過這段時間,滾動會自動定位到最終位置,并且通過computeScrollOffset()會得到的傳回值為false,表明滾動動作已經結束。
三、構造函數
根據指定的動畫插入器建立一個Scroller,如果指定的動畫插入器為空,則會使用預設的動畫插入器(粘滞viscous)建立。
四、公共方法
public void abortAnimation ()
參見
public boolean computeScrollOffset ()
當想要知道新的位置時,調用此函數。如果傳回true,表示動畫還沒有結束。位置改變以提供一個新的位置。
public void extendDuration (int extend)
參數
extend 卷動事件延長的時間,以毫秒為機關
參見
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方向距離原點的絕對偏移。
參見
public void setFinalY (int newY)
設定scroller的Y方向終止位置
newY 新位置在Y方向距離原點的絕對偏移。
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)
以提供的起始點和将要滑動的距離開始滾動。
duration 滾動持續時間,以毫秒計。
public int timePassed ()
傳回自滾動開始經過的時間
經過時間以毫秒為機關
五、補充
文章精選
示例代碼
建立工程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){
// mScrollX = mScroller.getCurrX();
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(){
if(!s2){
mScroller.startScroll(0, 0, 5, 20,10);
s2=true;
mScroller.startScroll(20, 20, -50, -20,10);
s2=false;
lay1.addView(tx);
lay2.addView(tx2);
}
}
本文轉自over140 51CTO部落格,原文連結:http://blog.51cto.com/over140/582535,如需轉載請自行聯系原作者