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

 

首先看看效果:

 

///项目布局

//// 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);
}
};

 

本文为原创,如需转载,请注明作者和出处,谢谢!

代码下载

https://files.cnblogs.com/not-code/testSildingTextView.zip

posted @ 2011-04-10 23:10  没有代码  阅读(6823)  评论(0编辑  收藏  举报