【Android进阶】文本切换器(TextSwitcher)的功能与用法(自动切换仿京东淘宝快报效果)

参考地址:http://www.tuicool.com/articles/2UF3iaB

参考地址:

http://www.mythroad.net/2013/09/22/textview%E6%96%87%E6%9C%AC%E6%BB%9A%E5%8A%A8%E6%98%BE%E7%A4%BA%E6%95%88%E6%9E%9C%E6%8E%A7%E4%BB%B6textswitcher%E3%80%90%E5%B7%B2%E8%A7%A3%E5%86%B3%E3%80%91/


TextSwitcher集成了ViewSwitcher, 因此它具有与ViewSwitcher相同的特性:可以在切换View组件时使用动画效果。与ImageSwitcher相似的是,使用TextSwitcher也需要设置一个ViewFactory。与ImageSwitcher不同的是,TextSwitcher所需要的ViewFactory的makeView()方法必须返回一个TextView组件。
<TextSwitcher与TextView的功能有点类似,它们都可用于显示文本内容,区别在于TextSwitcher的效果更炫,它可以指定文本切换时的动画效果。>

不多说,直接上代码了。界面布局文件如下:

<RelativeLayout 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" >
  
  <!-- 定义一个TextSwitcher,并制定了文本切换时的动画效果 -->

  <TextSwitcher
    android:id="@+id/textSwitcher"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:inAnimation="@android:anim/slide_in_left"
    android:outAnimation="@android:anim/slide_out_right" 
    android:onClick="next"
    >
  </TextSwitcher>

</RelativeLayout>

ps: 系统的左进右出:

android:inAnimation="@android:anim/slide_in_left"
  android:outAnimation="@android:anim/slide_out_right" 

slide_in_left:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate android:fromXDelta="-50%p" android:toXDelta="0"
            android:duration="@android:integer/config_mediumAnimTime"/>
	<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_out_right:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate android:fromXDelta="0" android:toXDelta="50%p"
            android:duration="@android:integer/config_mediumAnimTime"/>
	<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

Activity如下:

public class MainActivity extends Activity {

	private TextSwitcher textSwitcher;

	// 要显示的文本
	String[] poemArray = new String[] { "one", "two", "three" };
	private int index;
        Timer timer;

	/*
	 * 快报滚动播放
	 */
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case ActivityForResultUtil.MSG_WHAT_UPDATE_NEWS_INFO:   //这个自己定义一下
				updateNews();
				break;
			default:
				break;
			}
		};
	};

	@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;
			}
		});
		// 设置图片来源
		tv_news.setText(poemArray[index]);
		
		 // 设置点击监听器  
		tv_news.setOnClickListener(new View.OnClickListener() {  
  
            public void onClick(View v) {                 
                Toast.makeText(MainActivity.this, poemArray[index], Toast.LENGTH_SHORT).show();
            }  
        });

		timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				mHandler.obtainMessage(
						ActivityForResultUtil.MSG_WHAT_UPDATE_NEWS_INFO)
						.sendToTarget();
			}

		}, 1, 4000);
	}

	// 设置切入动画
	// tv_news.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
	// R.anim.slide_down_in));
	// // 设置切出动画
	// tv_news.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(),
	// R.anim.slide_up_out));

	/*
	 * 更新快报
	 */
	protected void updateNews() {
		
		 index++;  
         if (index >= poemArray.length) {  
             index = 0;  
         }  
         tv_news.setText(poemArray[index]);  
		
	}
}

我们可以在java文件中设置动画,也可以在布局文件中直接设置。


附上下进上出xml文件:

slide_down_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" 
	xmlns:android="http://schemas.android.com/apk/res/android">
 	<translate android:fromYDelta="100%p" 
 		android:toYDelta="0"
		android:duration="400" />
</set>

slide_up_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" 
	xmlns:android="http://schemas.android.com/apk/res/android">
 	<translate android:fromYDelta="0" 
 		android:toYDelta="-100%p"
		android:duration="400" />
</set>


ps:自动滚动用的handler 




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值