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

来源:互联网 发布:钢琴淘宝 编辑:程序博客网 时间:2024/04/30 07:49

参考地址: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() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case ActivityForResultUtil.MSG_WHAT_UPDATE_NEWS_INFO:   //这个自己定义一下updateNews();break;default:break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);textSwitcher.setFactory(new ViewFactory() {@Overridepublic 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() {@Overridepublic void run() {// TODO Auto-generated method stubmHandler.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 0
原创粉丝点击