編輯:關於 Android 程式設計
效果預覽

簡要說明
現在的 Android 應用幾乎離不開網路請求,而擁有良好互動體驗的程式,對這些耗時操作的處理尤其講究。一個得體的載入提示,能讓使用者清楚知道系統正在工作,而不是懷疑程式當掉了。
一、載入提示的版面
dialog_loading.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/dialog_view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@drawable/progress" />
<TextView
android:id="@+id/tipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="數據加載中……" />
</LinearLayout>
這就是自訂的呈現版面,結構單純明瞭:最外層一個垂直排列的線性版面,裡面依序放 ImageView 與 TextView。
二、旋轉動畫
loading_animation.xml:
<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" xmlns:android="https://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="+360"
android:duration="1500"
android:startOffset="-1"
android:repeatMode="restart"
android:repeatCount="-1"/>
</set>
這裡定義的是旋轉動畫的基本屬性,檔案需放在 res 底下的 anim 資料夾中(該資料夾需手動建立)。
三、自訂 CustomProgressDialog
package com.cc.customprogressdialog.util;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
* Created by CC on 2017/2/4.
*/
public class CustomProgressDialog extends Dialog {
Context context;
private ImageView spaceshipImage;
private Animation hyperspaceJumpAnimation;
public CustomProgressDialog(Context context) {
super(context);
this.context = context;
}
public CustomProgressDialog(Context context, int theme) {
super(context, theme);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.dialog_loading, null);// 取得載入用 view
LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 載入版面
// main.xml 中的 ImageView
spaceshipImage = (ImageView) v.findViewById(R.id.img);
// 載入動畫
hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
// 以 ImageView 呈現動畫
spaceshipImage.startAnimation(hyperspaceJumpAnimation);
setCancelable(false);// 不允許以「返回鍵」取消
setContentView(layout, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));// 設定版面
}
}
這個類別就是自訂的 ProgressDialog,關鍵步驟都已在註解中標明。
四、使用方式
package com.cc.customprogressdialog;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.cc.customprogressdialog.util.CustomProgressDialog;
public class MainActivity extends AppCompatActivity {
private Button btn;
private CustomProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new CustomProgressDialog(MainActivity.this, R.style.loading_dialog);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
SystemClock.sleep(2000);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mProgressDialog.dismiss();
}
}.execute();
}
});
}
}
在主 Activity 中放一顆按鈕,點擊後啟動一個非同步任務來模擬網路耗時:任務開始前顯示對話框,任務結束後關閉它。
要注意的是,建立 CustomProgressDialog 時我傳入了一個 style。系統預設的樣式實在不合用,因此必須自行定義:
<!-- 自訂 loading dialog -->
<style name="loading_dialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
這些屬性的具體含義有興趣可自行查閱,這裡不逐一展開。
實作起來不複雜,但非常實用,希望對各位讀者有所幫助。
Android 實現漫天飛舞雪花以及下雨天效果
前言 這個效果的原作者是國外一位大神,我在其基礎上進行了測試,在代碼中補充了不少註釋並做了局部修改。後來我又根據漫天飛舞的雪花效果,改進實現了下雨天場景。原作者的項目是
Android 群英傳筆記(九):系統信息獲取與安全機制
本書也正式進入尾聲。在 Android 的世界裡,不同的軟硬件信息就像一個國家的經濟與軍事水平,各種配置參數代表著一個 Android 帝國的強弱,所以廠商們才熱衷於打「
第5章 全局大喇叭:詳解 Android 廣播機制
1、廣播機制簡介 在 Android 中,每個應用都可以為自己感興趣的廣播進行註冊,從而只接收自己關心的內容。這些廣播可能來自系統,也可能來自其他應用。Android 提
在 Android Framework 中新增字串與圖片資源
改寫 AOSP 時經常需要讓系統層級的程式也能取用自訂的字串或圖片。這與在 App 專案裡新增資源的關鍵差異在於:framework 資源多了一道 public.xml