編輯:關於 Android 程式設計
先來看看我們要做出來的「透明進度對話框」長什麼樣子——它沒有傳統對話框的邊框與背景,只有一個持續旋轉的加載動畫,乾淨又低調。

1、建立專案
用 Eclipse 建立一個新的 Android 專案,建議使用 Android 2.2 或以上版本。
2、建立版面配置 activity_main.xml
在 /res/layout 資料夾建立線性版面配置,主要用來加入一個文字標籤和一個按鈕:
複製程式碼 程式碼如下:
activity_main.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="8dp"
android:textSize="20sp"
android:text="Transparent Progress Indicator" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check it out!"
android:layout_marginTop="40dp"
android:layout_gravity="center"
android:id="@+id/the_button" />
</LinearLayout>
3、在 styles.xml 中定義透明對話框樣式
在 /res/values 中打開 styles.xml,加入透明對話框的樣式。請務必指定 parent 屬性,否則運行時會出問題:
複製程式碼 程式碼如下:
styles.xml
<resources>
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<style name="AppTheme" parent="AppBaseTheme">
</style>
<!-- Transparent dialog -->
<style name="TransparentProgressDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTitleStyle">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:background">@android:color/transparent</item>
</style>
</resources>
4、加入旋轉動畫圖片
在 /res 中放入一張會動態旋轉的加載圖片(如 spinner)。

5、實作 MainActivity.java
複製程式碼 程式碼如下:
MainActivity.java
package com.authorwjf.transparentprogressdialog;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener {
private TransparentProgressDialog pd;
private Handler h;
private Runnable r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h = new Handler();
pd = new TransparentProgressDialog(this, R.drawable.spinner);
r =new Runnable() {
@Override
public void run() {
if (pd.isShowing()) {
pd.dismiss();
}
}
};
findViewById(R.id.the_button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
pd.show();
h.postDelayed(r,5000);
}
@Override
protected void onDestroy() {
h.removeCallbacks(r);
if (pd.isShowing() ) {
pd.dismiss();
}
super.onDestroy();
}
}
6、實作透明動畫的核心程式碼
複製程式碼 程式碼如下:
private class TransparentProgressDialog extends Dialog {
private ImageView iv;
public TransparentProgressDialog(Context context, int resourceIdOfImage) {
super(context, R.style.TransparentProgressDialog);
WindowManager.LayoutParams wlmp = getWindow().getAttributes();
wlmp.gravity = Gravity.CENTER_HORIZONTAL;
getWindow().setAttributes(wlmp);
setTitle(null);
setCancelable(false);
setOnCancelListener(null);
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
iv = new ImageView(context);
iv.setImageResource(resourceIdOfImage);
layout.addView(iv, params);
addContentView(layout, params);
}
@Override
public void show() {
super.show();
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(3000);
iv.setAnimation(anim);
iv.startAnimation(anim);
}
}
關鍵在於:自定義 Dialog 沿用 TransparentProgressDialog 樣式,去掉視窗邊框與背景;show() 時為 ImageView 掛上一個無限循環的 RotateAnimation,就能呈現持續旋轉的加載效果。點擊按鈕後弹出,5 秒後自動關閉。最終效果如下:

OSGi ServiceListener實戰:動態監聽服務註冊與變更
前言 在OSGi的世界裡,動態性是最鮮明的特徵之一。Bundle可以在執行期安裝、啟動、停止、更新與移除,而服務也可能隨時被註冊、修改或取消註冊。若應用需要即時感知這些變
Servlet 學習筆記(六):Session 會話追蹤完整解析
Session 與 Cookie 有著相似之處,兩者都能用來記錄資料與狀態;真正的差異在於儲存的位置:Cookie 把資料保存在用戶端,而 Session 則把資料保存在
手機就能做圖片教程 兩種簡便方法分享
不少人習慣把截圖傳到電腦,用PS標註加工後再發到社交平台,來回折騰其實並不省事。事實上Android手機完全可以勝任這件事,而且並不需要專業的修圖軟件。 這裡分享兩種在手
Android 分組 ListView 實作詳解
只要有大量資料需要呈現,幾乎就一定會面臨分組的需求。分組的形式很多,最常見的是把分組標題直接鑲嵌在清單裡,像 Android 內建聯絡人就依拼音首字母(A、B、C……)分