溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

android怎么實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條

發(fā)布時(shí)間:2021-08-19 17:39:25 來源:億速云 閱讀:115 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“android怎么實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“android怎么實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條”吧!

調(diào)用

ProgressUtil.startProgress(this, new ProgressUtil.ICallback() {
     @Override
     public void progress(int count) {
                    LogUtil.d(count + "%");
                }
            });

ProgressUtil

package com.coral3.common_module.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.coral3.common_module.R;
import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;

public class ProgressUtil {

    private static View progressContainer;
    private static TextView tvView;
    private static ProgressBar progressView;
    private static ViewGroup contentView;
    private static Timer timer = new Timer();
    private static TimerTask task;
    private static int count = 0;
    private static ICallback myICallback;
    private static Handler handler = new Handler(new Handler.Callback(){

        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what == 0x1){
                count++;
                progressView.setProgress(count);
                tvView.setText(count + "%");
                myICallback.progress(count);
            }
            return false;
        }
    });

    public static void startProgress(Context context, ICallback iCallback){
        if(null == contentView) contentView = ((Activity)context).findViewById(android.R.id.content);
        if (progressContainer == null) {
            progressContainer = LayoutInflater.from(context).inflate(R.layout.view_progress, null, false);
            progressView = progressContainer.findViewById(R.id.pb_common);
            tvView = progressContainer.findViewById(R.id.tv_progress);
            contentView.addView(progressContainer);
        } else {
            progressContainer.setVisibility(View.VISIBLE);
        }
        myICallback = iCallback;
        task = new TimerTask() {
            @Override
            public void run() {

                if(count > 99){
                    hideProgressInUiThread((Activity) context);
                }else{
                    handler.sendEmptyMessage(0x1);
                }
            }
        };
        if(timer == null) timer = new Timer();
        timer.schedule(task, 10, 1000/60);
    }

    public static void endTimer(){
        timer.cancel();
        task.cancel();
        task = null;
        timer = null;
        count = 0;
    }

    public static void hideProgress(){
        if (progressContainer != null) {
            endTimer();
            progressContainer.setVisibility(View.GONE);
        }
    }

    public static void startProgressInUiThread(Context context, ICallback iCallback){
        ((Activity)context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                startProgress(context, iCallback);
            }
        });
    }

    public static void hideProgressInUiThread(Activity activity){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                hideProgress();
            }
        });
    }

    public interface ICallback{
        void progress(int count);
    }
}

view_progress.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:gravity="center"
            android:padding="8dp"
            android:layout_height="match_parent">
            <ProgressBar android:id="@+id/pb_common"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:progress="10"
                ></ProgressBar>
            <TextView
                android:id="@+id/tv_progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0%"/>
        </LinearLayout>

</RelativeLayout>

感謝各位的閱讀,以上就是“android怎么實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)android怎么實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI