本文主要是介绍ProgressDialog进度条更改文件单位,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ProgressDialog进度条更改文件单位
效果图
重点部分
重点方法:setProgressNumberFormat
DownLoadRunnable:模拟下载线程
package com.demo.progress;/*** Created by talon on 2020/5/20* note: 模拟下载线程*/
public class DownLoadRunnable implements Runnable {// 模拟数据大小10Mprivate long fileLength = 10 * 1024 * 1024;private UpdateCallBack updateCallBack;void setUpdateCallBack(UpdateCallBack callBack) {updateCallBack = callBack;}@Overridepublic void run() {long curLength = 0;while (true) {try {Thread.sleep(100);curLength = curLength + 100000;if (updateCallBack != null)updateCallBack.updateProcess(fileLength, curLength);if (curLength >= fileLength)break;} catch (InterruptedException e) {e.printStackTrace();}}}
}
MainActivity: ProgressDialog的具体实现
package com.demo.progress;import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;import java.util.Locale;/*** Created by talon on 2020/5/20* note: progressDialog 显示百分比和文件大小*/
public class MainActivity extends AppCompatActivity implements UpdateCallBack {private ProgressDialog mProgressDialog;private DownLoadRunnable mDownLoadRunnable;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mDownLoadRunnable = new DownLoadRunnable();mDownLoadRunnable.setUpdateCallBack(this);mProgressDialog = new ProgressDialog(this);mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);mProgressDialog.setCancelable(false);mProgressDialog.setTitle("下载中...");}public void Start(View view) {mProgressDialog.show();new Thread(mDownLoadRunnable).start();}@Overridepublic void updateProcess(final long all, final long cur) {runOnUiThread(new Runnable() {@Overridepublic void run() {if (cur >= all) {mProgressDialog.dismiss();return;}float allFloat = (float) all / 1024 / 1024;float curFloat = (float) cur / 1024 / 1024;mProgressDialog.setProgressNumberFormat(String.format(Locale.CHINA, "%.2fM/%.2fM", curFloat, allFloat));mProgressDialog.setMax((int) all);mProgressDialog.setProgress((int) cur);}});}}
UpdateCallBack: 线程和界面之间的桥梁
package com.demo.progress;/*** Created by talon on 2020/5/20* note: 接口通信-桥梁*/
public interface UpdateCallBack {void updateProcess(long all, long cur);}
demo地址
https://download.csdn.net/download/u011368551/12453905
这篇关于ProgressDialog进度条更改文件单位的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!