Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> android 組件動畫(二)——TextView刷入與刷出的效果

android 組件動畫(二)——TextView刷入與刷出的效果

編輯:Android開發實例

首先看看效果:

 

///項目布局

//// 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"

 

 

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

這個動畫效果主要是每次開出一條線程來運行的,首次運行後等2點5秒,就將textview以動畫效果向左運行,等0.5秒後從右邊出現,動畫結束後隱藏,不斷循環 

代碼下載

testSildingTextView.zip

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved