Compose 动画艺术探索之属性动画

news2024/9/23 1:30:34

本篇文章是此专栏的第三篇文章,如果想阅读前两篇文章的话请点击下方链接:

  • Compose 动画艺术探索之瞅下 Compose 的动画
  • Compose 动画艺术探索之可见性动画

Compose的属性动画

属性动画是通过不断地修改值来实现的,而初始值和结束值之间的过渡动画就需要来计算了。在 Compose 中为我们提供了一整套 api 来实现属性动画,具体有哪些呢?让我们一起来看下吧!

在这里插入图片描述

官方为我们提供了上图这十种方法,我们可以根据实际项目中的需求进行挑选使用。

在第一篇文章中也提到了 Compose 的属性动画,但只是简单使用了下,告诉大家 Compose 有这个东西,今天咱们来具体看下!

先来看下 animateColorAsState 的代码吧:

@Composable
fun animateColorAsState(
    targetValue: Color,
    animationSpec: AnimationSpec<Color> = colorDefaultSpring,
    label: String = "ColorAnimation",
    finishedListener: ((Color) -> Unit)? = null
): State<Color> {
    val converter = remember(targetValue.colorSpace) {
        (Color.VectorConverter)(targetValue.colorSpace)
    }
    return animateValueAsState(
        targetValue, converter, animationSpec, label = label, finishedListener = finishedListener
    )
}

可以看到一共接收四个参数,来分别看下代表什么吧:

  • targetValue:顾名思义,目标值,这里对应的就是想要转换成的颜色
  • animationSpec:动画规格,动画随着时间改变值的一种规格吧,上一篇文章中也提到了,但由于上一篇文章主要内容并不是这个,也就没有讲,本篇文章会详细说明
  • label:标签,以区别于其他动画
  • finishedListener:在动画完成时会进行回调

参数并不算多,而且有三个是可选参数,也就只有 targetValue 必须要进行设置。方法体内只通过 Color.colorSpace 强转构建了一个 TwoWayConverter

前面说过,大多数 Compose 动画 API 支持将 FloatColorDp 以及其他基本数据类型作为 开箱即用的动画值,但有时我们需要为其他数据类型(比如自定义类型)添加动画效果。在动画播放期间,任何动画值都表示为 AnimationVector。使用相应的 TwoWayConverter 即可将值转换为 AnimationVector,反之亦然,这样一来,核心动画系统就可以统一对其进行处理了。由于颜色有 argb,所以构建的时候使用的是 AnimationVector4D ,来看下吧:

val Color.Companion.VectorConverter:
    (colorSpace: ColorSpace) -> TwoWayConverter<Color, AnimationVector4D>
        get() = ColorToVector

如果按照我之前的习惯肯定要接着看 animateValueAsState 方法内部的代码了,但今天等会再看!再来看看 animateDpAsState 的代码吧!

@Composable
fun animateDpAsState(
    targetValue: Dp,
    animationSpec: AnimationSpec<Dp> = dpDefaultSpring,
    label: String = "DpAnimation",
    finishedListener: ((Dp) -> Unit)? = null
): State<Dp> {
    return animateValueAsState(
        targetValue,
        Dp.VectorConverter,
        animationSpec,
        label = label,
        finishedListener = finishedListener
    )
}

发现了点什么没有,参数基本一摸一样,别着急,咱们再看看别的!

@Composable
fun animateIntAsState(
    targetValue: Int,
    animationSpec: AnimationSpec<Int> = intDefaultSpring,
    label: String = "IntAnimation",
    finishedListener: ((Int) -> Unit)? = null
)
​
@Composable
fun animateSizeAsState(
    targetValue: Size,
    animationSpec: AnimationSpec<Size> = sizeDefaultSpring,
    label: String = "SizeAnimation",
    finishedListener: ((Size) -> Unit)? = null
)
​
@Composable
fun animateRectAsState(
    targetValue: Rect,
    animationSpec: AnimationSpec<Rect> = rectDefaultSpring,
    label: String = "RectAnimation",
    finishedListener: ((Rect) -> Unit)? = null
)

不能说是大同小异,只能说是一摸一样!既然一摸一样的话咱们就以文章开头的 animateColorAsState 来看吧!

上面的说法其实是不对的,并不是有十种,而是九种,因为九种都调用了 animateValueAsState ,其实也可以说有无数种,因为可以自定义。。。。

参数

下面先来看下 animateValueAsState 的方法体吧:

@Composable
fun <T, V : AnimationVector> animateValueAsState(
    targetValue: T,
    typeConverter: TwoWayConverter<T, V>,
    animationSpec: AnimationSpec<T> = remember { spring() },
    visibilityThreshold: T? = null,
    label: String = "ValueAnimation",
    finishedListener: ((T) -> Unit)? = null
): State<T>

