刷android,android 组件动画(二)——TextView刷入与刷出的效果


首先看看效果:
刷android,android 组件动画(二)——TextView刷入与刷出的效果
///项目布局
刷android,android 组件动画(二)——TextView刷入与刷出的效果
//// attrs.xml 自定义属性
"1.0" encoding="utf-8"?>

"SlidingText">
"animationDuration" format="integer" />


///// main.xml
"1.0" encoding="utf-8"?>
"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">

android:id="@+id/sliding_textview" android:layout_width="fill_parent"
android:layout_height="wrap_content"
slidingtext:animationDuration="500"
android:layout_gravity="center">
"fill_parent" android:gravity="center_horizontal"
android:layout_height="wrap_content" android:text="sssssss" />


首先自定义名称xmlns:slidingtext=http://schemas.android.com/apk/res/com.testSildingTextView
这里使用了自定义的名称
slidingtext:animationDuration="500"

///// 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 _disibledevent=>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 _disibledevent=>if(showTexts.length!=0){ count = (count + 1) % showTexts.length; text.setText(showTexts[count]); } text.setVisibility(VISIBLE); } @Override public void _disibledevent=>public void _disibledevent=>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 _disibledevent=>public void _disibledevent=>//动画结束时 设置textview的状态 @Override public void _disibledevent=>500); text.setVisibility(INVISIBLE); } }); startAnimation(animation); } };
这个动画效果主要是每次开出一条线程来运行的,首次运行后等2点5秒,就将textview以动画效果向左运行,等0.5秒后从右边出现,动画结束后隐藏,不断循环
本文为原创,如需转载,请注明作者和出处,谢谢!
代码下载
http://files.cnblogs.com/not-code/testSildingTextView.zip
Tags:  刷android

延伸阅读

最新评论

发表评论