一个简单的文本切换动画效果案例。点击文本是,文本左进右出。
代码如下
package com.test.textswitcher;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
//声明控件
private TextSwitcher textSwitcher;
//为TextSwitcher提供String文本
int curStr;
String [] strs = new String[]{
"android","ios","java","c++"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testseitcher_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.BLUE);
return tv;
}
});
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"
tools:context="${relativePackage}.${activityClass}" >
<TextSwitcher android:id="@+id/textswitcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right"
android:onClick="next" />
</LinearLayout>
这里的动画效果比较简单的系统自带的效果,在布局中就已经定义好了,无需在代码中体现。