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

來源:互聯網
上載者:User

 

首先看看效果:

 

///項目布局

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.