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+PyQt5实现多屏幕协同播放功能

《Python+PyQt5实现多屏幕协同播放功能》在现代会议展示、数字广告、展览展示等场景中,多屏幕协同播放已成为刚需,下面我们就来看看如何利用Python和PyQt5开发一套功能强大的跨屏播控系统吧... 目录一、项目概述:突破传统播放限制二、核心技术解析2.1 多屏管理机制2.2 播放引擎设计2.3 专

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

使用Python实现文本转语音(TTS)并播放音频

《使用Python实现文本转语音(TTS)并播放音频》在开发涉及语音交互或需要语音提示的应用时,文本转语音(TTS)技术是一个非常实用的工具,下面我们来看看如何使用gTTS和playsound库将文本... 目录什么是 gTTS 和 playsound安装依赖库实现步骤 1. 导入库2. 定义文本和语言 3

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后