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í)界面無刷新的問題
- 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中的TextView實(shí)現(xiàn)逐字顯示動(dòng)畫
在安卓程序啟動(dòng)的時(shí)候,想逐字顯示一段話,每個(gè)字都有一個(gè)從透明到不透明的漸變動(dòng)畫。那如何顯示這個(gè)效果,下面一起來看看。2016-08-08
Android中TextView實(shí)現(xiàn)部分文字可點(diǎn)擊跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Android中TextView實(shí)現(xiàn)部分文字可點(diǎn)擊跳轉(zhuǎn)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android 3D旋轉(zhuǎn)動(dòng)畫效果實(shí)現(xiàn)分解
如何實(shí)現(xiàn)View的3D旋轉(zhuǎn)效果,實(shí)現(xiàn)的主要原理就是圍繞Y軸旋轉(zhuǎn),同時(shí)在Z軸方面上有一個(gè)深入的縮放,具體實(shí)現(xiàn)代碼如下,感興趣的朋友可以參考下哈2013-06-06
Android應(yīng)用中實(shí)現(xiàn)跳轉(zhuǎn)外部瀏覽器打開鏈接功能
在開發(fā)Android應(yīng)用程序時(shí),有時(shí)候我們需要讓用戶跳轉(zhuǎn)到外部瀏覽器打開特定的鏈接,例如打開一個(gè)網(wǎng)頁(yè)、下載文件等,本文將介紹如何在Android應(yīng)用中實(shí)現(xiàn)跳轉(zhuǎn)外部瀏覽器打開鏈接的功能,感興趣的朋友一起看看吧2024-06-06
詳解Android中使用Notification實(shí)現(xiàn)進(jìn)度通知欄(示例三)
這篇文章主要介紹了詳解Android中使用Notification實(shí)現(xiàn)進(jìn)度通知欄(示例三),具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12
Android RecyclerView網(wǎng)格布局(支持多種分割線)詳解(2)
這篇文章主要為大家詳細(xì)介紹了Android RecyclerView網(wǎng)格布局,支持多種分割線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

