編輯:關於 Android 開發
網路請求時想擋一個半透明的載入動畫,用 Dialog 搭配 frame 動畫是最省事的做法。整個實作分四塊:Dialog 的顯示程式碼、Dialog 的樣式、動畫資源,以及放在 Dialog 裡的布局。
// 顯示載入動畫 dialog = new Dialog(context, R.style.loading); dialog.setContentView(R.layout.loadinglayout); // 該布局內含一個 ProgressBar dialog.setCancelable(true); // 允許取消 dialog.show(); WindowManager.LayoutParams params = dialog.getWindow().getAttributes(); params.height = LayoutParams.MATCH_PARENT; params.width = LayoutParams.MATCH_PARENT; params.format = PixelFormat.TRANSLUCENT; params.flags = LayoutParams.FLAG_FULLSCREEN | LayoutParams.FLAG_LAYOUT_IN_SCREEN; dialog.getWindow().setAttributes(params); // 需要關閉時 dialog.cancel();
loadinglayout.xml,就是把 ProgressBar 置中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ProgressBar
android:id="@+id/loadingbar"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:layout_gravity="center" />
</LinearLayout>
Dialog 用 loading 樣式讓背景透明、不要標題、不落陰影;ProgressBar 則用 loadingStyle 指定自訂的 frame 動畫:
<!-- 以 frame 動畫製作的 logo 炫動效果 -->
<style name="loadingStyle">
<item name="android:indeterminateDrawable">@anim/loading</item>
</style>
<style name="loading">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item> <!-- 不做背景模糊 -->
<item name="android:windowIsFloating">true</item> <!-- 類似 Windows 的彈出視窗 -->
<item name="android:windowContentOverlay">@null</item> <!-- 不要頂部陰影 -->
<item name="android:windowBackground">@color/transparent</item> <!-- 背景設為透明 -->
</style>
res/anim/loading.xml,用 animation-list 把六張圖輪播:
<?xml version="1.0" encoding="UTF-8"?>
<animation-list android:oneshot="false"
xmlns:android="https://schemas.android.com/apk/res/android">
<item android:duration="150" android:drawable="@drawable/load1" />
<item android:duration="150" android:drawable="@drawable/load2" />
<item android:duration="150" android:drawable="@drawable/load3" />
<item android:duration="150" android:drawable="@drawable/load4" />
<item android:duration="150" android:drawable="@drawable/load5" />
<item android:duration="150" android:drawable="@drawable/load6" />
</animation-list>
oneshot="false" 表示無限循環,每幀 150 毫秒即為約 6.7 幀/秒的轉速;想轉快一點就把 duration 調小。實務上還要在請求結束的 onPostExecute() 或網路回呼中確實呼叫 cancel(),否則動畫會一直轉下去。
Android 開發(3):Intent 與 Bundle 傳值、ListView 應用與 RelativeLayout 通訊錄實作
前言 本篇要處理的是好幾個常一起出現的知識點:Intent 與 Bundle 在頁面跳轉中的用法、ListView 與各種 Adapter 的搭配,順便把 Relativ
Android 基礎之 Activity:創建、任務棧、啟動模式與生命週期
Activity 是 Android 四大組件之一,也是整個組件體系中的重中之重。本文將從 Activity 的基本概念出發,系統講解它的創建方式、任務棧、啟動模式以及完
Android 斷點續傳下載原理與實作要點
Android 斷點續傳下載原理與實作要點斷點續傳的核心是把下載任務分段,並記錄已完成的位置。若網路中斷,下次可從上次位置繼續,而不必從頭下載。多執行緒下載還可把檔案拆成
Android 學習之路:從菜鳥到入門的完整指南
Android 學習之路 本文記錄了一位開發者自學 Android 的整個歷程,並整理給新手們的建議,希望能幫正在或準備踏入程式開發領域的朋友少走一些彎路。 硬件準備 電