短视频app搭建,android动态显隐进度条

短视频app搭建,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"
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>
复制代码

 

以上就是短视频app搭建,android动态显隐进度条实现的相关代码, 更多内容欢迎关注之后的文章

posted @   云豹科技-苏凌霄  阅读(75)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示