Android ProgressDialog 控制項自訂

來源:互聯網
上載者:User

標籤:android   progressdialog   

在項目中,我們經常需要通過網路請求去服務端擷取相應的資料,以便於在用戶端進行展示。而這個過程是需要網路的,因此就有了等待的過程。對於網路快的童靴,那麼等待的時間就短;而對於網路慢的童靴,那麼等待的時間就長。因此為了消除童靴們等待的焦慮感,我們需要顯示一個progress dialog來提示童靴們,資料正在擷取中,請稍候片刻。


先上,如下:

   


那麼如何?呢?直接上代碼


1. 在anim檔案夾下建立sf_progress_dialog_anim.xml,實現轉動的效果:

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false" >    <item        android:drawable="@drawable/sf_progress_1"        android:duration="200"/>    <item        android:drawable="@drawable/sf_progress_2"        android:duration="200"/>    <item        android:drawable="@drawable/sf_progress_3"        android:duration="200"/>    <item        android:drawable="@drawable/sf_progress_4"        android:duration="200"/>    <item        android:drawable="@drawable/sf_progress_5"        android:duration="200"/>    <item        android:drawable="@drawable/sf_progress_6"        android:duration="200"/>    <item        android:drawable="@drawable/sf_progress_7"        android:duration="200"/>    <item        android:drawable="@drawable/sf_progress_8"        android:duration="60"/></animation-list>
其中:android:oneshot表示動畫只播放一次停留在最後一幀上,當設定為false時,則代表動畫迴圈播放;否則,則代表動畫只播放一次。

資源檔可到如下連結進行下載:

http://download.csdn.net/detail/shenjichao2008/8248073


2. 在values檔案夾中建立style.xml,自訂progress dialog的樣式:

<style name="SF_dialogCustom" parent="@android:style/Theme.Dialog">        <item name="android:windowFrame">@null</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowContentOverlay">@null</item>        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>    </style>    <style name="SF_pressDialogCustom" parent="@style/SF_dialogCustom">        <item name="android:windowBackground">@android:color/transparent</item>         <item name="android:windowNoTitle">true</item>    </style>

3. 在layout檔案夾中建立sf_view_custom_progress_dialog.xml,自訂progress dilaog的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <ImageView        android:id="@+id/sf_iv_progress_dialog_loading"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@anim/sf_progress_dialog_anim"        android:contentDescription="@string/sf_progress_dialog_image_loading" />    <TextView        android:id="@+id/sf_tv_progress_dialog_loading"        style="@style/SF_MediumLightGreyTxt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical" /></LinearLayout>

4. 建立SFProgrssDialog類,繼承Dialog,實現如下:

package com.snapfish.view;import com.snapfish.R;import android.app.Dialog;import android.content.Context;import android.graphics.drawable.AnimationDrawable;import android.text.TextUtils;import android.view.Gravity;import android.widget.ImageView;import android.widget.TextView;public class SFProgrssDialog extends Dialog {private static SFProgrssDialog m_progrssDialog;private SFProgrssDialog(Context context, int theme) {super(context, theme);}public static SFProgrssDialog createProgrssDialog(Context context) {m_progrssDialog = new SFProgrssDialog(context,R.style.SF_pressDialogCustom);m_progrssDialog.setContentView(R.layout.sf_view_custom_progress_dialog);m_progrssDialog.getWindow().getAttributes().gravity = Gravity.CENTER;return m_progrssDialog;}@Overridepublic void onWindowFocusChanged(boolean hasFocus) {if (null == m_progrssDialog)return;ImageView loadingImageView = (ImageView) m_progrssDialog.findViewById(R.id.sf_iv_progress_dialog_loading);AnimationDrawable animationDrawable = (AnimationDrawable) loadingImageView.getBackground();animationDrawable.start();}public SFProgrssDialog setMessage(String msg) {TextView loadingTextView = (TextView) m_progrssDialog.findViewById(R.id.sf_tv_progress_dialog_loading);if (!TextUtils.isEmpty(msg))loadingTextView.setText(msg);elseloadingTextView.setText(R.string.sf_progress_dialog_image_loading);return m_progrssDialog;}}

5. 編寫顯示/隱藏 progress dialog的方法:

private SFProgrssDialog m_customProgrssDialog;final void showCustomProgrssDialog(String msg) {if (null == m_customProgrssDialog)m_customProgrssDialog = SFProgrssDialog.createProgrssDialog(m_parent);if (null != m_customProgrssDialog) {m_customProgrssDialog.setMessage(msg);m_customProgrssDialog.show();m_customProgrssDialog.setCancelable(false);}}final void hideCustomProgressDialog() {if (null != m_customProgrssDialog) {m_customProgrssDialog.dismiss();m_customProgrssDialog = null;}}


6. 在網路請求之前,調用showCustomProgrssDialog方法,傳入顯示的message;在網路響應之後,調用hideProgressDialog方法,消除progress dialog。


Android ProgressDialog 控制項自訂

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.