AndroidUI组件之TextSwitcher

package com.gc.textswitcherdemo;
/*
 * 文本切换器(TextSwitcher):
 * 1、TextSwitcher继承了ViewSwitcher,因此它具有与ViewSwitcher相同的特征
 * 可以在切换View组件时使用动画效果。与ImageSwitcher相似的是,使用TextSwitcher
 * 也需要设置一个ViewFactory。与ImageSwitcher不同的是,TextSwitcher所需的
 * ViewFactory的makeView()方法必须返回一个TextView组件。
 * 2、TextSwitcher与TextView的功能有点相似,它们都可用于显示文本内容,区别在于
 * TextSwitcher的效果更炫,它可以指定文本切换时的动画效果
 * 
 */
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
/**
 * 
 * @author Android将军
 *
 */
public class MainActivity extends Activity {

	private TextSwitcher textSwitcher;
	private String[] strs=new String[]
			{
			"Android将军",
			"ios将军",
			"Cocos2d-X将军",
			"将军"
			
			};
	private int curStr;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textSwitcher=(TextSwitcher)findViewById(R.id.textSwitcher);
		textSwitcher.setFactory(new ViewFactory() {
			
			@Override
			public View makeView() {
				TextView tv=new TextView(MainActivity.this);
				tv.setTextSize(40);
				tv.setTextColor(Color.MAGENTA);
				return tv;
			}
		});
		//调用next方法显示下一个字符串
		next(null);
	}
	//事件处理函数,控制显示下一个字符串
	public void next(View source)
	{
		textSwitcher.setText(strs[curStr++%strs.length]);
	}
	

	

}

布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    
   >
   <!-- 定义一个TextSwitcher,并指定了文本切换时的动画效果 -->
   <TextSwitcher 
       android:id="@+id/textSwitcher"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:inAnimation="@android:anim/slide_in_left"
       android:outAnimation="@android:anim/slide_out_right"
       android:onClick="next"
       />
    
</LinearLayout>
效果截图:

转载请注明出处: http://blog.csdn.net/android_jiangjun/article/details/25604177

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值