Android 自定义ProgressDialog
2024-09-06 15:31:57
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
最新文章
- CGI与FastCGI nginx+PHP-FPM
- 34 网络相关函数(二)——live555源码阅读(四)网络
- ThinkPHP 3.2.3 数据缓存与静态缓存
- Redis 三:存储类型之字符串
- POJ 1275 Cashier Employment(差分约束)
- SKSpriteNode类
- Cplus
- Ubuntu root 密码设置及远程登录
- C++ STL 优先队列详解
- JS跨域请求 JSONP B/S全代码
- 织梦5.7sp1最新问题:后台不显示编辑器
- vue路由--网站导航功能
- linux常用命令 cut字符截取命令
- Ruby面向对象
- 进程间通信(IPC)
- Java基础-多线程-③线程同步之synchronized
- 用flask Flask-RESTful,实现RESTful API
- 20165228 2017-2018-2《Java程序设计》课程总结
- 学以致用八---centos7.2 安装vim8+支持python3
- Mac下,如何把Github上的仓库删除掉