Android视频播放暂停动效的按钮

2024-06-15 11:12

本文主要是介绍Android视频播放暂停动效的按钮,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

上来直接给大家搂代码

 

class PlayButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :View(context, attrs, defStyleAttr) {companion object {/** 播放状态 */const val STATE_PLAY: Int = 0/** 暂停状态 */const val STATE_PAUSE: Int = 1}/** 当前状态 */private var currentState: Int = STATE_PAUSE/** 动画时间 */private var animDuration: Intprivate val paint: Paintprivate var viewWidth: Int = 0private var viewHeight: Int = 0private var centerX: Int = 0private var centerY: Int = 0private var circleRadius: Int = 0private var rectF: RectF? = nullprivate var bgRectF: RectF? = nullprivate var fraction: Float = 1fprivate val path: Pathprivate val dstPath: Pathprivate val pathMeasure: PathMeasureprivate var pathLength: Float = 0finit {val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.PlayButton)val lineColor: Int = typedArray.getColor(R.styleable.PlayButton_pb_lineColor, Color.WHITE)val lineSize: Int = typedArray.getInteger(R.styleable.PlayButton_pb_lineSize,resources.getDimension(R.dimen.dp_4).toInt())animDuration = typedArray.getInteger(R.styleable.PlayButton_pb_animDuration, 200)typedArray.recycle()// 关闭硬件加速setLayerType(LAYER_TYPE_SOFTWARE, null)paint = Paint(Paint.ANTI_ALIAS_FLAG)paint.style = Paint.Style.STROKEpaint.strokeCap = Paint.Cap.ROUNDpaint.color = lineColorpaint.strokeWidth = lineSize.toFloat()paint.pathEffect = CornerPathEffect(1f)path = Path()dstPath = Path()pathMeasure = PathMeasure()}override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) {super.onSizeChanged(width, height, oldWidth, oldHeight)viewWidth = width * 9 / 10viewHeight = height * 9 / 10circleRadius = width / resources.getDimension(R.dimen.dp_4).toInt()centerX = width / 2centerY = height / 2rectF = RectF((centerX - circleRadius).toFloat(), centerY + 0.6f * circleRadius,(centerX + circleRadius).toFloat(), centerY + 2.6f * circleRadius)bgRectF = RectF(centerX - viewWidth / 2f, centerY - viewHeight / 2f,centerX + viewWidth / 2f, centerY + viewHeight / 2f)path.moveTo((centerX - circleRadius).toFloat(), centerY + 1.8f * circleRadius)path.lineTo((centerX - circleRadius).toFloat(), centerY - 1.8f * circleRadius)path.lineTo((centerX + circleRadius).toFloat(), centerY.toFloat())path.close()pathMeasure.setPath(path, false)pathLength = pathMeasure.length}override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {var finalWidthMeasureSpec: Int = widthMeasureSpecvar finalHeightMeasureSpec: Int = heightMeasureSpecwhen (MeasureSpec.getMode(finalWidthMeasureSpec)) {MeasureSpec.AT_MOST, MeasureSpec.UNSPECIFIED ->finalWidthMeasureSpec = MeasureSpec.makeMeasureSpec(resources.getDimension(R.dimen.dp_60).toInt(), MeasureSpec.EXACTLY)MeasureSpec.EXACTLY -> {}}when (MeasureSpec.getMode(finalHeightMeasureSpec)) {MeasureSpec.AT_MOST, MeasureSpec.UNSPECIFIED ->finalHeightMeasureSpec = MeasureSpec.makeMeasureSpec(resources.getDimension(R.dimen.dp_60).toInt(), MeasureSpec.EXACTLY)MeasureSpec.EXACTLY -> {}}setMeasuredDimension(finalWidthMeasureSpec, finalHeightMeasureSpec)}override fun onDraw(canvas: Canvas) {super.onDraw(canvas)canvas.drawCircle(centerX.toFloat(), centerY.toFloat(), viewWidth / 2f, paint)when {fraction < 0 -> {// 弹性部分canvas.drawLine((centerX + circleRadius).toFloat(),centerY - 1.6f * circleRadius + 10 * circleRadius * fraction, (centerX + circleRadius).toFloat(),centerY + (1.6f * circleRadius) + (10 * circleRadius * fraction), paint)canvas.drawLine((centerX - circleRadius).toFloat(), centerY - 1.6f * circleRadius, (centerX - circleRadius).toFloat(), centerY + 1.6f * circleRadius, paint)canvas.drawArc(bgRectF!!, -105f, 360f, false, paint)}fraction <= 0.3 -> {// 右侧直线和下方曲线canvas.drawLine((centerX + circleRadius).toFloat(),centerY - 1.6f * circleRadius + circleRadius * 3.2f / 0.3f * fraction, (centerX + circleRadius).toFloat(),centerY + 1.6f * circleRadius, paint)canvas.drawLine((centerX - circleRadius).toFloat(), centerY - 1.6f * circleRadius, (centerX - circleRadius).toFloat(), centerY + 1.6f * circleRadius, paint)if (fraction != 0f) {canvas.drawArc(rectF!!, 0f, 180f / 0.3f * fraction, false, paint)}canvas.drawArc(bgRectF!!, -105 + 360 * fraction, 360 * (1 - fraction), false, paint)}fraction <= 0.6 -> {// 下方曲线和三角形canvas.drawArc(rectF!!, 180f / 0.3f * (fraction - 0.3f),180 - 180f / 0.3f * (fraction - 0.3f), false, paint)dstPath.reset()pathMeasure.getSegment(0.02f * pathLength,0.38f * pathLength + 0.42f * pathLength / 0.3f * (fraction - 0.3f),dstPath, true)canvas.drawPath(dstPath, paint)canvas.drawArc(bgRectF!!, -105 + 360 * fraction, 360 * (1 - fraction), false, paint)}fraction <= 0.8 -> {// 三角形dstPath.reset()pathMeasure.getSegment(0.02f * pathLength + 0.2f * pathLength / 0.2f * (fraction - 0.6f),0.8f * pathLength + 0.2f * pathLength / 0.2f * (fraction - 0.6f), dstPath, true)canvas.drawPath(dstPath, paint)canvas.drawArc(bgRectF!!, -105 + 360 * fraction, 360 * (1 - fraction), false, paint)}else -> {// 弹性部分dstPath.reset()pathMeasure.getSegment(10 * circleRadius * (fraction - 1), pathLength, dstPath, true)canvas.drawPath(dstPath, paint)}}}/*** 播放状态*/fun play() {if (currentState == STATE_PLAY) {return}currentState = STATE_PLAYval valueAnimator: ValueAnimator = ValueAnimator.ofFloat(1f, 100f)valueAnimator.duration = animDuration.toLong()valueAnimator.interpolator = AnticipateInterpolator()valueAnimator.addUpdateListener { animation: ValueAnimator ->fraction = 1 - animation.animatedFractioninvalidate()}valueAnimator.start()}/*** 暂停状态*/fun pause() {if (currentState == STATE_PAUSE) {return}currentState = STATE_PAUSEval valueAnimator: ValueAnimator = ValueAnimator.ofFloat(1f, 100f)valueAnimator.duration = animDuration.toLong()valueAnimator.interpolator = AnticipateInterpolator()valueAnimator.addUpdateListener { animation: ValueAnimator ->fraction = animation.animatedFractioninvalidate()}valueAnimator.start()}/*** 获取当前状态*/fun getCurrentState(): Int {return currentState}/*** 设置动画时间*/fun setAnimDuration(duration: Int) {animDuration = duration}/*** 设置线条颜色*/fun setLineColor(color: Int) {paint.color = colorinvalidate()}/*** 设置线条大小*/fun setLineSize(size: Int) {paint.strokeWidth = size.toFloat()invalidate()}
}

