Lua 应用开发LVGL全局函数与常量全局函数

Anim 动画

LVGL 动画对象

构造

Anim(var: any, opts: table) -> anim
obj:Anim(opts: table) -> anim       -- 语法糖

opts 参数

键类型说明
start_valueinteger起始值
end_valueinteger结束值
time / durationinteger动画时长(ms)
delayinteger启动延迟(ms)
repeat_countinteger重复次数,-1 为无限
repeat_delayinteger重复间延迟(ms)
early_applyinteger/boolean立即应用首帧
playback_timeinteger回放时长(往返动画)
playback_delayinteger回放前延迟
pathstring缓动:"linear" / "ease_in" / "ease_out" / "ease_in_out" / "overshoot" / "bounce" / "step"
exec_cbfunction执行回调 function(obj, value)
done_cbfunction完成回调 function(anim, obj)
runbooleantrue 则立即启动

实例方法

方法返回值说明
a:set(opts)anim更新参数
a:startanim启动动画
a:stopnil停止动画
a:deletenil删除动画

示例

local a = obj:Anim{
  start_value = 0,
  end_value   = 120,
  duration    = 1000,
  path        = "ease_out",
  exec_cb     = function(o, v)
    o:set({ x = v })
  end,
  run = true,
}

社区补充(洛汐文档库)

以下内容来自洛汐文档库《LVGL 样式、事件与动画》:docs.luoxe.cn/docs/vela/lua/lvgl-style-animation

控件动画 object:anim

预装脚本使用以下形式:

image:anim {
  property = "x",
  from = -100,
  to = 100,
  duration = 800,
  repeat_count = -1,
  path = "linear",
}

常见字段:

字段类型说明
propertystring要变化的属性,如 x、y、opa、angle、zoom
from / tonumber起始值与结束值
durationinteger单程时长,毫秒
delayinteger开始延迟,毫秒
repeat_countinteger重复次数;-1 表示无限
repeat_delayinteger每轮重复前延迟
playback_durationinteger回放时长
playback_delayinteger回放前延迟
pathstring/function插值路径
ready_cbfunction动画结束回调

停止对象上的动画:

object:remove_all_anim

Anim 对象

lvgl.Anim(object, options) -> animation
object:Anim(options) -> animation
字段类型说明
start_valueinteger起始值
end_valueinteger结束值
time / durationinteger动画时长,毫秒
delayinteger启动延迟,毫秒
repeat_countinteger重复次数;-1 为无限
repeat_delayinteger重复间延迟
early_applyboolean/integer是否立即应用首帧
playback_timeinteger反向回放时长
playback_delayinteger回放前延迟
pathstringlinear、ease_in、ease_out、ease_in_out、overshoot、bounce 或 step
exec_cbfunctionfunction(object, value),应用当前值
done_cbfunctionfunction(animation, object),完成回调
runboolean创建后立即启动

实例方法:

方法说明
animation:set(options)更新参数
animation:start启动
animation:stop停止
animation:delete删除
local animation = object:Anim {
  start_value = 0,
  end_value = 120,
  duration = 1000,
  path = "ease_out",
  exec_cb = function(target, value)
    target:set { x = value }
  end,
  run = true,
}

需要 JSON 状态动画时使用 animengine。

页面生命周期

function pageOnPause
  object:remove_all_anim
end

function pageOnResume
  -- 按当前状态重新建立动画
end

无限动画会持续唤醒 UI 线程。页面隐藏后应停止,回到页面时重新创建。

参考来源:官方 docs-lua / 洛汐文档库

大纲