项目当中遇到这样一个应用场景:执行某个操作需要耗时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
>
<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
>
<?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
>
<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
>
<style name="OppoTheme" parent="@Android:style/Theme.Dialog.Alert">
<item name="Android:progressBarStyle">@style/ProgressRound</item>
</style>
最后来到生成Dialog的地方:
-
@Override
-
protected
DialogonCreateDialog(
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
;
-
}
-
}
分享到:
相关推荐
android 自定义ProgressDialog对话框,
android自定义ProgressDialog样式详解,一分钟教会你如何自定义android样式。
android 自定义组合progressdialog进度条,方便你的使用............................................................................................
Android中自定义ProgressDialog实现加载滚动条效果,例如显示“请稍后……”,希望对于有帮助
NULL 博文链接:https://duohuoteng.iteye.com/blog/1504193
android 自定义progressDialog实现 加载转圈等待
闲来无事,总结了两个自定义的ProgressDialog,大家可以参考下
详细请参考我的csdn博客:http://blog.csdn.net/allen315410/article/details/41699063
这个demo是关于android中继承Dialog重新自定义一个ProgressDialog。 需求:带加载图片和文字说明,可定时停止也可被动中断。 如有BUG或者不完善,欢迎小伙伴们提出~
主要介绍了Android自定义ProgressDialog系统加载框,通过本文大家可以尝试利用Android自定义ProgressDialog,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了android自定义ProgressDialog加载效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
有时我们在项目执行某些耗时的操作是需要在界面上给出等待之类的信息,在android里面可以使用progressbar或者progressdialog,但是...所以需要自定义一个progressdialog,这个小例子试下了简单的自定义progressdialog
自定义ProgressDialog 更加美观 主要用于耗时操作 比如网络请求等
Android中自定义ProgressDialog实现加载滚动条效果(例如:1.加载中…2.请稍后……等)
Android自定义类似ProgressDialog效果的Dialog
Android本身已经提供了ProgressDialog进度等待框,使用该Dialog,我们可以为用户提供更好的体验:在网络请求时,弹出此框等待网络数据。 不过,既然是为了提高用户体验,我们肯定希望该Dialog能更加炫酷,让用户看着...
主要介绍了实例详解Android自定义ProgressDialog进度条对话框的实现的相关资料,需要的朋友可以参考下
自定义的很漂亮的Android ProgressDialog, 可以拿去修改一下,用在自己的项目中