android TextSwitcher

 public class TextSwitcher extends ViewSwitcherhtml

java.lang.Objectjava

android.view.Viewandroid

      android.view.ViewGroupapp

           android.widget.FrameLayoutide

               android.widget.ViewAnimator函數

                     android.widget.ViewSwitcher佈局

                               android.widget.TextSwitcher動畫

 ImageSwitcher 和TextViewSwitcher都是ViewSwitcher 的子類,ViewSwitcher又是ViewAnimator 的子類,ViewAnimator (FrameLayout的子類)提供了不一樣View之間切換時的動畫效果支持,而ViewAnimator 爲FrameLayout的子類,所以ViewAnimator中包含的子View都是疊放在一塊兒,通常狀況下支持看到最上面的一個View。this

ViewSwitcher只能最多包括兩個子View。每次只顯示其中一個View,它有兩個子類TextSwitcher 和ImageSwitcher 。本例介紹TextSwitcher ,TextSwitcher 種能包含TextView,TextSwitcher主要能夠用法文字切換時動畫效果。spa

每當調用setText時,TextSwitcher 使用設定的動畫效果顯示新文字串和原先文字之間的切換。能夠使用addView 爲ViewSwitcher 手動添加一個View到ViewSwitcher中,也能夠使用ViewSwitcher.ViewFactory 自動建立一個新View。本例使用ViewSwitcher.ViewFactory爲TextSwitcher建立一個新View。

構造函數

public TextSwitcher (Context context)

建立一個新的空TextSwitcher

參數

context 應用程序上下文

public TextSwitcher (Context context, AttributeSet attrs)

使用提供的contextattributes來建立一個空的TextSwitcher

參數

context 應用程序環境

attrs 屬性集合

公共方法

public void addView (View child, int index, ViewGroup.LayoutParams params)

根據指定的佈局參數新增一個子視圖

參數

child 新增的子視圖

index 新增子視圖的位置

params 新增子視圖的佈局參數

拋出異常

IllegalArgumentException 當子視圖不是一個TextView實例時

public void setCurrentText (CharSequence text)

設置當前顯示的文本視圖的文字內容。非動畫方式顯示。

參數

text 須要顯示的新文本內容

public void setText (CharSequence text)

設置下一視圖的文本內容並切換到下一視圖。能夠動畫的退出當前文本內容,顯示下一文本內容。

參數

text 須要顯示的新文本內容

案例:

<?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" >
    
    <TextSwitcher 
        android:id="@+id/textviewswitcher_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        >
     </TextSwitcher>
     
    <Button 
        android:id="@+id/btn_textviewswitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextSwitcher"
        />
</LinearLayout>

package com.test;

import android.R.string;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;

public class TextSwitcherDemo extends Activity implements ViewFactory
		 {
	
	private  TextSwitcher  textSwitcher;
	
	private  int counter=0;
	
	private Button   button; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.textswitcher);
		
		textSwitcher=(TextSwitcher)findViewById(R.id.textviewswitcher_id);
		textSwitcher.setFactory(this);
		
		Animation in = AnimationUtils.loadAnimation(this,  android.R.anim.fade_in);  
		Animation out = AnimationUtils.loadAnimation(this,  android.R.anim.fade_out);  
		textSwitcher.setInAnimation(in);  
		textSwitcher.setOutAnimation(out); 
		
		button = (Button)findViewById(R.id.btn_textviewswitcher);
		
		button.setOnClickListener(listener);
		updateCounter();
	}
	
	private OnClickListener  listener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			counter++;
			updateCounter();
		}
	};
	
	private void updateCounter() {
		// TODO Auto-generated method stub
		textSwitcher.setText(String.valueOf(counter));
	}
	
	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		TextView  textView = new TextView(this);
		textView.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL);
		textView.setTextSize(35);
		return textView;
	}

}

3.執行結果:

 

相關文章
相關標籤/搜索