这篇文章主要讲解了“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" ></ProgressBar> <TextView android:id="@+id/tv_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0%"/> </LinearLayout> </RelativeLayout>
感谢各位的阅读,以上就是“android怎么实现动态显示隐藏进度条”的内容了,经过本文的学习后,相信大家对android怎么实现动态显示隐藏进度条这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。