【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>
0 0
- 【Android进阶】文本切换器(TextSwitcher)的功能与用法(自动切换仿京东淘宝快报效果)
- Textswitcher的简单用法与功能(文本切换器)
- 文本切换器(TextSwitcher)的功能与用法
- Android TextSwitcher - 文本切换器
- Android TextSwitcher 文本切换器
- 文本切换器(TextSwitcher)
- TextSwitcher--文本切换器
- 文本切换器的功能和用法
- Android中TextSwitcher的功能与用法
- ImageSwitcher 图片切换器 和 TextSwitcher 文本切换器
- TextSwitcher,ImageSwitcher切换器
- Android仿淘宝商品自动滚动 ImageSwitcher和TextSwitcher的用法
- TextSwitcher类(TextView改变文本动画切换)
- android文本切换器
- 【Android进阶】自定义带动画切换效果的ViewPager
- 【Android进阶】实现各种各样的Tab切换效果
- 仿淘宝的图片切换效果
- Android学习之路---ImageSwitcher和TextSwitcher切换图片及文本
- iOS开发中清理沙盒目录的缓存
- 多字文本android:ellipsize属性不正常工作的解决办法
- 结构体,联合和枚举
- Android6.0的处理 转载
- sizeof的用法
- 【Android进阶】文本切换器(TextSwitcher)的功能与用法(自动切换仿京东淘宝快报效果)
- 【LeetCode】104. Maximum Depth of Binary Tree
- Unity新项目如何快速理清顶层代码结构
- firefox和chrome的开发者工具使用技巧
- Java对象序列化之 transient 使用小记
- text-overflow:ellipsis的巧妙运用
- 各种盒模型
- c++第一次实验 圆的半径
- 安装Windows和Ubuntu双系统