Roson讲Qt#36 QML动画实例大全

绝地灬酷狼 2022-09-13 15:16 239阅读 0赞

1.颜色动画(ColorAnimation)

下面这个示例使用颜色的变化来展示天空和草坪从白天到晚上的变化。

  1. gradient: Gradient {
  2. GradientStop {
  3. position: 0.0
  4. SequentialAnimation on color {
  5. loops: Animation.Infinite
  6. ColorAnimation { from: "#14148c"; to: "#0E1533"; duration: 5000 }
  7. ColorAnimation { from: "#0E1533"; to: "#14148c"; duration: 5000 }
  8. }
  9. }
  10. GradientStop {
  11. position: 1.0
  12. SequentialAnimation on color {
  13. loops: Animation.Infinite
  14. ColorAnimation { from: "#14aaff"; to: "#437284"; duration: 5000 }
  15. ColorAnimation { from: "#437284"; to: "#14aaff"; duration: 5000 }
  16. }
  17. }
  18. }

e96fc21f0dc44f4ba8def56edadc46af.gif

2.属性动画(PropertyAnimation)

下面这个示例使用数字动画来展示一个上下弹跳的圆圈。

  1. // Animate the y property. Setting loops to Animation.Infinite makes the
  2. // animation repeat indefinitely, otherwise it would only run once.
  3. SequentialAnimation on y {
  4. loops: Animation.Infinite
  5. // Move from minHeight to maxHeight in 300ms, using the OutExpo easing function
  6. NumberAnimation {
  7. from: smiley.minHeight; to: smiley.maxHeight
  8. easing.type: Easing.OutExpo; duration: 300
  9. }
  10. // Then move back to minHeight in 1 second, using the OutBounce easing function
  11. NumberAnimation {
  12. from: smiley.maxHeight; to: smiley.minHeight
  13. easing.type: Easing.OutBounce; duration: 1000
  14. }
  15. // Then pause for 500ms
  16. PauseAnimation { duration: 500 }
  17. }

66d87c3b5141405ca1f4a7210e48cfff.gif

3.动画家(Animators)

下面这个示例使用动画家来展示一个上下弹跳的图标

  1. SequentialAnimation {
  2. SequentialAnimation {
  3. ParallelAnimation {
  4. YAnimator {
  5. target: smiley;
  6. from: smiley.minHeight;
  7. to: smiley.maxHeight
  8. easing.type: Easing.OutExpo;
  9. duration: 300
  10. }
  11. ScaleAnimator {
  12. target: shadow
  13. from: 1
  14. to: 0.5
  15. easing.type: Easing.OutExpo;
  16. duration: 300
  17. }
  18. }
  19. ParallelAnimation {
  20. YAnimator {
  21. target: smiley;
  22. from: smiley.maxHeight;
  23. to: smiley.minHeight
  24. easing.type: Easing.OutBounce;
  25. duration: 1000
  26. }
  27. ScaleAnimator {
  28. target: shadow
  29. from: 0.5
  30. to: 1
  31. easing.type: Easing.OutBounce;
  32. duration: 1000
  33. }
  34. }
  35. }
  36. PauseAnimation { duration: 500 }
  37. running: true
  38. loops: Animation.Infinite
  39. }

435b90199f934b0fa2355f203ff0df17.gif

4. Behaviors(Behaviors)

下面这个示例使用Behaviors将矩形移动到您鼠标的位置。

  1. // Set an 'elastic' behavior on the focusRect's y property.
  2. Behavior on y {
  3. NumberAnimation { easing.type: Easing.OutElastic; easing.amplitude: 3.0; easing.period: 2.0; duration: 300 }
  4. }

7cf19da57c7645ee907ba4f91e2226a0.gif

5.扭动的文本(Wiggly Text)

Wiggly Text演示了使用更复杂的行为来动画和拖动文本。它通过给每个字母分配一个复杂的绑定来实现:

  1. x: follow ? follow.x + follow.width : container.width / 6
  2. y: follow ? follow.y : container.height / 2

然后,它使用行为来动画每个字母的运动:

  1. Behavior on x { enabled: container.animated; SpringAnimation { spring: 3; damping: 0.3; mass: 1.0 } }
  2. Behavior on y { enabled: container.animated; SpringAnimation { spring: 3; damping: 0.3; mass: 1.0 } }

