欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-03-12 11:21:08
本文实例为大家分享了android实现动态显示隐藏进度条的具体代码,供大家参考,具体内容如下调用progressutil.startprogress(this, new progressutil.ic...

本文实例为大家分享了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 进度条