news 2026/6/10 16:55:51

Compose笔记(六十四)--LottieAnimation

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Compose笔记(六十四)--LottieAnimation

这一节主要了解一下Compose中的LottieAnimation,在Jetpack Compose中,LottieAnimation是一个用于渲染Lottie动画的Composable组件。它由Lottie-Android库提供支持,允许开发者在Android应用中轻松展示复杂的动画效果.简单总结如下:

API:
composition:Lottie 动画的解析结果,包含动画数据
progress:控制动画进度的函数,返回0f(开始)到1f(结束)之间的值。
outlineMasksAndMattes:是否对蒙版和遮罩启用轮廓描边
applyOpacityToLayers:是否将透明度应用到整个图层
enableMergePaths:是否启用After Effects的“合并路径”功能。
renderMode:选择渲染模式,影响动画绘制方式。
dynamicProperties:动态修改动画属性(如颜色、透明度)的回调接口。
asyncUpdates:控制动画帧更新的线程策略。

栗子:

implementation("com.airbnb.android:lottie-compose:6.4.0")
import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp import com.airbnb.lottie.compose.LottieAnimation import com.airbnb.lottie.compose.LottieCompositionSpec import com.airbnb.lottie.compose.rememberLottieComposition import com.example.composedemo0802.R @Composable fun LottieDemo() { val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.animation)) LottieAnimation( composition = composition, modifier = Modifier.size(200.dp) ) }
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.size import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.unit.dp import com.airbnb.lottie.RenderMode import com.airbnb.lottie.compose.LottieAnimation import com.airbnb.lottie.compose.LottieClipSpec import com.airbnb.lottie.compose.LottieCompositionSpec import com.airbnb.lottie.compose.LottieConstants import com.airbnb.lottie.compose.rememberLottieAnimatable import com.airbnb.lottie.compose.rememberLottieComposition import com.example.composedemo0802.R @Composable fun LottieDemo() { val composition by rememberLottieComposition(LottieCompositionSpec.RawRes(R.raw.animation)) val animatable = rememberLottieAnimatable() var isPlaying by remember { mutableStateOf(true) } LaunchedEffect(isPlaying) { if (isPlaying) { animatable.animate( composition = composition!!, clipSpec = LottieClipSpec.Progress(0f, 1f), speed = 1.5f, iterations = LottieConstants.IterateForever ) } } Column( modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ) { LottieAnimation( composition = composition, progress = { animatable.value }, modifier = Modifier.size(300.dp), renderMode = RenderMode.HARDWARE, contentScale = ContentScale.Crop ) Spacer(modifier = Modifier.height(16.dp)) Button(onClick = { isPlaying = !isPlaying }) { Text(if (isPlaying) "Pause" else "Play") } } }

注意:
1 优先使用本地raw资源(而非assets或网络),加载更快更稳定。
2 避免在LottieAnimation内部频繁重建composition,用rememberLottieComposition缓存。
3 不要在列表(LazyColumn)中大量使用高帧率Lottie,可能影响滚动性能

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/9 22:21:06

YOLO模型太大无法部署?教你用GPU+TensorRT极致压缩

YOLO模型太大无法部署?教你用GPUTensorRT极致压缩 在工业质检线上,每秒流过数十个零件,系统必须在几十毫秒内完成缺陷检测;在自动驾驶车辆中,摄像头实时捕捉道路画面,任何延迟都可能带来安全隐患。这些场景…

作者头像 李华
网站建设 2026/6/10 11:11:23

Next AI Draw.io 核心实现深度分析

Next AI Draw.io 核心实现深度分析 请关注公众号【碳硅化合物AI】 前言 大家好!上一篇我们聊了项目的整体架构,今天咱们深入代码,看看这 8 个核心模块是怎么实现的。我会从入口类开始,分析关键类的关系,然后用时序图…

作者头像 李华
网站建设 2026/6/10 12:51:33

YOLO开源项目推荐:这10个高星项目都用了我们的GPU

YOLO开源项目推荐:这10个高星项目都用了我们的GPU 在智能制造车间的高速生产线上,每分钟有上千个产品经过质检工位。传统人工检测不仅效率低、易疲劳,还难以应对微米级缺陷识别。如今,越来越多工厂选择部署基于YOLO的目标检测系统…

作者头像 李华
网站建设 2026/6/10 12:49:19

YOLO模型支持Vaex大数据集处理,减少内存占用

YOLO 模型集成 Vaex 实现高效大数据集处理 在智能制造与边缘 AI 快速发展的今天,一个常被忽视却至关重要的问题浮出水面:当目标检测模型已经能在毫秒级完成推理时,为什么训练前的数据准备动辄耗时数十分钟?尤其在拥有数千万张图像…

作者头像 李华