在项目中,我们经常需要通过网络请求去服务端获取相应的数据,以便于在客户端进行展示。而这个过程是需要网络的,因此就有了等待的过程。对于网络快的童靴,那么等待的时间就短;而对于网络慢的童靴,那么等待的时间就长。因此为了消除童靴们等待的焦虑感,我们需要显示一个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;
}
@Override
public 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);
else
loadingTextView.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。