2b024011ae624453969c94d5d4bea5c4.gif

6.电视网球( Tv Tennis)

电视网球使用复杂的行为,使球拍跟随球,模拟无限网球游戏。同样,依赖于其他值的绑定被应用到位置和动画提供的行为。

  1. y: ball.direction == 'left' ? ball.y - 45 : page.height/2 -45;
  2. Behavior on y { SpringAnimation{ velocity: 300 } }

3bca4f7f8ccd4957b34382e9ca2ba7d6.gif

7.缓冲曲线( Easing Curves)

6e03d51e625745f6a8abcd9fcf1e4b19.gif

8.状态( States)

States演示了项目的属性如何在不同的状态之间变化。
它定义了几个状态:

  1. // In state 'middleRight', move the image to middleRightRect
  2. State {
  3. name: "middleRight"
  4. PropertyChanges { target: userIcon; x: middleRightRect.x; y: middleRightRect.y }
  5. },
  6. // In state 'bottomLeft', move the image to bottomLeftRect
  7. State {
  8. name: "bottomLeft"
  9. PropertyChanges { target: userIcon; x: bottomLeftRect.x; y: bottomLeftRect.y }
  10. }

eeb7d3f85f2a4abe9af2adb60696839c.gif

9.转换( Transitions)

Transitions以States为例,并通过设置Transitions来动画属性的变化:

  1. // Transitions define how the properties change when the item moves between each state
  2. transitions: [
  3. // When transitioning to 'middleRight' move x,y over a duration of 1 second,
  4. // with OutBounce easing function.
  5. Transition {
  6. from: "*"; to: "middleRight"
  7. NumberAnimation { properties: "x,y"; easing.type: Easing.OutBounce; duration: 1000 }
  8. },
  9. // When transitioning to 'bottomLeft' move x,y over a duration of 2 seconds,
  10. // with InOutQuad easing function.
  11. Transition {
  12. from: "*"; to: "bottomLeft"
  13. NumberAnimation { properties: "x,y"; easing.type: Easing.InOutQuad; duration: 2000 }
  14. },
  15. // For any other state changes move x,y linearly over duration of 200ms.
  16. Transition {
  17. NumberAnimation { properties: "x,y"; duration: 200 }
  18. }

8f4d24e285e647d19ba4e598a9a129c4.gif

10.路径动画(PathAnimation)

使用PathAnimation沿贝塞尔曲线动画图像。

  1. PathAnimation {
  2. id: pathAnim
  3. duration: 2000
  4. easing.type: Easing.InQuad
  5. target: box
  6. orientation: PathAnimation.RightFirst
  7. anchorPoint: Qt.point(box.width/2, box.height/2)
  8. path: Path {
  9. startX: 50; startY: 50
  10. PathCubic {
  11. x: window.width - 50
  12. y: window.height - 50
  13. control1X: x; control1Y: 50
  14. control2X: 50; control2Y: y
  15. }
  16. onChanged: canvas.requestPaint()
  17. }
  18. }

995fa4b1585549baadd625259a84f6b6.gif

11.路径插入器(PathInterpolator)

使用PathInterpolator沿贝塞尔曲线动画图像。

  1. PathInterpolator {
  2. id: motionPath
  3. path: Path {
  4. startX: 50; startY: 50
  5. PathCubic {
  6. x: window.width - 50
  7. y: window.height - 50
  8. control1X: x; control1Y: 50
  9. control2X: 50; control2Y: y
  10. }
  11. onChanged: canvas.requestPaint()
  12. }
  13. SequentialAnimation on progress {
  14. running: true
  15. loops: -1
  16. PauseAnimation { duration: 1000 }
  17. NumberAnimation {
  18. id: progressAnim
  19. running: false
  20. from: 0; to: 1
  21. duration: 2000
  22. easing.type: Easing.InQuad
  23. }
  24. }
  25. }

818e7a3b8e554983a433c5beecfab14c.gif

发表评论

表情:
评论列表 (有 0 条评论,239人围观)

还没有评论,来说两句吧...

相关阅读