一种控制对象位置的动画。

构造方法

TranslateAnimation (fromXDelta, toXDelta, fromYDelta, toYDelta)

Parameters
fromXDelta 在动画开始时应用X坐标的变化
toXDelta 在动画结束时应用X坐标的变化
fromYDelta 在动画开始时应用的Y坐标变化
toYDelta 在动画结束时应用的Y坐标变化

TranslateAnimation ( fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue)

Parameters
fromXType 指定应该如何解释fromXValue。可选值
Animation.ABSOLUTE,
Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT.
fromXValue 在动画开始时应用X坐标的变化。如果fromXType是绝对的,这个值可以是绝对数字;如果是绝对的,这个值可以是百分比(1.0是100%)。
toXType 指定应如何解释toXValue。可选值
Animation.ABSOLUTE,
Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT.
toXValue 在动画结束时应用X坐标的变化。如果toXType是绝对的,这个值可以是绝对数值,如果是100%,这个值可以是百分比(1.0是100%)。
fromYType 指定fromYValue应该如何解释。可选值
Animation.ABSOLUTE,
Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT.
fromYValue 在动画开始时应用的Y坐标变化。如果fromYType是绝对的,这个值可以是绝对数字,否则可以是百分比(1.0是100%)。
toYType 指定应该如何解释toYValue,可选值
Animation.ABSOLUTE,
Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_PARENT.
toYValue 在动画结束时应用的Y坐标变化。如果toYType是绝对的,这个值可以是绝对数字;如果是绝对的,这个值可以是百分比(1.0是100%)。

示例:

  1. require "import"
  2. import "android.os.*"
  3. import "android.app.*"
  4. import "android.view.*"
  5. import "android.widget.*"
  6. import "android.view.animation.*"
  7. local layout = loadlayout({
  8. LinearLayout,
  9. layout_width = "fill",
  10. layout_height = "fill",
  11. gravity = "center",
  12. {
  13. Button,
  14. layout_width = "100dp",
  15. layout_height = "50dp",
  16. text = "开始动画",
  17. id = "button",
  18. },
  19. })
  20. activity.setContentView(layout)
  21. function initAnimate()
  22. -- 定义平移动画,用的第一种构造方法
  23. local animate = TranslateAnimation(0, 100, 0, 100)
  24. -- 设置动画时间
  25. animate.setDuration(3000)
  26. -- 点击按钮开始动画
  27. button.onClick = function()
  28. button.startAnimation(animate)
  29. end
  30. end
  31. function main()
  32. initAnimate()
  33. end

效果:

补间动画-平移动画 - 图1