文章內容
1. TextSwitcher組件介紹
2. 使用TextSwitcher組件
3. 應用實例代碼
1. TextSwitcher組件介紹
TextSwitcher 是android平臺自帶的組件,它繼承了ViewSwitcher和ViewAnimator,主要爲文本切換提供動畫效果。TextSwitcher包含兩個子視圖(TextView),但同一時刻只能顯示其中的一個。在由一個視圖向另一個視圖切換時,TextSwitcher可以做到先淡出當前的視圖,然後再淡入下一個視圖,因而有動畫之效果。
2. 使用TextSwitcher組件
TextSwitcher的使用十分簡單,其自身包含的成員函數也相當少。使用該組件的代碼可以直接擴展Activity類,或者同時實現ViewSwitcher.ViewFactory接口。如果像往常一樣直接擴展Activity類,我們需要手動爲TextSwitcher添加兩個子視圖(只能是兩個TextView),如果同時實現了ViewSwitcher.ViewFactory接口,則需要使用該接口內的方法來添加視圖(該方法被調用兩次)。無論使用何種方式,都需要爲兩個子視圖設置動畫(淡入,淡出)。最後,在需要切換視圖時,只需要調用TextSwitcher內的setText方法。
3. 應用實例代碼
最後,我們通過一個簡單的例子來具體演示如何使用TextSwitcher組件。
(提示:創建工程及設置應用清單文件的過程省略。)
首先,在工程的res/layout/目錄下創建一名爲show_textswitcher.xml的佈局文件,內容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView android:id="@+id/prompt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click the button to display the text frade-in and frade-out."/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="Click me" />
<TextSwitcher android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
然後定義活動, 我們先只擴展Activity類:
public class UseTextSwitcher extends Activity{
private TextSwitcher mSwitcher;
private int mClickTimes = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_textswitcher);
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
//1.
mSwitcher.addView(makeView());
//2.
mSwitcher.addView(makeView());
Animation fade_in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
Animation fade_out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
mSwitcher.setInAnimation(fade_in);
mSwitcher.setOutAnimation(fade_out);
mSwitcher.setText(String.valueOf(mClickTimes));
Button nextButton = (Button) findViewById(R.id.click);
nextButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
mClickTimes++;
mSwitcher.setText(String.valueOf(mClickTimes));
}
});
}
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
t.setTextSize(30);
Log.w("wanjf", "makeView is called!");
return t;
}
}
在代碼裏,我們只調用了兩次addView(makeView())方法爲TextSwitcher添加了兩個子視圖,必須是兩 個。然後使用setInAnimation,setOutAnimation方法設置淡入和淡出動畫,最後使用setText設置初始值。在button 的點擊事件回調裏簡單地調用setText方法來執行文本切換。
提示:makeView方法返回一個TextView,該方法可以取任意的名字,之所以使用makeView名字是爲了在UseTextSwitcher同時實現ViewSwitcher.ViewFactory接口時可以繼續使用。
現在我們讓上述代碼同時實現ViewSwitcher.ViewFactory接口,該接口內有唯一的方法makeView需要被實現(上述代碼已具體該條件),修改後的代碼如下:
public class UseTextSwitcher extends Activity implements ViewSwitcher.ViewFactory {
private TextSwitcher mSwitcher;
private int mClickTimes = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_textswitcher);
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
Animation fade_in = AnimationUtils.loadAnimation(this,android.R.anim.fade_in);
Animation fade_out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
mSwitcher.setInAnimation(fade_in);
mSwitcher.setOutAnimation(fade_out);
mSwitcher.setText(String.valueOf(mClickTimes));
Button nextButton = (Button) findViewById(R.id.click);
nextButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
mClickTimes++;
mSwitcher.setText(String.valueOf(mClickTimes));
}
});
}
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
t.setTextSize(30);
Log.w("wanjf", "makeView is called!");
return t;
}
}
仔細觀察後發現,修改後的代碼和先前區別並不是很大,除了實現ViewSwitcher.ViewFactory外,用mSwitcher.setFactory(this)替換兩個mSwitcher.addView(makeView())的調用即可。因爲在mSwitcher.setFactory內部完成添加視圖的工作。
最後,運行程序如下:
2012年5月10,晚畢