[Android] TextSwitcher — 做什麼的

TextSwitcherJava 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控件
    
    
第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)來切換文本時就會產生如下效果:
TextSwitcher效果

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *