標籤:android animate
TextSwitcher的Java Doc是這樣描述自己的:
Specialized ViewSwitcher that contains only children of type TextView. A TextSwitcher is useful to animate a label on screen. Whenever setText(CharSequence) is called, TextSwitcher animates the current text out and animates the new text in.
由此可知,TextSwitcher:
- 有個TextView子視圖
- 在文本更新時,能夠讓舊文本淡出,新文本淡入,從而呈現平滑切換的動畫效果
如何使用TextSwitcher第1步:在layout中添加TextSwitcher控制項
<TextSwitcher android:id="@+id/ts" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_marginTop="20dp" android:layout_weight="1" > </TextSwitcher>
第2步:為TextSwitcher控制項設定工廠(用於生產視圖)
mTs.setFactory(new TextSwitcher.ViewFactory() { @Override public View makeView() { final TextView tv = (TextView) LayoutInflater.from( getApplicationContext()).inflate(R.layout.text, null); return tv; } });
第3步:設定淡入淡齣動畫
mTs.setInAnimation(AnimationUtils.loadAnimation( getApplicationContext(), android.R.anim.fade_in)); mTs.setOutAnimation(AnimationUtils.loadAnimation( getApplicationContext(), android.R.anim.fade_out));
之後,執行mTs.setText(txt)來切換文本時就會產生如下效果:
註:完整代碼在 GitHub
[Android] TextSwitcher -- 做什麼的