博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android 自定义progressbar 样式
阅读量:6259 次
发布时间:2019-06-22

本文共 6478 字,大约阅读时间需要 21 分钟。

在res下创建drawable文件夹,新建文件drawable/progressbar_color.xml

progressBar style

style="?android:attr/progressBarStyleHorizontal"

 

 

dialog自定义样式XML

 

 

CommonProgressDialog.java类:

package buzz.things.prigressdialog;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.widget.ProgressBar;import android.widget.TextView;import java.text.NumberFormat;/** * Created by buzz on 2015/6/12. */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.progress_bar_sweet);        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);  }

 

 

效果图

ref:

ref:

 

转载于:https://www.cnblogs.com/stuart/p/4572340.html

你可能感兴趣的文章
不仅是NRA球赛,手术也在VR直播了!
查看>>
android 空调遥控器——简单发送内容
查看>>
数字比较
查看>>
MS CRM 2011 Form与Web Resource在JScript中的相互调用
查看>>
Oracle下定时删除归档日志脚本
查看>>
thinkphp-删除delete函数
查看>>
SQL Server dbcc inputbuffer
查看>>
eclipse导入svn项目,项目却没有svn的标记
查看>>
1、Cacti配置安装、监控Cisco交换机
查看>>
Windows Server 2012版本区别
查看>>
Linux系统安全加固基础
查看>>
vnx vmax分盘过程
查看>>
php断点续传之分割合并文件
查看>>
Lesson 5-Exchange server 2010 Transfer mails in public network
查看>>
Chrome源码剖析【三】
查看>>
windows系统自带命令查看硬件信息,怎样dos命令查看硬盘和内存/CPU信息
查看>>
Nginx基础应用--------基于CentOS6源码安装
查看>>
流媒体服务器之nginx的rtmp模块
查看>>
AChartEngine中属性XYMultipleSeriesRenderer和XYSeriesRender属性详解
查看>>
免费的上网行为管理系统和软路由系统推荐。
查看>>