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

可以先看下,接下来将实现的Dialog效果图:

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

<?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: 定义动画,使得弹出框上的图片可以不停的旋转。

<?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逻辑

/**
* 公用的弹出框
*
* @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的风格:

       <!-- 自定义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显示和关闭即可,建议将讲方法封装起来,放在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,当然,具体在项目中需要什么样的效果,可以调整。

源代码下载地址(免费):http://download.csdn.net/detail/zuiwuyuan/8407657

Android 自定义ProgressDialog的更多相关文章

  1. (转载)Android自定义ProgressDialog进度等待框

    Android自定义ProgressDialog进度等待框 作者:无缘公子 字体:[增加 减小] 类型:转载 时间:2016-01-11我要评论 这篇文章主要介绍了Android自定义Progress ...

  2. android 自定义progressDialog实现

    我们在项目中经常会遇到这样一个应用场景:执行某个耗时操作时,为了安抚用户等待的烦躁心情我们一般会使用进度条之类的空间,在android中让 大家最容易想到的就是progressbar或者progres ...

  3. Android自定义类似ProgressDialog效果的Dialog

    Android自定义类似ProgressDialog效果的Dialog. 方法如下: 1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景). 如我要的效果: 2.定义loadin ...

  4. android 自定义动画

    android自定义动画注意是继承Animation,重写里面的initialize和applyTransformation,在initialize方法做一些初始化的工作,在applyTransfor ...

  5. Android 之 ProgressDialog用法介绍

    布局文件测试: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" androi ...

  6. Android自定义View 画弧形,文字,并增加动画效果

    一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类   B ...

  7. Android自定义View4——统计图View

    1.介绍 周末在逛慕课网的时候,看到了一张学习计划报告图,详细记录了自己一周的学习情况,天天都是0节课啊!正好在学习Android自定义View,于是就想着自己去写了一个,这里先给出一张慕课网的图,和 ...

  8. (转)[原] Android 自定义View 密码框 例子

    遵从准则 暴露您view中所有影响可见外观的属性或者行为. 通过XML添加和设置样式 通过元素的属性来控制其外观和行为,支持和重要事件交流的事件监听器 详细步骤见:Android 自定义View步骤 ...

  9. Android 自定义View合集

    自定义控件学习 https://github.com/GcsSloop/AndroidNote/tree/master/CustomView 小良自定义控件合集 https://github.com/ ...

随机推荐

  1. Kth Minimum Clique

    Kth Minimum Clique 题目描述 Given a vertex-weighted graph with N vertices, find out the K-th minimum wei ...

  2. POJ1635Subway tree systems

    Subway tree systems Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8049   Accepted: 33 ...

  3. UE4物理模块(三)---碰撞查询(下)SAP/MBP/BVH算法简介

    在上一文中介绍了碰撞查询的配置方法: Jerry:UE4物理模块(三)---碰撞查询(上)​zhuanlan.zhihu.com 本篇介绍下UE4的各种零大小的射线检测,以及非零大小(带体积)的射线检 ...

  4. pytest 用 @pytest.mark.usefixtures("fixtureName")或@pytest.fixture(scope="function", autouse=True)装饰,实现类似setup和TearDown的功能

    conftest.py import pytest @pytest.fixture(scope="class") def class_auto(): print("&qu ...

  5. bzoj 2705 [SDOI2012]Longge的问题——欧拉函数大水题

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2705 撕逼题.不就是枚举gcd==d,求和phi[ n/d ]么. 然后预处理sqrt (n ...

  6. js里面的this指向

    1.  this是动态绑定的,或者说是在代码运行期绑定而不是在书写期 function fire () { console.log(this.a) } var obj = { a: 1, fire: ...

  7. Spring Boot → 06:项目实战-账单管理系统

    Spring Boot → 06:项目实战-账单管理系统

  8. 足迹地图 搜索jvectormap

    https://blog.wangjunfeng.com/foot_print/

  9. SQLServer —— 视图

    一.视图的概念 是存储在服务器端的一个查询块,是一张虚拟表. 表示一张表的部分数据或多张表的综合数据. 其结构和数据是建立在对表的查询基础上. 视图的使用,跟对普通的表的查询使用完全一样. 二.视图中 ...

  10. srand函数

    srand函数是随机数发生器的初始化函数. 原型: void srand(unsigned seed); 用法:它需要提供一个种子,这个种子会对应一个随机数,如果使用相同的种子后面的rand()函数会 ...