来看看接收的参数吧,可以发现有两个参数没有见过:

  • typeConverter:类型转换器,将需要的类型转换为 AnimationVector
  • visibilityThreshold:一个可选的阈值,用于定义何时动画值可以被认为足够接近targetValue以结束动画

OK,剩下的参数在上面都介绍过,就不重复进行介绍了。

方法体

由于 animateValueAsState 方法有点长,所以分开来看吧,接下来看下 animateValueAsState 方法中的前半部分:

val animatable = remember { Animatable(targetValue, typeConverter, visibilityThreshold, label) }
val listener by rememberUpdatedState(finishedListener)
val animSpec: AnimationSpec<T> by rememberUpdatedState(
    animationSpec.run {
        if (visibilityThreshold != null && this is SpringSpec &&
            this.visibilityThreshold != visibilityThreshold
        ) {
            spring(dampingRatio, stiffness, visibilityThreshold)
        } else {
            this
        }
    }
)
val channel = remember { Channel<T>(Channel.CONFLATED) }
SideEffect {
    channel.trySend(targetValue)
}
LaunchedEffect(channel) {
    for (target in channel) {
        val newTarget = channel.tryReceive().getOrNull() ?: target
        launch {
            if (newTarget != animatable.targetValue) {
                animatable.animateTo(newTarget, animSpec)
                listener?.invoke(animatable.value)
            }
        }
    }
}

可以看到首先构建了一个 Animatable ,然后记录了完成回调,又记录了 AnimationSpec ,之后有个判断,如果 visibilityThreshold 不为空并且 AnimationSpecSpringSpec 的时候为新构建的一个 AnimationSpec ,反之则还是传进来的 AnimationSpec

Animatable 是个啥呢?它是一个值容器,它可以在通过 animateTo 更改值时为值添加动画效果,它可确保一致的连续性和互斥性,这意味着值变化始终是连续的,并且会取消任何正在播放的动画。Animatable 的许多功能(包括 animateTo)以挂起函数的形式提供,所以需要封装在适当的协程作用域内,所以下面使用了 LaunchedEffect 来包裹执行 animateTo 方法,最后调用了动画完成的回调。

由于 Animatable 类中代码比较多,先来看下类的初始化及构造方法吧!

class Animatable<T, V : AnimationVector>(
    initialValue: T,
    val typeConverter: TwoWayConverter<T, V>,
    private val visibilityThreshold: T? = null,
    val label: String = "Animatable"
) 

可以看到这里使用到的参数在 animateValueAsState 中都有,就不一一介绍了,挑着重点来,来看看上面使用到的 animateTo 吧:

suspend fun animateTo(
    targetValue: T,
    animationSpec: AnimationSpec<T> = defaultSpringSpec,
    initialVelocity: T = velocity,
    block: (Animatable<T, V>.() -> Unit)? = null
): AnimationResult<T, V> {
    val anim = TargetBasedAnimation(
        animationSpec = animationSpec,
        initialValue = value,
        targetValue = targetValue,
        typeConverter = typeConverter,
        initialVelocity = initialVelocity
    )
    return runAnimation(anim, initialVelocity, block)
}

可以看到 animateTo 使用传进来的参数构建了一个 TargetBasedAnimation ,这是一个方便的动画包装类,适用于所有基于目标的动画,即具有预定义结束值的动画。然后返回调用了 runAnimation ,返回值为 AnimationResult ,来看下吧:

class AnimationResult<T, V : AnimationVector>(
    
    val endState: AnimationState<T, V>,
    
    val endReason: AnimationEndReason
) {
    override fun toString(): String = "AnimationResult(endReason=$endReason, endState=$endState)"
}

AnimationResult 在动画结尾包含关于动画的信息,endState 捕获动画在最后一帧的值 evelocityframe time 等。它可以用于启动另一个动画以从先前中断的动画继续速度。endReason 描述动画结束的原因。

下面看下 runAnimation 吧:

private suspend fun runAnimation(
    animation: Animation<T, V>,
    initialVelocity: T,
    block: (Animatable<T, V>.() -> Unit)?
): AnimationResult<T, V> {
​
    val startTime = internalState.lastFrameTimeNanos
    return mutatorMutex.mutate {
        try {
            ......
            endState.animate(
                animation,
                startTime
            ) {
                updateState(internalState)
                ......
            }
            val endReason = if (clampingNeeded) BoundReached else Finished
            endAnimation()
            AnimationResult(endState, endReason)
        } catch (e: CancellationException) {
            // Clean up internal states first, then throw.
            endAnimation()
            throw e
        }
    }
}

这里需要注意:所有不同类型的动画代码路径最终都会汇聚到这个方法中。

好了,基本快见到阳光了!

天亮了

上面方法中有一行:endState.animate ,这个是关键,来看下!

