ProgressDialog进度条更改文件单位

ProgressDialog进度条更改文件单位

效果图

ProgressDialog进度条更改文件单位_第1张图片

重点部分

重点方法:setProgressNumberFormat

DownLoadRunnable:模拟下载线程

package com.demo.progress;

/**
 * Created by talon on 2020/5/20
 * note: 模拟下载线程
 */
public class DownLoadRunnable implements Runnable {

    // 模拟数据大小10M
    private long fileLength = 10 * 1024 * 1024;
    private UpdateCallBack updateCallBack;

    void setUpdateCallBack(UpdateCallBack callBack) {
        updateCallBack = callBack;
    }

    @Override
    public 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;

    @Override
    protected 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();
    }

    @Override
    public void updateProcess(final long all, final long cur) {

        runOnUiThread(new Runnable() {
            @Override
            public 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

你可能感兴趣的:(Android基础)