android实现动态显示隐藏进度条
作者:蓝之静云 时间:2023-04-12 23:07:10
本文实例为大家分享了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>
来源:https://blog.csdn.net/weixin_44659286/article/details/119121215
标签:android,进度条
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springBoot之如何获取接口请求数据和返回数据实现日志
2023-11-23 10:43:58
java中对象调用成员变量与成员实例方法
2023-08-04 11:42:49
![](https://img.aspxhome.com/file/2023/4/71944_0s.png)
C#中FormClosing与FormClosed的区别详细解析
2023-01-26 16:28:13
Java开发之Lombok指南
2022-11-19 21:49:28
![](https://img.aspxhome.com/file/2023/2/64202_0s.jpg)
java 对象参数去空格方式代码实例
2023-11-27 09:49:34
android通过usb读取U盘的方法
2023-03-14 07:43:25
Java构造代码块,静态代码块原理与用法实例分析
2023-11-03 09:03:45
![](https://img.aspxhome.com/file/2023/7/58707_0s.png)
Java中对AtomicInteger和int值在多线程下递增操作的测试
2023-10-22 18:32:03
C#使用is、as关键字以及显式强转实现引用类型转换
2023-01-29 16:15:27
Spring入门实战之Profile详解
2021-10-03 19:08:34
![](https://img.aspxhome.com/file/2023/4/116184_0s.png)
基于springboot2集成jpa,创建dao的案例
2021-08-02 00:40:46
C# 正则表达式进阶
2022-12-26 01:56:32
C#实现学生档案查询
2022-10-10 05:22:07
![](https://img.aspxhome.com/file/2023/3/94543_0s.jpg)
Android 如何使用短信链接打开APP
2022-01-09 02:45:25
![](https://img.aspxhome.com/file/2023/1/109221_0s.gif)
关于idea更新到2020.2.3无法创建web项目原因 library is not specified
2022-11-24 10:13:28
![](https://img.aspxhome.com/file/2023/6/64006_0s.png)
java Hibernate多对多映射详解及实例代码
2023-07-02 07:24:40
![](https://img.aspxhome.com/file/2023/2/61052_0s.png)
详解Java的文件与目录管理以及输入输出相关操作
2022-05-03 15:07:22
![](https://img.aspxhome.com/file/2023/0/101750_0s.jpg)
C#中程序自删除实现方法
2021-06-01 19:47:29
利用java实现邮箱群发功能
2021-07-11 21:55:23
mybatis trim标签的使用详解
2022-07-10 23:27:54
![](https://img.aspxhome.com/file/2023/3/106113_0s.png)