前言
本章內容是 android.widget.Scroller,版本爲Android 2.3 r1,翻譯來自"pengyouhong",再次感謝"pengyouhong"!期待你一起參與Android中文API的翻譯,聯繫我[email protected]。
聲明
歡迎轉載,但請保留文章原始出處:)
Android中文翻譯組:http://code.taobao.org/project/view/404/
正文
一、結構
二、概述
三、構造函數
public Scroller (Context context)
使用缺省的持續持續時間和動畫插入器創建一個Scroller。(譯者注:interpolator這裏翻譯爲動畫插入器,見這裏。)
public Scroller (Context context, Interpolator interpolator)
根據指定的動畫插入器創建一個Scroller,如果指定的動畫插入器爲空,則會使用缺省的動畫插入器(粘滯viscous)創建。
四、公共方法
public void abortAnimation ()
停止動畫。與forceFinished(boolean)相反,Scroller滾動到最終x與y位置時中止動畫。
參見
public boolean computeScrollOffset ()
當想要知道新的位置時,調用此函數。如果返回true,表示動畫還沒有結束。位置改變以提供一個新的位置。
public void extendDuration (int extend)
延長滾動動畫時間。此函數允許當使用setFinalX(int) or setFinalY(int) 時,捲動動作持續更長時間並且捲動更長距離。
參數
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)
以提供的起始點和將要滑動的距離開始滾動。
參數
startX 水平方向滾動的偏移值,以像素爲單位。正值表明滾動將向左滾動
startY 垂直方向滾動的偏移值,以像素爲單位。正值表明滾動將向上滾動
dx 水平方向滑動的距離,正值會使滾動向左滾動
dy 垂直方向滑動的距離,正值會使滾動向上滾動
duration 滾動持續時間,以毫秒計。
public int timePassed ()
返回自滾動開始經過的時間
返回值
經過時間以毫秒爲單位
五、補充
文章精選
ScrollTextView - scrolling TextView for Android
示例代碼
創建工程MyScroler,或者將下類名“MyScroler”改爲自己創建的工程,將下面代碼直接覆蓋生成的.java文件運行即可:
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);
}
}
結束
本文中文資料較少,內容也較長,再次感謝"pengyouhong"是辛勤付出 :)