Android实现步进式录像进度条

2023-12-05 06:58

本文主要是介绍Android实现步进式录像进度条,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

现在的APP对用户的体验要求越来越高,操作简单、样式新颖的交互能够提高用户的黏性。今天来实现一下步进式录像进度条,秒拍中用到这样的进度条,如下图:


下面来简单实现一下:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><RelativeLayoutandroid:id="@+id/progress_layout"android:layout_width="match_parent"android:layout_height="wrap_content" ><com.jackie.steppingprogressbar.SteppingProgressBarandroid:id="@+id/stepping_progressbar"android:layout_width="match_parent"android:layout_height="50dp" /></RelativeLayout><LinearLayoutandroid:id="@+id/button_layout"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/progress_layout"android:layout_marginTop="30dp" ><Buttonandroid:id="@+id/btn_start"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="15dp"android:onClick="onStartClick"android:text="开始" /><Buttonandroid:id="@+id/btn_stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:onClick="onStopClick"android:text="停止" /><Buttonandroid:id="@+id/btn_reset"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:onClick="onResetClick"android:text="重置" /><Buttonandroid:id="@+id/btn_delete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:onClick="onDeleteClick"android:text="删除" /></LinearLayout></RelativeLayout>
SteppingProgressBar.java

package com.jackie.steppingprogressbar;import java.util.ArrayList;
import java.util.List;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;public class SteppingProgressBar extends View {private Paint mPaint;private int mMaxWidth;private float mPercent = 0;private boolean mIsProgressPause;private boolean mAddTimeStamp;private List<Rect> mTimeStampPosition = new ArrayList<Rect>();private List<Float> mTimeStampPercent = new ArrayList<Float>();private final int STATE_NORMAL = 1;private final int STATE_DELETE_PREPARE = 2;private final int STATE_DELETE_DONE = 3;private int mState = STATE_NORMAL;private int MAX_PERCENT = 100;private final static int PROGRESS_TEXT_SIZE = 56;private final static int PROGRESS_SCALE = 1000;private final static int PROGRESS_BACKGROUND_COLOR = Color.parseColor("#66030e18");private final static int PROGRESS_PASSED_COLOR = Color.parseColor("#7cb855");private final static int PROGRESS_STAMP_COLOR = Color.parseColor("#4a7b17");private final static int PROGRESS_TEXT_COLOR = Color.parseColor("#ff0000");private final static int PROGRESS_DELETING_COLOR = Color.parseColor("#3c6e57");private SteppingProgressBarCallbackListener mSteppingProgressBarCallbackListener = null;public void setOnDeleteCallbackListener(SteppingProgressBarCallbackListener listener) {this.mSteppingProgressBarCallbackListener = listener;}public interface SteppingProgressBarCallbackListener {/* 删除完成的回调 */public void deleteDone(float percent);}public SteppingProgressBar(Context context, AttributeSet attrs) {super(context, attrs);initData();}private void initData() {mPaint = new Paint();mPaint.setAlpha(255);mPaint.setStyle(Style.FILL);mPaint.setDither(true);  //防抖动mPaint.setAntiAlias(true); //放锯齿}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//获取尺寸int size = MeasureSpec.getSize(heightMeasureSpec);size = (int)(size + PROGRESS_TEXT_SIZE + 10);heightMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);super.onMeasure(widthMeasureSpec, heightMeasureSpec);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//绘制底色mPaint.setColor(PROGRESS_BACKGROUND_COLOR);float width = mMaxWidth = getWidth();float height = getHeight() - PROGRESS_TEXT_SIZE - 10;canvas.drawRect(0, 0, width, height, mPaint);//绘制进度色mPaint.setColor(PROGRESS_PASSED_COLOR);width = (float) mPercent / (float) MAX_PERCENT * width;canvas.drawRect(0, 0, width, height, mPaint);//在下次进度条重新开始时,计算上次的分隔条if (mAddTimeStamp && mState == STATE_NORMAL && !mIsProgressPause) {mAddTimeStamp = false;Rect mRect = new Rect();mRect.left = (int) (width - 4); mRect.top = 0;mRect.right = (int) width;mRect.bottom = (int) height;mTimeStampPosition.add(mRect);mTimeStampPercent.add(mPercent);}//绘制所有分隔条for (Rect mRect : mTimeStampPosition) {mPaint.setColor(PROGRESS_STAMP_COLOR);canvas.drawRect(mRect, mPaint);}//绘制进度值文本mPaint.setStrokeWidth(0);mPaint.setColor(PROGRESS_TEXT_COLOR);mPaint.setTextSize(PROGRESS_TEXT_SIZE);mPaint.setTypeface(Typeface.DEFAULT);float textWidth = mPaint.measureText(mPercent / PROGRESS_SCALE + "s");if (mPercent != 0) {if (width + textWidth > mMaxWidth) {canvas.drawText(mPercent / PROGRESS_SCALE + "s", (width - textWidth), height + PROGRESS_TEXT_SIZE, mPaint);} else {canvas.drawText(mPercent / PROGRESS_SCALE + "s", width, height + PROGRESS_TEXT_SIZE, mPaint);}}if (mState == STATE_DELETE_DONE) {mState = STATE_NORMAL;//当删除完成时,准备绘制本次分隔条,当percent为0的时候不绘制if (mPercent != 0) {this.mAddTimeStamp = true;}} else if (mState == STATE_DELETE_PREPARE) {//当准备删除时,将上段时间内的进度条变色mPaint.setColor(PROGRESS_DELETING_COLOR);int left;if (mTimeStampPosition != null && mTimeStampPosition.size() > 0) {left = mTimeStampPosition.get(mTimeStampPosition.size() -1).right;} else {left = 0;}canvas.drawRect(left, 0, width, height, mPaint);}}/*** 进度条的最大值* @param maxPercent*/public void setMax(int maxPercent) {this.MAX_PERCENT = maxPercent;}/*** 当前进度* @return*/public float getProgress() {return mPercent;}/*** 设置当前进度* @param percent*/public void setProgress(float percent) {if (percent < 0) {return;}if (percent >= MAX_PERCENT) {percent = MAX_PERCENT;}this.mPercent = percent;this.mState = STATE_NORMAL;this.mIsProgressPause = false;invalidate();}/*** 设置视频录制中间的断点时间戳* @param timeStamp*/public void setTimeStamp(boolean timeStamp) {this.mIsProgressPause = true;this.mAddTimeStamp = timeStamp;}/*** 准备删除前一段进度*/public void deleteLastStepPrepare() {if (mState == STATE_DELETE_PREPARE) {return;}mState = STATE_DELETE_PREPARE;invalidate();}/*** 确认删除前一段进度*/public void deleteLastStep() {if (mState != STATE_DELETE_PREPARE) {deleteLastStepPrepare();return;}if (mTimeStampPercent != null && mTimeStampPercent.size() > 0) {this.mPercent = mTimeStampPercent.remove(mTimeStampPercent.size() - 1);} else {this.mPercent = 0;}this.mAddTimeStamp = false;mState = STATE_DELETE_DONE;//操作断点if (mTimeStampPosition != null && mTimeStampPosition.size() > 0) {mTimeStampPosition.remove(mTimeStampPosition.size() - 1);}if (mSteppingProgressBarCallbackListener != null) {mSteppingProgressBarCallbackListener.deleteDone(this.mPercent);}invalidate();}/*** 取消删除前一段进度*/public void deleteLastStepCancel() {if (mState == STATE_NORMAL) {return;}mState = STATE_DELETE_DONE;invalidate();}/*** 重置进度条*/public void reset() {this.mPercent = 0;this.mAddTimeStamp = false;mTimeStampPosition.clear();mTimeStampPercent.clear();invalidate();}
}
MainActivity.java

package com.jackie.steppingprogressbar;import com.jackie.steppingprogressbar.SteppingProgressBar.SteppingProgressBarCallbackListener;import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;public class MainActivity extends Activity {SteppingProgressBar mProgressBar;private boolean mIsStopped = true;private int mProgress;private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);setProgress();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mProgressBar = (SteppingProgressBar) findViewById(R.id.stepping_progressbar);mProgressBar.setMax(10000);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.// getMenuInflater().inflate(R.menu.main, menu);return true;}public void onStartClick(View view) {if (!mIsStopped) {return;}mIsStopped = false;mHandler.removeMessages(0);mHandler.sendEmptyMessage(0);}private void setProgress() {if (mProgress > 10000) {mHandler.removeMessages(0);} else if (!mIsStopped) {mProgress += 100;mProgressBar.setProgress(mProgress);mHandler.sendEmptyMessageDelayed(0, 100);}}public void onStopClick(View view) {mIsStopped = true;mProgressBar.setTimeStamp(true);}public void onResetClick(View view) {mProgressBar.reset();mProgress = 0;}public void onDeleteClick(View view) {if (!mIsStopped) {return;}mProgressBar.deleteLastStep();mProgressBar.setOnDeleteCallbackListener(new SteppingProgressBarCallbackListener() {@Overridepublic void deleteDone(float percent) {mProgress = (int) percent;}});}
}
效果图如下:

   


这篇关于Android实现步进式录像进度条的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/456602

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q