編輯:關於 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 設定延遲,讓新界面在舊界面淡出後再淡入,並在動畫結束時移除舊界面。
Android Bottom Sheet 完整解析:靠 Behavior 幾行代碼搞定底部面板
Support Library 升級帶來的驚喜 Android 的 Support Library 更新到 23.2 版本後,官方博客裡一口氣放出了幾個相當實用的新東西,
AsyncTask 全面透視:從簡單封裝到自定義線程池的實踐之路
AsyncTask 全面透視:從簡單封裝到自定義線程池的實踐之路 前言 近期在開發 SDK 的過程中,需要為它設計一套可用的線程池方案。翻了不少資料卻始終找不到切入點,直
Android 開發常見錯誤匯總與解決方案
本文整理 Android 開發過程中經常遇到的錯誤訊息與對應的解決辦法,方便日後查閱。 1. 未指定 API 等級警告 訊息:Application does not s
SharedPreferences 實現記住密碼功能
SharedPreferences 實現記住密碼功能 SharedPreferences 常用於保存簡單的鍵值對資料,例如帳號、密碼勾選狀態與其他登入設定。本文以登入頁面