android實(shí)現(xiàn)動態(tài)顯隱進(jìn)度條
本文實(shí)例為大家分享了android實(shí)現(xiàn)動態(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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 七種進(jìn)度條的樣式
- Android中實(shí)現(xiàn)Webview頂部帶進(jìn)度條的方法
- android自定義進(jìn)度條漸變色View的實(shí)例代碼
- Android文件下載進(jìn)度條的實(shí)現(xiàn)代碼
- android ListView和ProgressBar(進(jìn)度條控件)的使用方法
- Android編程之ProgressBar圓形進(jìn)度條顏色設(shè)置方法
- Android中自定義進(jìn)度條詳解
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對話框的實(shí)現(xiàn)
- android中實(shí)現(xiàn)OkHttp下載文件并帶進(jìn)度條
- Android 動態(tài)改變SeekBar進(jìn)度條顏色與滑塊顏色的實(shí)例代碼
相關(guān)文章
Android項目實(shí)戰(zhàn)手把手教你畫圓形水波紋loadingview
這篇文章主要為大家詳細(xì)介紹了Android項目實(shí)戰(zhàn)手把手教你畫圓形水波紋loadingview,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
Android簡單實(shí)現(xiàn)一個顏色漸變的ProgressBar的方法
本篇文章主要介紹了Android簡單實(shí)現(xiàn)一個顏色漸變的ProgressBar的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android selector狀態(tài)選擇器的使用詳解
這篇文章主要為大家詳細(xì)介紹了Android selector狀態(tài)選擇器的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-09-09
Android實(shí)現(xiàn)漂亮的Gallery畫廊
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)漂亮的Gallery畫廊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
Android中的廣播(BroadCast)詳細(xì)介紹
這篇文章主要介紹了Android中的廣播(BroadCast)詳細(xì)介紹,本文講解了什么是廣播、廣播有什么用、實(shí)現(xiàn)廣播、動態(tài)注冊方式、配置文件方式等內(nèi)容,需要的朋友可以參考下2015-03-03
在Android打包中區(qū)分測試和正式環(huán)境淺析
這篇文章主要給大家介紹了關(guān)于在Android打包中如何區(qū)分測試和正式環(huán)境的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起看看吧。2017-10-10
PC版與Android手機(jī)版帶斷點(diǎn)續(xù)傳的多線程下載
這篇文章主要介紹了PC版與Android手機(jī)版帶斷點(diǎn)續(xù)傳的多線程下載的相關(guān)資料,需要的朋友可以參考下2015-10-10
Android提高之自定義Menu(TabMenu)實(shí)現(xiàn)方法
這篇文章主要介紹了Android自定義Menu(TabMenu)實(shí)現(xiàn)方法,是非常實(shí)用的功能,需要的朋友可以參考下2014-08-08

