欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android实现动态显示隐藏进度条

时间:2023-02-18 10:17:06|栏目:Android代码|点击:

本文实例为大家分享了android实现动态显示隐藏进度条的具体代码,供大家参考,具体内容如下

调用

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"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"></ProgressBar>
            <TextView
                android:id="@+id/tv_progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0%"/>
        </LinearLayout>

</RelativeLayout>

上一篇:Android开启新线程播放背景音乐

栏    目:Android代码

下一篇:没有了

本文标题:android实现动态显示隐藏进度条

本文地址:http://www.codeinn.net/misctech/226006.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有