由于原生的ProgressDialog的进度条的格式是:0/100这样的,这样就不符合某些项目的需求了!刚好最近项目上对这块下载的需要改善,所以我参照了一些网上的大神所写的代码,自己在另外改造了一下!!!如有雷同,也没啥关系!下面请先看效果图,在看代码~
效果图:
代码:
一:先看下这个自定义的ProgressDialog,我自己添加了一个取消的按钮,测试时是自己模拟了断点更新的功能!
import android.app.AlertDialog;
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.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.text.NumberFormat;
/*
* @创建者 wjh
* @创建时间 2016/8/5 21:22
* @描述 ${自定义下载的进度提示框}
*/
public class CommonProgressDialog extends AlertDialog implements View.OnClickListener {
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;
private TextView tvCancel;
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);
tvCancel = (TextView) findViewById(R.id.tv_cancel);
// 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("");
}
}
};
onProgressChanged();
if (mMessage != null) {
setMessage(mMessage);
}
if (mMax > 0) {
setMax(mMax);
}
if (mProgressVal > 0) {
setProgress(mProgressVal);
}
//设置监听
tvCancel.setOnClickListener(this);
}
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;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_cancel://取消
if (mOnOkAndCancelListener!=null) {
mOnOkAndCancelListener.onCancel(v);
}
break;
default:
break;
}
}
/**声明成员变量*/
public OnOkAndCancelListener mOnOkAndCancelListener;
/**暴露接口取消方法,如果需要确定,另行添加*/
public interface OnOkAndCancelListener {
void onCancel(View v);
}
/**暴露方法,设置监听*/
public void setOnOkAndCancelListener(OnOkAndCancelListener mOnOkAndCancelListener) {
this.mOnOkAndCancelListener = mOnOkAndCancelListener;
}
}
二: ProgressDialog布局 common_progress_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--@drawable/common_progress_dialog_background"-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff">
<!--Title-->
<TextView
android:id="@+id/progress_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginTop="30dp"
android:layout_gravity="center_horizontal"
android:textColor="#000"
android:text="234"
/>
<!--进度-->
<ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:progressDrawable="@drawable/common_progressbar_bg"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!--左边的-->
<TextView
android:layout_weight="1"
android:id="@+id/progress_percent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="18sp"
android:gravity="center_horizontal"
android:textColor="#000"
android:text="3243"
/>
<!--右边的-->
<TextView
android:id="@+id/progress_number"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="16sp"
android:gravity="center_horizontal"
android:textColor="#000"
android:text="23424sdfsf2"
/>
</LinearLayout>
<TextView
android:id="@+id/tv_cancel"
android:layout_marginTop="20dp"
android:padding="10dp"
android:layout_marginRight="30dp"
android:layout_gravity="right"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
/>
</LinearLayout>
</FrameLayout>
三: ProgressBar的样式common_progressbar_bg.xml 样式自己可以根据项目主题设置
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" >
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip >
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#440000ff"
android:centerColor="#0000ff"
android:centerY="0.75"
android:endColor="#880000ff"
android:angle="270"
/>
</shape>
</clip>
</item>
</layer-list>
四: 如何使用?请看如下测试类MainActivity.java
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener, CommonProgressDialog.OnOkAndCancelListener {
private CommonProgressDialog mDialog;
private int dataCount = 0;// 记录累加的值
private boolean isCancel; // 用来判断是否点击了取消
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvShow = (TextView) findViewById(R.id.tv_show);
tvShow.setOnClickListener(this);
}
public void showDialog() {
mDialog = new CommonProgressDialog(this);
mDialog.setMessage("正在下载" + "(100MB)");
mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mDialog.setMax(100 * 1024 * 1024);
mDialog.setIndeterminate(true);
mDialog.setOnOkAndCancelListener(this);
mDialog.show();
new Thread() {
@Override
public void run() {
try {
// 如果没有点击取消,继续更新进度
while (dataCount <= 100) {
if (!isCancel) {
// 由线程来控制进度(注意:需要子线程更新)
mDialog.setProgress((dataCount++) * 1024 * 1024);
Thread.sleep(100);
Log.i("test", dataCount + "---showDialog");
} else {
//如果是点击了取消,跳出当前循环
break;
}
}
if (!isCancel) {
// 如果没有点击取消,正常走完,重新赋值为0
dataCount = 0;
}
// 当停止以后,更新标记
isCancel = false;
mDialog.cancel();
} catch (Exception e) {
mDialog.cancel();
}
}
}.start();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_show:
showDialog();
break;
default:
break;
}
}
@Override
public void onCancel(View v) {
mDialog.dismiss();
isCancel = true;
}
}
五:到这里,相信你应该会用了!请大家不要喷~谢谢!以后有空会分享更多内容