internal suspend fun <T, V : AnimationVector> AnimationState<T, V>.animate(
    animation: Animation<T, V>,
    startTimeNanos: Long = AnimationConstants.UnspecifiedTime,
    block: AnimationScope<T, V>.() -> Unit = {}
) {
    val initialValue = animation.getValueFromNanos(0)
    val initialVelocityVector = animation.getVelocityVectorFromNanos(0)
    var lateInitScope: AnimationScope<T, V>? = null
    try {
        if (startTimeNanos == AnimationConstants.UnspecifiedTime) {
            val durationScale = coroutineContext.durationScale
            animation.callWithFrameNanos {
                lateInitScope = AnimationScope(...).apply {
                    // 第一帧
                    doAnimationFrameWithScale(it, durationScale, animation, this@animate, block)
                }
            }
        } else {
            lateInitScope = AnimationScope(...).apply {
                // 第一帧
                doAnimationFrameWithScale()
            }
        }
        // 后续帧
        while (lateInitScope!!.isRunning) {
            val durationScale = coroutineContext.durationScale
            animation.callWithFrameNanos {
                lateInitScope!!.doAnimationFrameWithScale(it, durationScale, animation, this, block)
            }
        }
        // 动画结束
    } catch (e: CancellationException) {
        lateInitScope?.isRunning = false
        if (lateInitScope?.lastFrameTimeNanos == lastFrameTimeNanos) {
            isRunning = false
        }
        throw e
    }
}

嗯,柳暗花明!这个动画函数从头到尾运行给定 animation 中定义的动画。在动画过程中,AnimationState 将被更新为最新的值,速度,帧时间等。

到这里 animateColorAsState 大概过了一遍,但也只是简单走了一遍流程,并没有深究里面的细节,比如 Animatable 类中都没看,runAnimation 方法也只是看了主要的代码等等。

结尾

本篇文章先写到这里吧,属性动画其实都差不多,区别只是泛型不同以及一些特定实现,大家如果有需要可以一个一个去看看。

本文所有源码基于 Compose 1.3.0-beta02

本文至此结束,有用的地方大家可以参考,当然如果能帮助到大家,哪怕是一点也足够了。就这样。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/57584.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java项目:ssm实验室设备管理系统

作者主页&#xff1a;源码空间站2022 简介&#xff1a;Java领域优质创作者、Java项目、学习资料、技术互助 文末获取源码 项目介绍 ssm实验室设备管理系统。前台jsplayuieasyui等框架渲染数据、后台java语言搭配ssm(spring、springmvc、mybatis、maven) 数据库mysql5.7、8.0版…

java - 数据结构,双向链表 - LinkedList

一、双向链表 &#xff08;不带头&#xff09; 无头双向链表&#xff1a;在Java的集合框架库中LinkedList底层实现就是无头双向循环链表 双向链表 和 单向链表的区别&#xff0c;就在于 双向 比 单向 多个 一个前驱地址。而且 你会发现 正因为有了前驱地址&#xff0c;所以所…

centos 安装和卸载 webmin

在centos里安装webmin 选择安装最新版本的安装包 官方下载路径可以查看下载版本http://download.webmin.com/download/yum/ wget http://download.webmin.com/download/yum/webmin-2.010-1.noarch.rpm如果安装提示 错误: 无法验证 prdownloads.sourceforge.net 的由 “/CUS…

15年架构师:再有面试官问你Kafka,就拿这篇学习笔记怼他

写在前面 Kafka是一个高度可扩展的消息系统&#xff0c;它在LinkedIn的中央数据库管理中扮演着十分重要的角色&#xff0c;因其可水平扩展和高吞吐率而被广泛使用&#xff0c;现在已经被多家不同类型的公司作为多种类型的数据管道和消息系统。 kafka的外在表现很像消息系统&a…

【图像分割】基于PCA结合模糊聚类算法FCM实现SAR图像分割附matlab代码

✅作者简介&#xff1a;热爱科研的Matlab仿真开发者&#xff0c;修心和技术同步精进&#xff0c;matlab项目合作可私信。 &#x1f34e;个人主页&#xff1a;Matlab科研工作室 &#x1f34a;个人信条&#xff1a;格物致知。 更多Matlab仿真内容点击&#x1f447; 智能优化算法 …

[附源码]计算机毕业设计疫情网课管理系统Springboot程序

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

代码随想录刷题Day53 | 1143. 最长公共子序列 | 1035. 不相交的线 | 53. 最大子数组和

代码随想录刷题Day53 | 1143. 最长公共子序列 | 1035. 不相交的线 | 53. 最大子数组和 1143. 最长公共子序列 题目&#xff1a; 给定两个字符串 text1 和 text2&#xff0c;返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 &#xff0c;返回 0 。 一个字…

创建Hibernate项目与实现一个例子(idea版)

