参考地址:http://www.tuicool.com/articles/2UF3iaB
参考地址:
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>