android實現(xiàn)動態(tài)顯示隱藏進度條
更新時間:2021年08月19日 16:10:19 作者:藍之靜云
這篇文章主要為大家詳細介紹了android實現(xiàn)動態(tài)顯示隱藏進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了android實現(xiàn)動態(tài)顯示隱藏進度條的具體代碼,供大家參考,具體內(nèi)容如下
調(diào)用
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>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 淺談Android中適配器的notifyDataSetChanged()為何有時不刷新
- Android 中解決Viewpage調(diào)用notifyDataSetChanged()時界面無刷新的問題
- Android 控件GridView使用案例講解
- Android報錯Error:Could not find com.android.tools.build:gradle:4.1解決辦法
- 解決Could not find com.android.tools.build:gradle:3.0.0
- 關于Android多渠道打包的進階知識
- Android notifyDataSetChanged() 動態(tài)更新ListView案例詳解
相關文章
利用Android中的TextView實現(xiàn)逐字顯示動畫
在安卓程序啟動的時候,想逐字顯示一段話,每個字都有一個從透明到不透明的漸變動畫。那如何顯示這個效果,下面一起來看看。2016-08-08Android中TextView實現(xiàn)部分文字可點擊跳轉
這篇文章主要為大家詳細介紹了Android中TextView實現(xiàn)部分文字可點擊跳轉的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android應用中實現(xiàn)跳轉外部瀏覽器打開鏈接功能
在開發(fā)Android應用程序時,有時候我們需要讓用戶跳轉到外部瀏覽器打開特定的鏈接,例如打開一個網(wǎng)頁、下載文件等,本文將介紹如何在Android應用中實現(xiàn)跳轉外部瀏覽器打開鏈接的功能,感興趣的朋友一起看看吧2024-06-06詳解Android中使用Notification實現(xiàn)進度通知欄(示例三)
這篇文章主要介紹了詳解Android中使用Notification實現(xiàn)進度通知欄(示例三),具有一定的參考價值,有興趣的可以了解一下。2016-12-12Android RecyclerView網(wǎng)格布局(支持多種分割線)詳解(2)
這篇文章主要為大家詳細介紹了Android RecyclerView網(wǎng)格布局,支持多種分割線,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02