首先看看效果:
///項目布局
//// 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