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

為 Android 界面切換添加簡潔動畫效果

編輯:關於 Android 開發

為 Android 界面切換添加簡潔動畫效果


1. 新建一個位移動畫的 xml 檔案

在 Activity 中開啟動畫

使用 AnimationUtils 類加載動畫資源檔案

left_to_right.xml

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="https://schemas.android.com/apk/res/android" android:fromXDelta="100%" android:toXDelta="0" android:duration="3000" > </translate>
View child=new IndexView(this).getMemberView(); child.startAnimation(AnimationUtils.loadAnimation(this, R.anim.left_to_right));

2. 淡入淡出動畫

當前界面執行淡出動畫及其執行時間

在淡出過程中,淡入界面處於等待狀態

第二個界面執行淡入及其執行時間

第一個界面動畫執行完成後,要將其刪除

package com.tsh.lottery.utils; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; public class FadeUtil { /** * 淡出界面 * @param view 界面 * @param duration 執行時間 */ public static void fadeOut(final View view,long duration){ AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0); alphaAnimation.setDuration(duration); view.startAnimation(alphaAnimation); //監聽動畫結束,刪除View元素 alphaAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { ViewGroup vg=(ViewGroup) view.getParent(); vg.removeView(view); } }); } /** * 淡入界面 * @param view 界面 * @param delay 延遲時間 * @param duration 執行時間 */ public static void fadeIn(View view,long delay,long duration){ AlphaAnimation alphaAnimation=new AlphaAnimation(0, 1); //設置開始時間延遲 alphaAnimation.setStartOffset(delay); alphaAnimation.setDuration(duration); view.startAnimation(alphaAnimation); } }
FadeUtil.fadeOut(child, 2000);
FadeUtil.fadeIn(child, 2000,2000);

透過位移動畫與淡入淡出動畫,可以讓界面切換過程更加自然流暢。淡入淡出的關鍵在於透過 setStartOffset 設定延遲,讓新界面在舊界面淡出後再淡入,並在動畫結束時移除舊界面。

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