Android ProgressDialog 自定义方法

原文:http://www.oschina.net/question/8676_11797

项目当中遇到这样一个应用场景:执行某个操作需要耗时15秒以上,依照惯例,这就要使用到进度条一类的UI控件,以安抚用户等待的烦躁心情。Android Framework已经提供了ProgressDialog,可以很好的解决这个问题。

ProgressDialog实际上是AlertDialog的子类,其有着两种不同的表现形式。第一种是针对没有明确的进度,不知道当前完成了多少的情况,此时使用一个转动的圆环来展现;第二种是针对有了明确的总进度,并知道当前的完成比例等信息,此时使用的是一个横条来展现。根据项目方案,我们的效果类似第一种情形。

不过我所处的项目情况比较特殊,因为由于设备的特性导致不能频繁刷新屏幕,–刷新一次屏幕的时间大于2s。因此凡是动画之类的效果统统禁用,原生的ProgressDialog也就不能使用了,只能另想解决办法。

解决问题的办法之一就是修改ProgressDialog的默认实现,使其不要频繁刷新。修改之前要先清楚其是怎么实现的。通过源代码可以知道,ProgressDiglog是通过在themes.xml定义progressBarStyle,进一步从styles.xml中找到 Widget.ProgressBar的样式定义:

<style name="Widget.ProgressBar">
<item name="Android:indeterminateOnly">true</item>
<item name="Android:indeterminateDrawable">@android:drawable/progress_medium</item>
<item name="Android:indeterminateBehavior">repeat</item>
</style>

如果没猜错的话,接下来要做事情就变成了修改Android:indeterminateDrawable对应的drawable。

先向drawable文件夹下增加两个图片progress_round_1.png和progress_round_2.png,然后增加一个XML文件progress_round.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/progress_round_1" android:duration="2000" />
<item Android:drawable="@drawable/progress_round_2" android:duration="2000" />
</animation-list>

这样不断转动的圆环变成了自己想要的图片。

接着增加相应的style:

<style name="ProgressRound">
<item name="Android:indeterminateDrawable">@drawable/progress_round</item>
</style>

然后增加相应的theme:

<style name="OppoTheme" parent="@Android:style/Theme.Dialog.Alert">
<item name="Android:progressBarStyle">@style/ProgressRound</item>
</style>

最后来到生成Dialog的地方:

@Override
protected Dialog onCreateDialog(int id) {
Log.d(TAG, "onCreateDialog");

switch (id) {
case PRODIALOG_KEY:
mProDialog = new ProgressDialog(this, R.style.OppoTheme);
mProDialog.setIndeterminate(true);
mProDialog.setCancelable(true);
return mProDialog;
default:
return null;
}
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想要在 Android 应用使用自定义的加载动画,可以通过创建自定义的布局和使用 Animation 类来实现。以下是一个简单的示例,演示如何在 Android 应用自定义加载动画: 首先,在 res/layout 目录下创建一个新的布局文件,例如 custom_loading.xml,其包含自定义的加载动画布局,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <ImageView android:id="@+id/loading_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/custom_loading_animation" android:layout_marginBottom="16dp"/> <TextView android:id="@+id/loading_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Loading..." android:textColor="@android:color/white"/> </LinearLayout> ``` 其,loading_image 是一个自定义的加载动画的 ImageView,loading_text 是一个文本视图,用于显示加载消息。 接下来,在 res/drawable 目录下创建一个新的动画文件,例如 custom_loading_animation.xml,其包含自定义的加载动画动画,例如: ```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/custom_loading_frame1" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame2" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame3" android:duration="100" /> <item android:drawable="@drawable/custom_loading_frame4" android:duration="100" /> </animation-list> ``` 其,custom_loading_frame1、custom_loading_frame2、custom_loading_frame3、custom_loading_frame4 是自定义的加载动画的帧。 最后,在你的 Activity ,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 Animation 类将 custom_loading_animation.xml 动画文件加载到 ImageView ,例如: ```java LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom_loading, null); ImageView imageView = (ImageView) view.findViewById(R.id.loading_image); Animation animation = AnimationUtils.loadAnimation(this, R.drawable.custom_loading_animation); imageView.startAnimation(animation); ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setCancelable(false); progressDialog.show(); progressDialog.setContentView(view); ``` 其,使用 LayoutInflater 类将 custom_loading.xml 布局文件实例化为 View 对象,并使用 findViewById() 方法获取 loading_image ImageView 对象。然后,使用 AnimationUtils.loadAnimation() 方法将 custom_loading_animation.xml 动画文件加载到 ImageView ,并调用 startAnimation() 方法开始播放动画。最后,将 View 对象设置为 ProgressDialog 的内容视图,调用 show() 方法显示 ProgressDialog
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值