編輯:關於 Android 程式設計
這個元件最早看到的是 V1 版本,後來作者又做了一輪升級,並補寫了原理分析文章。這篇就把用法與重點整理出來,方便直接上手。



畫面中的每一幀都是純手繪完成,可見原作者的基本功相當紮實。
最簡單的方式,直接把對應的 Renderer 包進 LoadingDrawable 再交給 ImageView:
ImageView.setImageDrawable(new LoadingDrawable(new GearLoadingRenderer(Context)));
ImageView.setImageDrawable(new LoadingDrawable(new WhorlLoadingRenderer(Context)));
ImageView.setImageDrawable(new LoadingDrawable(new LevelLoadingRenderer(Context)));
ImageView.setImageDrawable(new LoadingDrawable(new MaterialLoadingRenderer(Context)));
如果想套在任意 View 的背景上,寫法幾乎一樣:
View.setBackground(new LoadingDrawable(new GearLoadingRenderer(Context)));
View.setBackground(new LoadingDrawable(new WhorlLoadingRenderer(Context)));
View.setBackground(new LoadingDrawable(new LevelLoadingRenderer(Context)));
View.setBackground(new LoadingDrawable(new MaterialLoadingRenderer(Context)));
不過,若後續需要對這個 Drawable 做控制(例如啟動、停止),建議用成員變數持有物件,而不是直接 new 一個丟進去。
先在類裡宣告兩個變數:
private ImageView mIvElectricFan;
private LoadingDrawable mElectricFanDrawable;
接著建立 LoadingDrawable 實例,這裡以第一個黃色的效果作為示範:
mElectricFanDrawable = new LoadingDrawable(new ElectricFanLoadingRenderer(this));
然後把 Drawable 設定給 ImageView:
mIvElectricFan.setImageDrawable(mElectricFanDrawable);
mElectricFanDrawable.start(); // 開啟動畫
mElectricFanDrawable.stop(); // 關閉動畫
更細緻的原理剖析在原文中有完整說明,想深入了解的讀者可以往下讀;只想直接用的話,上面這些已經足夠。
原始碼下載:LoadingDrawable(GitHub)
Android Studio 下 NDK 開發全流程踩坑記錄
以前做 NDK 開發都是在 Eclipse 環境下。今天試著用 Android Studio 配置了一遍,結果處處是坑,特此記錄一下。 一、開發步驟 1. 首先創建 Ma
用 XML 佈局改造 Android HelloWorld:從版面到樣式一次搞懂
用 adt-bundle 建好 Android 開發環境之後,出來的預設就是一個 HelloWorld 專案。很多初學者卡住的不是 Java 語法,而是搞不清楚這個專案到
Android 6.0 動態權限踩坑實錄:應對「不再提示」後的權限申請
Android 6.0 之後,一類被稱為「危險權限」的能力不再靠清單文件一聲招呼就能拿到,必須在用戶真正用到時,於運行期動態申請。 在此之前,用戶安裝應用時會看到一大串「
用 CircleImageView 打造圓形頭像的實作教學
圓形頭像在介面設計裡幾乎是標配,本文示範如何藉由 CircleImageView 這個開源元件達成,效果如下。 準備工作 做法不複雜。先在專案裡建一個 circlei