文章目录创建Hibernate项目一、前提准备二、创建项目三、实现一个例子创建Hibernate项目 一、前提准备 准备Hibernate开发必需的jar包。准备数据库的驱动jar包。准备junit.jar包。 这些包你可以去官网下载&#xff0c;也可以下载我已下载好的(本人目前使用的)。 https://pan…

【机器学习】评价指标 : 准确率,查准率与查全率

引言 在机器学习中&#xff0c;有几个评价指标&#xff0c;今天专门来介绍一下。之前的学习中主要是看模型&#xff0c;学算法&#xff0c;突然有一天发现&#xff0c;机器学习中的一些基本概念还是有点模糊&#xff0c;导致不知道如何综合评价模型的好坏。 这篇文章主要介绍如…

HTML5期末考核大作业:基于Html+Css+javascript的网页制作(化妆品公司网站制作)

&#x1f389;精彩专栏推荐 &#x1f4ad;文末获取联系 ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业&#xff1a; 【&#x1f4da;毕设项目精品实战案例 (10…

【强化学习论文合集 | 2018年合集】一. ICML-2018 强化学习论文

强化学习(Reinforcement Learning, RL),又称再励学习、评价学习或增强学习,是机器学习的范式和方法论之一,用于描述和解决智能体(agent)在与环境的交互过程中通过学习策略以达成回报最大化或实现特定目标的问题。 本专栏整理了近几年国际顶级会议中,涉及强化学习(Rein…

我的数学学习回忆录——一个数学爱好者的反思(二)

早点关注我&#xff0c;精彩不错过&#xff01;上回说到我在数学学习过程中走的种种弯路&#xff0c;相关内容请戳&#xff1a;我的数学学习回忆录——一个数学爱好者的反思&#xff08;一&#xff09;那在这样坎坷的旅程中&#xff0c;有没有给我带来意外惊喜&#xff0c;是不…

C++中的类型转换

文章目录一、隐式类型转换二、显式类型转换三、c风格的类型转换一、隐式类型转换 隐式类型转换&#xff0c;顾名思义&#xff0c;就是没有明显的声明要进行类型转换&#xff0c;隐式类型转换有可能造成数据精度的丢失&#xff0c;所以通常所做的类型转换都是从size小的数据到si…

哈夫曼编码(Huffman coding)

哈夫曼编码哈夫曼编码简介发展历史思想示例不足哈夫曼编码 简介 哈夫曼编码(Huffman Coding)&#xff0c;又称霍夫曼编码&#xff0c;是一种编码方式&#xff0c;哈夫曼编码是可变字长编码(VLC)的一种。Huffman于1952年提出一种编码方法&#xff0c;该方法完全依据字符出现概…

[附源码]JAVA毕业设计计算机在线学习管理系统-(系统+LW)

[附源码]JAVA毕业设计计算机在线学习管理系统-&#xff08;系统LW&#xff09; 目运行 环境项配置&#xff1a; Jdk1.8 Tomcat8.5 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项…

实现自定义Spring Boot Starter

实现自定义Spring Boot Starter一、原理二、实战1 自定义 Spring Boot Starter1.1 添加maven依赖1.2 属性类AuthorProperties1.3 自动配置类AuthorAutoConfiguration1.4 业务逻辑AuthorServer1.5 spring.factories2 测试自定义的 Spring Boot Starter2.1 新建module或者新建工程…

什么软件能识别软件?学会这几个软件就可以了

在日常学习或工作中&#xff0c;我们经常会因为各种各样的原因&#xff0c;导致资料无法记全。比如上课的时候老师讲课速度过快、或者开会时需要整理的资料太多&#xff0c;我们做不到一心二用&#xff0c;边听边记。你们遇到类似情况的时候&#xff0c;都是怎么解决的呢&#…

0x02. Spring Boot 3 之SpringBoot 版本升级最佳实践指南

Spring Boot 3 之SpringBoot低版本升级最佳实践0x01 前言0x02 升级Spring Boot2.1 从Spring Boot 1.5.x 升级到Spring Boot 2.x2.1.1 依赖检查2.1.2 检查自定义配置2.1.3 检查系统需要2.1.4 升级到Spring Boot 2.x2.1.5 配置属性迁移2.2 从Spring Boot 2.7.x 升级到Spring Boot…

概率统计·样本及抽样分布【随机样本、抽样分布】

总体与个体 总体&#xff1a;试验的全部可能的观察值称为总体 个体&#xff1a;总体中每个可能的观察值称为个体 总体期望样本平均期望 总体方差/n样本平均方差 随机样本 X1&#xff0c;X2……Xn相互独立&#xff08;x1&#xff0c;x2……xn是观察值&#xff09;&#xff0…

[附源码]计算机毕业设计springboot疫苗及注射管理系统

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…