Android自定义ProgressDialog进度等待框

来源:国外主机
导读:目前正在解读《Android自定义ProgressDialog进度等待框》的相关信息,《Android自定义ProgressDialog进度等待框》是由用户自行发布的知识型内容!下面请观看由(国外服务器 - www.2bp.net)用户发布《Android自定义ProgressDialog进度等待框》的详细说明。

Android本身已经提供了ProgressDialog进度等待框,使用该Dialog,我们可以为用户提供更好的体验:在网络请求时,弹出此框等待网络数据。 不过,既然是为了提高用户体验,我们肯定希望该Dialog能更加炫酷,让用户看着更舒服。那如何做呢,当然是我们自己定义一个ProgressDialog了。
可以先看下,接下来将实现的Dialog效果图:

Android自定义ProgressDialog进度等待框

步骤1:要定义布局文件,该布局文件即是Dialog的布局了

1

2

3

4

5

6

7

8

9

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:id="@+id/dialog_view"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 android:background="@drawable/dialog_load_bg"

 android:gravity="center"

 android:minHeight="100dp"

 android:minWidth="190dp"

 android:orientation="vertical"

 android:padding="10dp" >

  

 <ImageView

  android:id="@+id/img"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:src="@drawable/publicloading" />

  

 <TextView

  android:id="@+id/tipTextView"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:layout_marginLeft="10dp"

  android:textColor="#acacac"

  android:textSize="15sp" />

  

</LinearLayout>

在布局文件中,我们只定义了两个组件,一个ImageView,用于显示旋转图,一个TextView,用于显示消息文本

步骤2:定义动画,使得弹出框上的图片可以不停的旋转。

1

2

3

4

5

6

7

8

9

<?xml version="1.0" encoding="utf-8"?>

<set android:shareInterpolator="false" xmlns:android="http://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>

步骤3:实现自定义的Dialog逻辑

1

2

3

4

5

6

7

8

9

/**

 * 公用的弹出框

 *

 * @author lining

 */

public class LoadingDialog {

  

 /**

  * 得到自定义的progressDialog

  *

  * @param context

  * @param msg

  * @return

  */

 public static Dialog createLoadingDialog(Context context, String msg) {

  

  // 首先得到整个View

  View view = LayoutInflater.from(context).inflate(

    R.layout.loading_dialog_view, null);

  // 获取整个布局

  LinearLayout layout = (LinearLayout) view

    .findViewById(R.id.dialog_view);

  // 页面中的Img

  ImageView img = (ImageView) view.findViewById(R.id.img);

  // 页面中显示文本

  TextView tipText = (TextView) view.findViewById(R.id.tipTextView);

  

  // 加载动画,动画用户使img图片不停的旋转

  Animation animation = AnimationUtils.loadAnimation(context,

    R.anim.dialog_load_animation);

  // 显示动画

  img.startAnimation(animation);

  // 显示文本

  tipText.setText(msg);

  

  // 创建自定义样式的Dialog

  Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);

  // 设置返回键无效

  loadingDialog.setCancelable(false);

  loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(

    LinearLayout.LayoutParams.MATCH_PARENT,

    LinearLayout.LayoutParams.MATCH_PARENT));

  

  return loadingDialog;

 }

}

代码注释已经很详细了,有一处需要注意的,就是在创建Dialog实例时,需要传递一个theme,该theme是Dialog的风格:

1

2

3

4

5

6

7

8

<!-- 自定义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:windowBackground">@drawable/dialog_load_bg</item>

 <item name="android:windowIsFloating">true</item>

 <item name="android:windowContentOverlay">@null</item>

;/style>

步骤4:使用自定义的ProgressDialog
接下来,我们可以直接使用已经定义好的Dialog了,很简单,只需要将Dialog显示和关闭即可,建议将讲方法封装起来,放在

1

2

3

4

5

6

7

BaseActivity(基类)中,方便随时调用。

/**

 * 显示Dialog

 */

private void showDialog() {

 if (dialog == null) {

  dialog = LoadingDialog.createLoadingDialog(this, "正在加载中...");

  dialog.show();

 }

}

  

/**

 * 关闭Dialog

 */

private void closeDialog() {

 if (dialog != null) {

  dialog.dismiss();

  dialog = null;

 }

}

通过上面步骤,我们即完成了自定义的ProgressDialog,当然,具体在项目中需要什么样的效果,可以调整。

提醒:《Android自定义ProgressDialog进度等待框》最后刷新时间 2023-03-27 02:11:40,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《Android自定义ProgressDialog进度等待框》该内容的真实性请自行鉴别。