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

 

首先看看效果:

 2011041022523753.gif

///项目布局

2011041022530693.png

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

转载于:https://www.cnblogs.com/not-code/archive/2011/04/10/2011929.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值