跳到主要内容

动画播放

⚠️ 已弃用:请使用状态机代替运行时直接播放动画

在 Android 运行时中,使用 riveAnimation 属性指定一个动画。

<app.rive.runtime.kotlin.RiveAnimationView
app:riveAutoPlay="true"
app:riveArtboard="Square"
app:riveAnimation="rollaround"
app:riveResource="@raw/artboard_animations" />

animationView.setRiveResource(
R.raw.artboard_animations,
artboardName = "Square",
animationName = "rollaround",
autoplay = true
)

调用播放控制

在动画视图上设置 Rive 资源后,可以调用动画播放控制方法。

除了以编程方式播放动画外,还可以根据需要选择动画的循环模式和方向作为附加参数。此外,也可以暂停或停止动画。

// 播放一个动画
animationView.play("rollaround")

// 设置循环模式和方向
animationView.play("rollaround", Loop.ONE_SHOT, Direction.Backwards)

animationView.pause()
animationView.pause("bouncing")

animationView.stop()
animationView.stop("bouncing")

动画事件监听器

Rive Android 运行时也支持监听器注册。有关工作原理的示例,请查看 rive player 中的事件部分。

val listener = object : Listener {
override fun notifyPlay(animation: PlayableInstance) {
var text: String? = null
if (animation is LinearAnimationInstance) {
text = animation.name
}
}

override fun notifyPause(animation: PlayableInstance) {
var text: String? = null
if (animation is LinearAnimationInstance) {
text = animation.name
}
}

override fun notifyStop(animation: PlayableInstance) {
var text: String? = null
if (animation is LinearAnimationInstance) {
text = animation.name
}
}

override fun notifyLoop(animation: PlayableInstance) {
var text: String? = null
if (animation is LinearAnimationInstance) {
text = animation.name
}
}
}
animationView.registerListener(listener)

查看此 Activity 示例: LoopModeActivity.kt