android實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條
本文實(shí)例為大家分享了android實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏進(jìn)度條的具體代碼,供大家參考,具體內(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 淺談Android中適配器的notifyDataSetChanged()為何有時(shí)不刷新
- Android 中解決Viewpage調(diào)用notifyDataSetChanged()時(shí)界面無(wú)刷新的問(wèn)題
- Android 控件GridView使用案例講解
- Android報(bào)錯(cuò)Error:Could not find com.android.tools.build:gradle:4.1解決辦法
- 解決Could not find com.android.tools.build:gradle:3.0.0
- 關(guān)于Android多渠道打包的進(jìn)階知識(shí)
- Android notifyDataSetChanged() 動(dòng)態(tài)更新ListView案例詳解
相關(guān)文章
Android AutoCompleteTextView控件使用實(shí)例
AutoCompleteTextView這個(gè)控件用于輸入框的自動(dòng)完成提示,非常適合搜索框等。它本質(zhì)上是個(gè)EditText,實(shí)際上它也是從EditText繼承的,使用起來(lái)也十分簡(jiǎn)單2014-04-04
android 6.0 權(quán)限授權(quán)方法
今天小編就為大家分享一篇android 6.0 權(quán)限授權(quán)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android自定義PopupWindow仿點(diǎn)擊彈出分享功能
這篇文章主要為大家詳細(xì)介紹了Android自定義PopupWindow仿點(diǎn)擊彈出分享功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
詳解Android開發(fā)錄音和播放音頻的步驟(動(dòng)態(tài)獲取權(quán)限)
這篇文章主要介紹了詳解Android開發(fā)錄音和播放音頻的步驟(動(dòng)態(tài)獲取權(quán)限),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android實(shí)現(xiàn)保存QQ賬號(hào)與密碼功能(文件存儲(chǔ))
這篇文章主要介紹了Android保存QQ賬號(hào)與密碼,文件存儲(chǔ)是Android中最基本的一種數(shù)據(jù)存儲(chǔ)方式,它與Java中的文件存儲(chǔ)類似,都是通過(guò)I/O流形式把數(shù)據(jù)直接存儲(chǔ)到文件中,下面我們一起來(lái)看一下如何用Android實(shí)現(xiàn)文件存儲(chǔ)功能吧2022-04-04
Android 線程優(yōu)化知識(shí)點(diǎn)學(xué)習(xí)
這篇文章主要為大家介紹了Android線程優(yōu)化知識(shí)點(diǎn)學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Android Path繪制貝塞爾曲線實(shí)現(xiàn)QQ拖拽泡泡
本文主要介紹Android Path繪制貝塞爾曲線,這里整理相關(guān)資料并運(yùn)用貝塞爾曲線實(shí)現(xiàn)QQ拖拽泡泡的示例,有興趣的小伙伴可以參考下2016-09-09

