Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

我的 Android 學習筆記 39:用 Alpha 動畫打造漸變啟動頁

編輯:關於 Android 開發

我的 Android 學習筆記 39:用 Alpha 動畫打造漸變啟動頁


還記得第一次打開 QQ 時,那隻漸漸浮現的企鵝嗎?那種由淡到濃的漸變效果,其實只要透過一個簡單的 Alpha 動畫就能實現。本文記錄了如何用 AlphaAnimation 做出啟動頁的漸顯效果,並在動畫結束後自動跳轉到下一個頁面。

package com.moonweather.app.activity; import com.moonweather.app.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.ImageView; public class ShouyeActivity extends Activity { private ImageView ima;; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shouye); ima=(ImageView) findViewById(R.id.ima); AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f, 1.0f); alphaAnimation.setDuration(3*1000); alphaAnimation.setRepeatCount(0); ima.startAnimation(alphaAnimation); alphaAnimation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { //這裡可以寫動畫結束後的事件,比如說跳轉 Intent intent=new Intent(ShouyeActivity.this,ChooseAreaActivity.class); startActivity(intent); ShouyeActivity.this.finish(); } }); } }

核心思路很簡單:建立一個從 0.3f 到 1.0f 的 AlphaAnimation,透明度由淡到濃,設定持續時間為 3 秒,然後透過 setAnimationListener 監聽動畫結束事件,在 onAnimationEnd 中完成頁面跳轉並關閉當前啟動頁。這樣一來,使用者便能看到一個流暢自然的漸顯過場。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved