Android自訂ProgressDialog
我們在開發Android上應用程式時,有很多時候會遇到“延時”等待的情況,例如資料載入時,尤其是在連網的時候,請求網路會有個等待時間,在這個等待的時間裡需要給使用者一個友好的提示,提示使用者現在正在做什麼操作,需要耐心等待等等,這時一個進度對話方塊就可以解決。Android提供給我們一個很好的控制項叫ProgressDialog,用來建立自訂資訊以及一些相關操作,唯一不好的一點就是Android原生控制項給我一種一如既往的單調和醜陋,下面是原生ProgressDialog的源碼以及效果:
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ProgressDialog dialog = new ProgressDialog(this);dialog.setMessage(登入中……);dialog.show();}}
源碼極其簡單,效果也極其簡陋,怎麼看怎麼不爽。那麼怎樣讓這個ProgressDialog看起來爽點呢?其實不妨做一個自訂的ProgressDialog,先看一下自訂ProgressDialog的效果吧!
好了,看上去不錯吧!下面開始一步一步來實現!
1,自訂ProgressDialog的布局。
2,捲軸的背景設定。
上面的XML布局中可以看到捲軸是一個ImageView,需要給ImageView設定一個動態背景,那這個動態背景該怎麼辦呢?其實就是給ImageView一個動畫背景,給出一定數量的圖片,在動畫中按一定時間勻速切換圖片即可,圖片資源如下:
在res/anim檔案夾建立這樣一個動畫集spinner.xml:
然後給整個的ProgressDialog設定一個背景色progress_custom_bg.xml:
然後還需要給自訂ProgressDialog設定一個風格,在res/value/style.xml下這樣定義:
3,接下來進入重點,我們通過代碼來構建一個自訂的ProgressDialog,具體做法就是自訂類繼承Dialog:
package com.example.myexample;import android.app.Dialog;import android.content.Context;import android.graphics.drawable.AnimationDrawable;import android.view.Gravity;import android.view.View;import android.view.WindowManager;import android.widget.ImageView;import android.widget.TextView;public class CustomProgress extends Dialog {public CustomProgress(Context context) {super(context);}public CustomProgress(Context context, int theme) {super(context, theme);}/** * 當視窗焦點改變時調用 */public void onWindowFocusChanged(boolean hasFocus) {ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView);// 擷取ImageView上的動畫背景AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground();// 開始動畫spinner.start();}/** * 給Dialog設定提示資訊 * * @param message */public void setMessage(CharSequence message) {if (message != null && message.length() > 0) {findViewById(R.id.message).setVisibility(View.VISIBLE);TextView txt = (TextView) findViewById(R.id.message);txt.setText(message);txt.invalidate();}}/** * 彈出自訂ProgressDialog * * @param context * 上下文 * @param message * 提示 * @param cancelable * 是否按返回鍵取消 * @param cancelListener * 按下返回鍵監聽 * @return */public static CustomProgress show(Context context, CharSequence message, boolean cancelable, OnCancelListener cancelListener) {CustomProgress dialog = new CustomProgress(context, R.style.Custom_Progress);dialog.setTitle();dialog.setContentView(R.layout.progress_custom);if (message == null || message.length() == 0) {dialog.findViewById(R.id.message).setVisibility(View.GONE);} else {TextView txt = (TextView) dialog.findViewById(R.id.message);txt.setText(message);}// 按返回鍵是否取消dialog.setCancelable(cancelable);// 監聽返回鍵處理dialog.setOnCancelListener(cancelListener);// 設定置中dialog.getWindow().getAttributes().gravity = Gravity.CENTER;WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();// 設定背景層透明度lp.dimAmount = 0.2f;dialog.getWindow().setAttributes(lp);// dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);dialog.show();return dialog;}}在Activity中引用自訂ProgressDialog:
public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); CustomProgress.show(this, 登入中..., true, null);}}好了,整個流程結束,我們簡單的自訂進度對話方塊就做好了,效果在上面已經了,這裡不再貼出。裡面的代碼我就不解釋了,都比較簡單而且有一些注釋。喜歡的朋友,請在下方點擊下載源碼。
源碼請在這裡下載