本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
android实现动态显示隐藏进度条
talkchan · 334浏览 · 发布于2021-08-25 +关注

这篇文章主要为大家详细介绍了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下vulkan与opengles纹理互通

talkchan · 1158浏览 · 2020-11-23 10:37:39
Android 使用RecyclerView实现轮播图

奔跑的男人 · 2166浏览 · 2019-05-09 17:11:13
微软发布新命令行工具 Windows Terminal

吴振华 · 860浏览 · 2019-05-09 17:15:04
Facebook 停止屏蔽部分区块链广告

· 746浏览 · 2019-05-09 17:20:08
加载中

0评论

评论
大家好,我是一名专注技术开发的技术屌丝,有什么问题可以互相交流,一起学习进步,谢谢。
分类专栏
小鸟云服务器
扫码进入手机网页