欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

实例详解Android自定义ProgressDialog进度条对话框的实现

程序员文章站 2023-12-21 19:33:52
android sdk已经提供有进度条组件progressdialog组件,但用的时候我们会发现可能风格与我们应用的整体风格不太搭配,而且progressdialog的可定...

android sdk已经提供有进度条组件progressdialog组件,但用的时候我们会发现可能风格与我们应用的整体风格不太搭配,而且progressdialog的可定制行也不太强,这时就需要我们自定义实现一个progressdialog。

通过看源码我们发现,progressdialog继承自alertdialog,有一个progressbar和两个textview组成的,通过对progressdialog的源码进行改进就可以实现一个自定义的progressdialog。

1、效果:

首先看一下自定义commonprogressdialog和原生progressdialog的对比:

实例详解Android自定义ProgressDialog进度条对话框的实现

实例详解Android自定义ProgressDialog进度条对话框的实现

2、代码:

common_progress_dialog.xml 布局文件: 
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<linearlayout 
android:layout_width="798px"
android:layout_height="460px"
android:orientation="vertical"
android:background="@drawable/common_progress_dialog_background">
<textview
android:id="@+id/progress_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textsize="44px"
android:layout_margintop="113px"
android:layout_gravity="center_horizontal"
android:textcolor="#ffffff"
/>
<progressbar 
android:id="@+id/progress"
style="?android:attr/progressbarstylehorizontal"
android:layout_width="712px"
android:layout_height="30px"
android:layout_margintop="100px"
android:layout_marginleft="47px"
android:layout_centerhorizontal="true"
android:progressdrawable="@drawable/common_progressdialog_progressbar_background"
/>
<linearlayout 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<textview
android:id="@+id/progress_percent"
android:layout_width="80px"
android:layout_height="wrap_content"
android:textsize="30px"
android:layout_marginleft="280px"
android:gravity="center_horizontal"
android:textcolor="#ffffff"
/>
<textview
android:id="@+id/progress_number"
android:layout_width="250px"
android:layout_height="wrap_content"
android:layout_marginleft="120px"
android:textsize="30px"
android:gravity="center_horizontal"
android:textcolor="#ffffff"
/>
</linearlayout>
</linearlayout>
</framelayout>
common_progressdialog_progressbar_background.xml progressbar进度条图片和背景图片设置 
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:id="@android:id/background"
android:drawable="@drawable/common_progress_dialog_progressbar3"
/>
<item
android:id="@android:id/progress"
android:drawable="@drawable/common_progress_dialog_progressbar2"
/>
</layer-list>
commonprogressdialog.java类: 
package com.johnny.testactivity;
import java.text.numberformat;
import android.app.alertdialog;
import android.app.progressdialog;
import android.content.context;
import android.os.bundle;
import android.os.handler;
import android.os.message;
import android.text.spannable;
import android.text.spannablestring;
import android.text.style.stylespan;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.widget.progressbar;
import android.widget.textview;
public class commonprogressdialog extends alertdialog {
private progressbar mprogress;
private textview mprogressnumber;
private textview mprogresspercent;
private textview mprogressmessage;
private handler mviewupdatehandler;
private int mmax;
private charsequence mmessage;
private boolean mhasstarted;
private int mprogressval;
private string tag="commonprogressdialog";
private string mprogressnumberformat;
private numberformat mprogresspercentformat;
public commonprogressdialog(context context) {
super(context);
// todo auto-generated constructor stub
initformats();
}
@override
protected void oncreate(bundle savedinstancestate) {
// todo auto-generated method stub
super.oncreate(savedinstancestate);
setcontentview(r.layout.common_progress_dialog);
mprogress=(progressbar) findviewbyid(r.id.progress);
mprogressnumber=(textview) findviewbyid(r.id.progress_number);
mprogresspercent=(textview) findviewbyid(r.id.progress_percent);
mprogressmessage=(textview) findviewbyid(r.id.progress_message);
// layoutinflater inflater = layoutinflater.from(getcontext());
mviewupdatehandler = new handler() {
@override
public void handlemessage(message msg) {
// todo auto-generated method stub
super.handlemessage(msg);
int progress = mprogress.getprogress();
int max = mprogress.getmax();
double dprogress = (double)progress/(double)(1024 * 1024);
double dmax = (double)max/(double)(1024 * 1024);
if (mprogressnumberformat != null) {
string format = mprogressnumberformat;
mprogressnumber.settext(string.format(format, dprogress, dmax));
} else {
mprogressnumber.settext("");
}
if (mprogresspercentformat != null) {
double percent = (double) progress / (double) max;
spannablestring tmp = new spannablestring(mprogresspercentformat.format(percent));
tmp.setspan(new stylespan(android.graphics.typeface.bold),
0, tmp.length(), spannable.span_exclusive_exclusive);
mprogresspercent.settext(tmp);
} else {
mprogresspercent.settext("");
}
}
};
// view view = inflater.inflate(r.layout.common_progress_dialog, null);
// mprogress = (progressbar) view.findviewbyid(r.id.progress);
// mprogressnumber = (textview) view.findviewbyid(r.id.progress_number);
// mprogresspercent = (textview) view.findviewbyid(r.id.progress_percent);
// setview(view);
//mprogress.setmax(100);
onprogresschanged();
if (mmessage != null) {
setmessage(mmessage);
}
if (mmax > 0) {
setmax(mmax);
}
if (mprogressval > 0) {
setprogress(mprogressval);
}
}
private void initformats() {
mprogressnumberformat = "%1.2fm/%2.2fm";
mprogresspercentformat = numberformat.getpercentinstance();
mprogresspercentformat.setmaximumfractiondigits(0);
}
private void onprogresschanged() {
mviewupdatehandler.sendemptymessage(0);
}
public void setprogressstyle(int style) {
//mprogressstyle = style;
}
public int getmax() {
if (mprogress != null) {
return mprogress.getmax();
}
return mmax;
}
public void setmax(int max) {
if (mprogress != null) {
mprogress.setmax(max);
onprogresschanged();
} else {
mmax = max;
}
}
public void setindeterminate(boolean indeterminate) {
if (mprogress != null) {
mprogress.setindeterminate(indeterminate);
} 
// else {
// mindeterminate = indeterminate;
// }
}
public void setprogress(int value) {
if (mhasstarted) {
mprogress.setprogress(value);
onprogresschanged();
} else {
mprogressval = value;
} 
}
@override
public void setmessage(charsequence message) {
// todo auto-generated method stub
//super.setmessage(message);
if(mprogressmessage!=null){
mprogressmessage.settext(message);
}
else{
mmessage = message;
}
}
@override
protected void onstart() {
// todo auto-generated method stub
super.onstart();
mhasstarted = true;
}
@override
protected void onstop() {
// todo auto-generated method stub
super.onstop();
mhasstarted = false;
}
}

测试程序:

private void showdialog(){
mdialog = new commonprogressdialog(this);
mdialog.setmessage("正在下载");
mdialog.setprogressstyle(progressdialog.style_horizontal);
mdialog.setoncancellistener(new oncancellistener() {
@override
public void oncancel(dialoginterface dialog) {
// todo auto-generated method stub
//cancel(true);
}
});
mdialog.show();
mdialog.setmax(100*1024*1024);
mdialog.setprogress(65*1024*1024);
}

上一篇:

下一篇: