翻譯自:http://developer.android.com/reference/android/widget/Scroller.html
android.widget.Scroller是用於模擬scrolling行為,它是scrolling行為的一個幫助類。我們通常通過它的 startScroll(int startX, 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(Context context)
Create a Scroller with the default duration and interpolator.
Scroller(Context context, Interpolator interpolator)
Create a Scroller with the specified interpolator.
interpolator參數只是在computeScrollOffset()函數中用於
對我們的流逝的時間(通過timePassed()取得
)這值進行重新解析
Scroller(Context context, Interpolator interpolator, 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, int velocityY代表初速度,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開始到現在已經失去的時間
示例代碼
示例程序來自: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);
}
}
結束!