xml给大家也展示下,图片的话暂不上传了,自己替换

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/black"android:orientation="vertical"><VideoViewandroid:id="@+id/vv_player_view_video"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="center"android:keepScreenOn="true" /><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"android:orientation="vertical"><com.hjq.shape.layout.ShapeFrameLayoutandroid:id="@+id/ll_player_view_top"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="top"android:orientation="horizontal"android:visibility="invisible"app:shape="rectangle"app:shape_angle="270"app:shape_centerColor="@color/black20"app:shape_endColor="@color/transparent"app:shape_startColor="#AA000000"tools:visibility="visible"><androidx.appcompat.widget.AppCompatImageViewandroid:id="@+id/iv_player_view_left"android:layout_width="@dimen/dp_50"android:layout_height="@dimen/dp_50"android:layout_gravity="center_vertical"android:padding="@dimen/dp_10"android:visibility="invisible"app:srcCompat="@drawable/arrows_left_ic"app:tint="@color/white"tools:visibility="visible" /><androidx.appcompat.widget.AppCompatTextViewandroid:id="@+id/tv_player_view_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginHorizontal="@dimen/dp_50"android:ellipsize="marquee"android:gravity="center_horizontal"android:singleLine="true"android:textColor="@color/white"android:textSize="@dimen/sp_17"tools:text="我是视频标题" /></com.hjq.shape.layout.ShapeFrameLayout><com.hjq.widget.view.PlayButtonandroid:id="@+id/iv_player_view_control"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:visibility="invisible"tools:visibility="visible" /><com.hjq.shape.view.ShapeImageViewandroid:id="@+id/iv_player_view_lock"android:layout_width="@dimen/dp_45"android:layout_height="@dimen/dp_45"android:layout_gravity="start|center_vertical"android:layout_marginStart="@dimen/dp_5"android:padding="@dimen/dp_10"android:visibility="invisible"app:shape="oval"app:shape_solidColor="@color/black30"app:shape_solidPressedColor="@color/black60"app:srcCompat="@drawable/video_lock_open_ic"tools:visibility="visible" /><com.hjq.shape.layout.ShapeLinearLayoutandroid:id="@+id/ll_player_view_bottom"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="bottom"android:gravity="center_vertical"android:orientation="horizontal"android:paddingHorizontal="@dimen/dp_15"android:visibility="invisible"app:shape="rectangle"app:shape_angle="90"app:shape_centerColor="@color/black20"app:shape_endColor="@color/transparent"app:shape_startColor="#AA000000"tools:visibility="visible"><androidx.appcompat.widget.AppCompatTextViewandroid:id="@+id/tv_player_view_play_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/white"android:textSize="@dimen/sp_14"tools:text="00:00" /><androidx.appcompat.widget.AppCompatSeekBarandroid:id="@+id/sb_player_view_progress"android:layout_width="0px"android:layout_height="wrap_content"android:layout_weight="1"android:foreground="@null"android:maxHeight="@dimen/dp_2"android:paddingVertical="@dimen/dp_10"android:progressDrawable="@drawable/video_progress_bg"android:thumb="@drawable/video_progress_ball_bg"tools:max="100"tools:progress="50"tools:secondaryProgress="80"tools:targetApi="m" /><androidx.appcompat.widget.AppCompatTextViewandroid:id="@+id/tv_player_view_total_time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/white"android:textSize="@dimen/sp_14"tools:text="10:00" /></com.hjq.shape.layout.ShapeLinearLayout></FrameLayout><androidx.cardview.widget.CardViewandroid:id="@+id/cv_player_view_message"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:gravity="center"android:orientation="vertical"android:visibility="gone"app:cardBackgroundColor="@color/black85"app:cardCornerRadius="@dimen/dp_15"app:cardElevation="0px"tools:visibility="visible"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:minWidth="@dimen/dp_110"android:minHeight="@dimen/dp_110"android:orientation="vertical"android:padding="@dimen/dp_10"><com.airbnb.lottie.LottieAnimationViewandroid:id="@+id/lav_player_view_lottie"android:layout_width="@dimen/dp_70"android:layout_height="@dimen/dp_70"app:lottie_autoPlay="false"app:lottie_loop="true"app:lottie_rawRes="@raw/progress" /><com.hjq.widget.view.SmartTextViewandroid:id="@+id/tv_player_view_message"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginHorizontal="@dimen/dp_15"android:layout_marginTop="@dimen/dp_5"android:maxLines="3"android:textColor="@color/white"android:textSize="@dimen/sp_14"tools:text="@string/common_loading" /></LinearLayout></androidx.cardview.widget.CardView></FrameLayout>

这篇关于Android视频播放暂停动效的按钮的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Python视频处理库VidGear使用小结

《Python视频处理库VidGear使用小结》VidGear是一个高性能的Python视频处理库,本文主要介绍了Python视频处理库VidGear使用小结,文中通过示例代码介绍的非常详细,对大家的... 目录一、VidGear的安装二、VidGear的主要功能三、VidGear的使用示例四、VidGea

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

流媒体平台/视频监控/安防视频汇聚EasyCVR播放暂停后视频画面黑屏是什么原因?

视频智能分析/视频监控/安防监控综合管理系统EasyCVR视频汇聚融合平台,是TSINGSEE青犀视频垂直深耕音视频流媒体技术、AI智能技术领域的杰出成果。该平台以其强大的视频处理、汇聚与融合能力,在构建全栈视频监控系统中展现出了独特的优势。视频监控管理系统EasyCVR平台内置了强大的视频解码、转码、压缩等技术,能够处理多种视频流格式,并以多种格式(RTMP、RTSP、HTTP-FLV、WebS

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk