TextSwitcher滑动动画改变内容
来源:互联网 发布:开心贝贝童装淘宝店招 编辑:程序博客网 时间:2024/05/24 07:09
由于项目需要,要做一个滑动改变选项内容效果的类似iOS的按钮,不由想起TextSwitcher,今天看了http://blog.csdn.net/tianjf0514/article/details/7556487的Demo,感觉不是很完美,故做了一些修改以更好实现效果。
首先是布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextSwitcher android:id="@+id/textSwicher" android:layout_width="200px" android:layout_height="wrap_content" android:background="@android:color/black" android:gravity="center" > </TextSwitcher></LinearLayout>
主Activity文件:
package org.crazyit.ui;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.view.ViewGroup.LayoutParams;import android.view.animation.AnimationUtils;import android.widget.TextSwitcher;import android.widget.TextView;import android.widget.ViewSwitcher;import android.widget.ViewSwitcher.ViewFactory;public class TextSwitcherTest extends Activity {private TextSwitcher textSwicher;// 图片数组private String[] arrayTexts = { "文本01", "文本02", "文本03", "文本04" };// 要显示的图片在图片数组中的Indexprivate int textIndex=0;;// 左右滑动时手指按下的X坐标private float touchDownX;// 左右滑动时手指松开的X坐标private float touchUpX;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);textSwicher = (TextSwitcher) findViewById(R.id.textSwicher);// 为TextSwitcher设置Factory,用来为TextSwitcher制造TextViewtextSwicher.setFactory(new ViewSwitcher.ViewFactory(){public View makeView(){TextView tv = new TextView(TextSwitcherTest.this);tv.setTextSize(40);tv.setTextColor(Color.WHITE);return tv;}});textSwicher.setText(arrayTexts[0]);// 设置TextSwitcher左右滑动监听事件textSwicher.setOnTouchListener(new TextSwicherOnTouchListener());}//开始按钮监听private final class TextSwicherOnTouchListener implements OnTouchListener{ public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) {// 取得左右滑动时手指按下的X坐标touchDownX = event.getX();return true;} else if (event.getAction() == MotionEvent.ACTION_UP) {// 取得左右滑动时手指松开的X坐标touchUpX = event.getX();// 从左往右,看前一文本if (touchUpX - touchDownX > 50) {// 设置文本切换的动画textSwicher.setOutAnimation(AnimationUtils.loadAnimation(TextSwitcherTest.this,android.R.anim.slide_out_right));// 设置当前要看的文本textSwicher.setText(arrayTexts[textIndex=textIndex==0?(arrayTexts.length-1):--textIndex]);textSwicher.setInAnimation(AnimationUtils.loadAnimation(TextSwitcherTest.this,android.R.anim.slide_in_left));// 从右往左,看下一张}else if(touchDownX - touchUpX > 50) {// 设置文本切换的动画// 由于Android没有提供slide_out_left和slide_in_right,所以仿照slide_in_left和slide_out_right编写了slide_out_left和slide_in_righttextSwicher.setOutAnimation(AnimationUtils.loadAnimation(TextSwitcherTest.this,R.anim.slide_out_left));// 设置当前要看的文本textSwicher.setText(arrayTexts[textIndex=textIndex==(arrayTexts.length-1)?0:++textIndex]);textSwicher.setInAnimation(AnimationUtils.loadAnimation(TextSwitcherTest.this,R.anim.slide_in_right));}return true;} return false; }}}
两个动画文件(完全参考上面链接的那个动画):
1.slide_out_left:
<?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="300"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /></set>
2.slide_in_right:
<?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="300"/> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /></set>
效果图就不贴了,很简单的.
- TextSwitcher滑动动画改变内容
- TextSwitcher类(TextView改变文本动画切换)
- 通过TextSwitcher实现广告栏内容动画切换
- 布局内容改变动画
- TextSwitcher切换动画
- AndroidUI 布局动画-布局内容改变动画
- 使用textswitcher进行切换动画
- TextSwitcher
- TextSwitcher
- 用TextSwitcher实现文字切换动画
- 改变ViewPager的内容之间切换动画的速度
- 滑动动画
- jquery lavaLamp背景滑动,内容缓慢滑动动画特效,图片缓慢缩放
- Hack5-用TextSwitcher和ImageSwitcher打造漂亮的动画效果
- Hack 5 用TextSwitcher实现文字切换动画
- Hack 5 用TextSwitcher实现文字切换动画
- 通过TextSwitcher实现上下滚动的广告动画效果
- 解决scrollview中内容改变后自动滑动到底部的问题
- malloc/free and new/delete in C++
- C++ 强制类型转换
- 字符串数组
- Firefox+Selenium IDE
- windbg符号文件路径设置
- TextSwitcher滑动动画改变内容
- C 资源
- ceph文件系统测试
- 支撑5亿用户、1.5亿活跃用户的Twitter最新架构详解及相关实现
- 海量数据处理专题(一)——开篇
- XML序列化与反序列化类库
- Red and Black 搜索
- 提高你的Java代码质量吧:不要让类型默默转换
- 话说耳鼻喉之鼻