實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
Android SDK已經(jīng)提供有進(jìn)度條組件ProgressDialog組件,但用的時(shí)候我們會(huì)發(fā)現(xiàn)可能風(fēng)格與我們應(yīng)用的整體風(fēng)格不太搭配,而且ProgressDialog的可定制行也不太強(qiáng),這時(shí)就需要我們自定義實(shí)現(xiàn)一個(gè)ProgressDialog。
通過(guò)看源碼我們發(fā)現(xiàn),ProgressDialog繼承自Alertdialog,有一個(gè)ProgressBar和兩個(gè)TextView組成的,通過(guò)對(duì)ProgressDialog的源碼進(jìn)行改進(jìn)就可以實(shí)現(xiàn)一個(gè)自定義的ProgressDialog。
1、效果:
首先看一下自定義CommonProgressDialog和原生ProgressDialog的對(duì)比:


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進(jìn)度條圖片和背景圖片設(shè)置
<?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;
}
}
測(cè)試程序:
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);
}
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android自定義對(duì)話框Dialog的簡(jiǎn)單實(shí)現(xiàn)
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- 詳解Android 全局彈出對(duì)話框SYSTEM_ALERT_WINDOW權(quán)限
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android 之BottomsheetDialogFragment仿抖音評(píng)論底部彈出對(duì)話框效果(實(shí)例代碼)
- Android實(shí)現(xiàn)退出界面彈出提示對(duì)話框
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android仿QQ消息提示實(shí)現(xiàn)彈出式對(duì)話框
- Android對(duì)話框使用方法詳解
相關(guān)文章
Android 6.0權(quán)限請(qǐng)求相關(guān)及權(quán)限分組方法
今天小編就為大家分享一篇Android 6.0權(quán)限請(qǐng)求相關(guān)及權(quán)限分組方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Kotlin標(biāo)準(zhǔn)函數(shù)與靜態(tài)方法基礎(chǔ)知識(shí)詳解
Kotlin中的標(biāo)準(zhǔn)函數(shù)指的是Standard.kt文件中定義的函數(shù),任何Kotlin代碼都可以自由地調(diào)用所有的標(biāo)準(zhǔn)函數(shù)。例如let這個(gè)標(biāo)準(zhǔn)函數(shù),他的主要作用就是配合?.操作符來(lái)進(jìn)行輔助判空處理2022-11-11
Android List(集合)中的對(duì)象以某一個(gè)字段排序案例
這篇文章主要介紹了Android List(集合)中的對(duì)象以某一個(gè)字段排序案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
Android實(shí)現(xiàn)多級(jí)樹(shù)形選擇列表
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多級(jí)樹(shù)形選擇列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
Android App實(shí)現(xiàn)應(yīng)用內(nèi)部自動(dòng)更新的最基本方法示例
這篇文章主要介紹了實(shí)現(xiàn)Android App內(nèi)部自動(dòng)更新的最基本方法示例,包括IIS服務(wù)器端的簡(jiǎn)單布置,需要的朋友可以參考下2016-03-03
淺談Android Service服務(wù)的高級(jí)技巧
這篇文章主要介紹了淺談Android 服務(wù)的高級(jí)技巧,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android編程簡(jiǎn)易實(shí)現(xiàn)XML解析的方法詳解
這篇文章主要介紹了Android編程簡(jiǎn)易實(shí)現(xiàn)XML解析的方法,結(jié)合實(shí)例形式總結(jié)分析了Android操作xml文件的各種常見(jiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08

