TextSwitcher的使用(一)

创建文本变化的动画效果
package com.example.androidtest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
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;

public class MainActivity extends Activity implements ViewSwitcher.ViewFactory, View.OnClickListener{
   
	private TextSwitcher mSwitcher;
	private int mCounter = 0;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     
        setContentView(R.layout.activity_main); 
        
        mSwitcher = (TextSwitcher)findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
        
        mSwitcher.setInAnimation(in);
        mSwitcher.setOutAnimation(out);
        
        Button nextButton = (Button)findViewById(R.id.next);
        nextButton.setOnClickListener(this);
        
        updateCounter();
    }  
    
    private void updateCounter()
    {
    	mSwitcher.setText(String.valueOf(mCounter));
    }
    
    public View makeView()
    {
    	TextView t = new TextView(this);
    	t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    	t.setTextSize(36);
    	return t;
    }
    
    public void onClick(View v)
    {
    	mCounter++;
    	updateCounter();
    }
}
activity_main.xml:



    


你可能感兴趣的:(android)