android 组件动画(二)——TextView刷入与刷出的效果
2011-04-10 23:10
513 查看
首先看看效果:
///项目布局
//// attrs.xml 自定义属性,该属性主要是控制动画播放的时间
///// main.xml
首先自定义名称xmlns:slidingtext=http://schemas.android.com/apk/res/com.testSildingTextView
这里使用了自定义的名称
slidingtext:animationDuration="500"
下面是动画效果的关键代码,大概说明一下它的功能。这个动画效果主要是每次开出一条线程来运行的,首次运行后等2点5秒,就将textview以动画效果向左运行,等0.5秒后从右边出现,动画结束后隐藏,不断循环
///// SlidingTextView
本文为原创,如需转载,请注明作者和出处,谢谢!
代码下载
http://files.cnblogs.com/not-code/testSildingTextView.zip
///项目布局
//// attrs.xml 自定义属性,该属性主要是控制动画播放的时间
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="SlidingText"> <attr name="animationDuration" format="integer" /> </declare-styleable> </resources>
///// main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" xmlns:slidingtext="http://schemas.android.com/apk/res/com.testSildingTextView" android:layout_height="fill_parent"> <com.testSildingTextView.SlidingTextView android:id="@+id/sliding_textview" android:layout_width="fill_parent" android:layout_height="wrap_content" slidingtext:animationDuration="500" android:layout_gravity="center"> <TextView android:layout_width="fill_parent" android:gravity="center_horizontal" android:layout_height="wrap_content" android:text="sssssss" /> </com.testSildingTextView.SlidingTextView> </LinearLayout>
首先自定义名称xmlns:slidingtext=http://schemas.android.com/apk/res/com.testSildingTextView
这里使用了自定义的名称
slidingtext:animationDuration="500"
下面是动画效果的关键代码,大概说明一下它的功能。这个动画效果主要是每次开出一条线程来运行的,首次运行后等2点5秒,就将textview以动画效果向左运行,等0.5秒后从右边出现,动画结束后隐藏,不断循环
///// SlidingTextView
private String[] showTexts = new String[] { "ssssss", "aaaaaa", "bbbbbb" }; // 用来记录显示哪个字符串 private int count = 0; private int mDuration; private TextView text; private int textWidth = 200; //获取自定义变量 public SlidingTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SlidingText); mDuration = a .getInteger(R.styleable.SlidingText_animationDuration, 750); } //设置要显示的字符串 public void setShowText(String[] showTexts){ this.showTexts=showTexts; } // 回调函数 界面初始化快结束时调用 protected void onFinishInflate() { super.onFinishInflate(); text = (TextView) this.getChildAt(0); mHandler.postDelayed(appear, 1000); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // 1为出现,2为隐藏 switch (msg.arg1) { case 1: doAnimationOpen(); break; case 2: doAnimationClose(); break; } } }; public void doAnimationOpen() { post(appear); } // 出现的效果 Runnable appear = new Runnable() { public void run() { TranslateAnimation animation; int fromXDelta = 0, toXDelta = 0, fromYDelta = 0, toYDelta = 0; int calculatedDuration = 0; fromXDelta = textWidth; toXDelta = 0; calculatedDuration = mDuration * Math.abs(toXDelta - fromXDelta) / textWidth; animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta); animation.setDuration(calculatedDuration); animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { if(showTexts.length!=0){ count = (count + 1) % showTexts.length; text.setText(showTexts[count]); } text.setVisibility(VISIBLE); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { mHandler.postDelayed(hide, 2500); } }); startAnimation(animation); } }; public void doAnimationClose() { post(hide); } // 隐藏的效果 Runnable hide = new Runnable() { public void run() { TranslateAnimation animation; int fromXDelta = 0, toXDelta = 0, fromYDelta = 0, toYDelta = 0; int calculatedDuration = 0; toXDelta = -1 * textWidth; calculatedDuration = mDuration * Math.abs(toXDelta - fromXDelta) / textWidth; animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta); animation.setDuration(calculatedDuration); animation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } //动画结束时 设置textview的状态 @Override public void onAnimationEnd(Animation animation) { mHandler.postDelayed(appear, 500); text.setVisibility(INVISIBLE); } }); startAnimation(animation); } };
本文为原创,如需转载,请注明作者和出处,谢谢!
代码下载
http://files.cnblogs.com/not-code/testSildingTextView.zip
相关文章推荐
- Android在xml中设置组件风格(圆角,点击效果selector,边框,进度条风格,动画)TextView文字透明度
- Android buttom textview 颜色平滑过渡的动画效果
- android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
- Android系列教程之TextView小组件的使用--附带超链接和跑马灯效果
- android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
- Android TextView实现点击展开动画效果
- Android实现带动画效果的可点击展开TextView
- Android:TextView的垂直滚动效果和上下滚动效果,原生动画实现
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
- Android系列教程之TextView小组件的使用--附带超链接和跑马灯效果
- android标题栏下面弹出提示框(一) TextView实现,带动画效果
- Android系列教程:TextView小组件的使用--附带超链接和跑马灯效果
- Android系列教程之六:TextView小组件的使用--附带超链接和跑马灯效果
- android 自定义TextView,内容指定位置滚动动画效果
- android自定义TextView实现安卓手机开机android文字Log的动画效果
- Android ViewPager多页面滑动切换以及动画效果
- android代码库之textview跑马灯效果
- Android ViewPager+属性动画 实现炫酷视差动画效果
- Android仿淘宝头条基于TextView实现上下滚动通知效果
- Android ViewPager多页面滑动切换以及动画效果