当前位置:   article > 正文

Qt Quick 动画入门篇_qt动画

qt动画

简介

在使用 QML 进行界面开发时,我们的目标是创建一套流体界面,所谓流体界面指的是UI组件是动态变化的。举个例子,当界面上的组件需要变化时,如果视觉画布突然变化会导致用户的体验感比较差。而如果在状态的变化过程中,我们添加一些引导,把状态从初始慢慢变化到目标状态,让用户可以感受到这个变化的过程,那么用户的感官体验这一块就会大大提升了,而这也就是所谓的动态变化

这里需要引出几个重要的概念:State(状态)、Transition(过度)、Animation(动画)

State(状态):所有项目都有一个默认状态,用于定义对象和特性值的默认配置。可以通过向 states 属性添加状态项来定义新状态,以允许项在不同配置之间切换

Transition(过度):发生状态更改时要应用的动画

Animation(动画):随着时间的推移逐渐改变属性

具体请参考官方文档:Important Concepts in Qt Quick - States, Transitions and Animations

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,C++设计模式,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

缓和曲线

通常动画中的变化是匀速的,如果开发者觉得太单调了,可以调节动画的变化速度曲线,即缓和曲线。我们通过动画的 easing 属性组来改变缓和曲线

type - 缓和曲线类型

amplitude - 幅度

period - 周期

overshoot - 过冲

bezierCurve - 贝塞尔

 

缓和曲线的类型如下所示:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

动画类型

AnchorAnimation - 为锚定值的更改设置动画

 

  1. Rectangle {
  2. id: myRect
  3. width: 100; height: 100
  4. color: "red"
  5. property int type: 1
  6. states: [
  7. State {
  8. name: "right"
  9. AnchorChanges { target: myRect; anchors.right: parent.right }
  10. },
  11. State {
  12. name: "bottom"
  13. AnchorChanges { target: myRect; anchors.bottom: parent.bottom }
  14. },
  15. State {
  16. name: "left"
  17. AnchorChanges { target: myRect; anchors.left: parent.left }
  18. },
  19. State {
  20. name: "top"
  21. AnchorChanges { target: myRect; anchors.top: parent.top }
  22. }
  23. ]
  24. transitions: Transition {
  25. AnchorAnimation { duration: 500 }
  26. }
  27. MouseArea {
  28. anchors.fill: parent
  29. onClicked: {
  30. let state = {
  31. 1: "right",
  32. 2: "bottom",
  33. 3: "left",
  34. 4: "top"
  35. }
  36. myRect.state = state[myRect.type]
  37. myRect.type++
  38. if ( myRect.type > 4 ) {
  39. myRect.type = 1
  40. }
  41. }
  42. }
  43. }

ColorAnimation - 为颜色值的更改设置动画

 

  1. Rectangle {
  2. id: myRect
  3. width: 100; height: 100
  4. color: "black"
  5. property int type: 1
  6. states: [
  7. State {
  8. name: "red"
  9. PropertyChanges { target: myRect; color: "red" }
  10. },
  11. State {
  12. name: "yellow"
  13. PropertyChanges { target: myRect; color: "yellow" }
  14. },
  15. State {
  16. name: "pink"
  17. PropertyChanges { target: myRect; color: "pink" }
  18. },
  19. State {
  20. name: "blue"
  21. PropertyChanges { target: myRect; color: "blue" }
  22. },
  23. State {
  24. name: "black"
  25. PropertyChanges { target: myRect; color: "black" }
  26. }
  27. ]
  28. transitions: Transition {
  29. ColorAnimation { duration: 500 }
  30. }
  31. MouseArea {
  32. anchors.fill: parent
  33. onClicked: {
  34. let state = {
  35. 1: "red",
  36. 2: "yellow",
  37. 3: "pink",
  38. 4: "blue",
  39. 5: "black",
  40. }
  41. myRect.state = state[myRect.type]
  42. myRect.type++
  43. if ( myRect.type > 5 ) {
  44. myRect.type = 1
  45. }
  46. }
  47. }
  48. }

NumberAnimation - 为 qreal 类型值的更改设置动画

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,C++设计模式,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

 

  1. Rectangle {
  2. id: myRect
  3. width: 100; height: 100
  4. color: "red"
  5. property int type: 1
  6. states: [
  7. State {
  8. name: "step1"
  9. PropertyChanges { target: myRect; x: 60; y: 60; width: 120; height: 120; rotation: 30 }
  10. },
  11. State {
  12. name: "step2"
  13. PropertyChanges { target: myRect; x: 60; y: 60; width: 120; height: 120; rotation: 60 }
  14. },
  15. State {
  16. name: "step3"
  17. PropertyChanges { target: myRect; x: 40; y: 40; width: 180; height: 180; rotation: 90 }
  18. },
  19. State {
  20. name: "step4"
  21. PropertyChanges { target: myRect; x: 100; y: 100; width: 50; height: 50; rotation: 45 }
  22. }
  23. ]
  24. transitions: Transition {
  25. NumberAnimation { duration: 500 }
  26. }
  27. MouseArea {
  28. anchors.fill: parent
  29. onClicked: {
  30. let state = {
  31. 1: "step1",
  32. 2: "step2",
  33. 3: "step3",
  34. 4: "step4"
  35. }
  36. myRect.state = state[myRect.type]
  37. myRect.type++
  38. if ( myRect.type > 4 ) {
  39. myRect.type = 1
  40. }
  41. }
  42. }
  43. }

ParentAnimation - 为父类的更改设置动画

 

  1. Rectangle {
  2. id: myRect
  3. width: 300; height: 100
  4. color: "black"
  5. property int type: 1
  6. Rectangle {
  7. id: redRect
  8. width: 100; height: 100
  9. color: "red"
  10. }
  11. Rectangle {
  12. id: blueRect
  13. x: 110
  14. width: 100; height: 100
  15. color: "blue"
  16. }
  17. Rectangle {
  18. id: yellowRect
  19. x: 220; y: 10
  20. width: 50; height: 50
  21. color: "yellow"
  22. states: [
  23. State {
  24. name: "red"
  25. ParentChange { target: yellowRect; parent: redRect; x: 10; y: 10 }
  26. },
  27. State {
  28. name: "blue"
  29. ParentChange { target: yellowRect; parent: blueRect; x: 10; y: 10 }
  30. },
  31. State {
  32. name: "black"
  33. ParentChange { target: yellowRect; parent: myRect; x: 220; y: 10 }
  34. }
  35. ]
  36. transitions: Transition {
  37. ParentAnimation {
  38. NumberAnimation { properties: "x,y"; duration: 500 }
  39. }
  40. }
  41. MouseArea {
  42. anchors.fill: parent
  43. onClicked: {
  44. let state = {
  45. 1: "red",
  46. 2: "blue",
  47. 3: "black"
  48. }
  49. yellowRect.state = state[myRect.type]
  50. myRect.type++
  51. if ( myRect.type > 3 ) {
  52. myRect.type = 1
  53. }
  54. }
  55. }
  56. }
  57. }

PathAnimation - 沿路径设置项目的动画

 

  1. Rectangle {
  2. width: 400; height: 400
  3. PathInterpolator {
  4. id: motionPath
  5. path: Path {
  6. startX: 0; startY: 0
  7. PathCubic {
  8. x: 350; y: 350
  9. control1X: 350; control1Y: 0
  10. control2X: 0; control2Y: 350
  11. }
  12. }
  13. NumberAnimation on progress { id: animation; from: 0; to: 1; duration: 2000 }
  14. }
  15. Rectangle {
  16. x: motionPath.x; y: motionPath.y
  17. width: 50; height: 50
  18. rotation: motionPath.angle
  19. color: "green"
  20. }
  21. MouseArea {
  22. anchors.fill: parent
  23. onClicked: animation.start()
  24. }
  25. }

PropertyAnimation - 为特性值的更改设置动画

 

  1. Rectangle {
  2. width: 100; height: 100
  3. color: "red"
  4. property bool location: true
  5. Behavior on x { PropertyAnimation {} }
  6. MouseArea {
  7. anchors.fill: parent
  8. onClicked: {
  9. var x = location ? 50 : 0
  10. parent.x = x
  11. location = !location
  12. }
  13. }
  14. }

RotationAnimation - 为旋转的更改设置动画

 

  1. Item {
  2. width: 300; height: 300
  3. Rectangle {
  4. id: myRect
  5. width: 150; height: 100
  6. anchors.centerIn: parent
  7. color: "red"
  8. antialiasing: true
  9. property int type: 1
  10. states: [
  11. State {
  12. name: "state1"
  13. PropertyChanges { target: myRect; rotation: 45 }
  14. },
  15. State {
  16. name: "state2"
  17. PropertyChanges { target: myRect; rotation: 90 }
  18. },
  19. State {
  20. name: "state3"
  21. PropertyChanges { target: myRect; rotation: 180 }
  22. }
  23. ]
  24. transitions: Transition {
  25. RotationAnimation { duration: 1000; direction: RotationAnimation.Counterclockwise }
  26. }
  27. }
  28. MouseArea {
  29. anchors.fill: parent
  30. onClicked: {
  31. let state = {
  32. 1: "state1",
  33. 2: "state2",
  34. 3: "state3"
  35. }
  36. myRect.state = state[myRect.type]
  37. myRect.type++
  38. if ( myRect.type > 3 ) {
  39. myRect.type = 1
  40. }
  41. }
  42. }
  43. }

Vector3dAnimation - 为 QVector3d 值的更改设置动画

 

  1. Rectangle {
  2. id: myRect
  3. x: 50; y: 50
  4. width: 100; height: 100
  5. color: "red"
  6. transform: Rotation {
  7. angle: 45
  8. origin.x: 50; origin.y: 50
  9. axis: Qt.vector3d(0, 1, 0)
  10. SequentialAnimation on axis {
  11. id: animation
  12. running: false
  13. Vector3dAnimation { from: "1, 0, 0"; to: "0, 1, 0"; duration: 1000 }
  14. Vector3dAnimation { from: "0, 1, 0"; to: "0, 0, 1"; duration: 1000 }
  15. Vector3dAnimation { from: "0, 0, 1"; to: "1, 0, 1"; duration: 1000 }
  16. Vector3dAnimation { from: "1, 0, 1"; to: "1, 1, 0"; duration: 1000 }
  17. Vector3dAnimation { from: "1, 1, 0"; to: "1, 1, 1"; duration: 1000 }
  18. Vector3dAnimation { from: "1, 1, 1"; to: "1, 0, 1"; duration: 1000 }
  19. Vector3dAnimation { from: "1, 0, 1"; to: "0, 0, 1"; duration: 1000 }
  20. Vector3dAnimation { from: "0, 0, 1"; to: "0, 1, 0"; duration: 1000 }
  21. Vector3dAnimation { from: "0, 1, 0"; to: "1, 0, 0"; duration: 1000 }
  22. }
  23. }
  24. MouseArea {
  25. anchors.fill: parent
  26. onClicked: animation.start()
  27. }
  28. }

组合动画

  • SequentialAnimation - 按顺序运行动画

 

  1. Rectangle {
  2. id: rect
  3. width: 100; height: 100
  4. color: "red"
  5. SequentialAnimation {
  6. id: animation
  7. running: false
  8. NumberAnimation { target: rect; property: "x"; to: 100; duration: 500 }
  9. NumberAnimation { target: rect; property: "y"; to: 100; duration: 500 }
  10. NumberAnimation { target: rect; property: "x"; to: 0; duration: 500 }
  11. NumberAnimation { target: rect; property: "y"; to: 0; duration: 500 }
  12. }
  13. MouseArea {
  14. anchors.fill: parent
  15. onClicked: animation.start()
  16. }
  17. }

ParallelAnimation - 并行运行动画

 

  1. Rectangle {
  2. id: rect
  3. width: 100; height: 100
  4. color: "red"
  5. ParallelAnimation {
  6. id: animation
  7. running: false
  8. NumberAnimation { target: rect; property: "x"; to: 100; duration: 1000 }
  9. NumberAnimation { target: rect; property: "y"; to: 100; duration: 1000 }
  10. NumberAnimation { target: rect; property: "width"; to: 200; duration: 1000 }
  11. NumberAnimation { target: rect; property: "height"; to: 200; duration: 1000 }
  12. NumberAnimation { target: rect; property: "rotation"; to: 90; duration: 1000 }
  13. }
  14. MouseArea {
  15. anchors.fill: parent
  16. onClicked: animation.start()
  17. }
  18. }

行为动画

  • Behavior - 为特性更改指定默认动画

 

  1. Rectangle {
  2. id: coloredRect
  3. width: 100; height: 100
  4. anchors.centerIn: parent
  5. color: "red"
  6. states: State {
  7. name: "GreenState"
  8. when: mouser.containsMouse
  9. PropertyChanges {
  10. target: coloredRect
  11. color: "green"
  12. }
  13. }
  14. Behavior on color { ColorAnimation {} }
  15. MouseArea {
  16. id: mouser
  17. anchors.fill: parent
  18. hoverEnabled: true
  19. }
  20. }

动画暂停

  • PauseAnimation - 在动画中引入暂停

 

  1. Rectangle {
  2. id: rect
  3. width: 100; height: 100
  4. color: "red"
  5. SequentialAnimation {
  6. id: animation
  7. running: false
  8. NumberAnimation { target: rect; property: "x"; to: 100; duration: 500 }
  9. NumberAnimation { target: rect; property: "y"; to: 100; duration: 500 }
  10. PauseAnimation { duration: 1000 }
  11. NumberAnimation { target: rect; property: "x"; to: 0; duration: 500 }
  12. NumberAnimation { target: rect; property: "y"; to: 0; duration: 500 }
  13. }
  14. MouseArea {
  15. anchors.fill: parent
  16. onClicked: animation.start()
  17. }
  18. }

弹簧动画

  • SpringAnimation - 模拟弹簧的振荡行为,使用适当的弹簧常数来控制加速度和阻尼,以控制效果消失的速度

 

  1. Rectangle {
  2. id: rect
  3. width: 50; height: 50
  4. color: "red"
  5. Behavior on x { SpringAnimation { spring: 2; damping: 0.2 } }
  6. Behavior on y { SpringAnimation { spring: 2; damping: 0.2 } }
  7. MouseArea {
  8. anchors.fill: parent
  9. onClicked: {
  10. rect.x = mouse.x - rect.width/2
  11. rect.y = mouse.y - rect.height/2
  12. }
  13. }
  14. }

属性跟踪动画

  • SmoothedAnimation - 允许特性平滑跟踪值

 

  1. Item {
  2. Rectangle {
  3. width: 60; height: 60
  4. x: rect1.x - 5; y: rect1.y - 5
  5. color: "green"
  6. Behavior on x { SmoothedAnimation { velocity: 200; duration: 500 } }
  7. Behavior on y { SmoothedAnimation { velocity: 200; duration: 500 } }
  8. }
  9. Rectangle {
  10. id: rect1
  11. x: 5; y: 5
  12. width: 50; height: 50
  13. color: "red"
  14. }
  15. focus: true
  16. Keys.onRightPressed: rect1.x = rect1.x + 100
  17. Keys.onLeftPressed: rect1.x = rect1.x - 100
  18. Keys.onUpPressed: rect1.y = rect1.y - 100
  19. Keys.onDownPressed: rect1.y = rect1.y + 100
  20. }

动画中运行脚本

  • ScriptAction - 在动画中运行脚本

 

  1. Rectangle {
  2. id: rect
  3. width: 100; height: 100
  4. color: "red"
  5. SequentialAnimation {
  6. id: animation
  7. running: false
  8. NumberAnimation { target: rect; property: "x"; to: 100; duration: 500 }
  9. NumberAnimation { target: rect; property: "y"; to: 100; duration: 500 }
  10. ScriptAction { script: doSomething() }
  11. NumberAnimation { target: rect; property: "x"; to: 0; duration: 500 }
  12. NumberAnimation { target: rect; property: "y"; to: 0; duration: 500 }
  13. ScriptAction { script: doStateStuff() }
  14. }
  15. MouseArea {
  16. anchors.fill: parent
  17. onClicked: animation.start()
  18. }
  19. function doSomething() {
  20. rect.color = "blue"
  21. rect.width = 120
  22. rect.height = 120
  23. }
  24. function doStateStuff() {
  25. rect.color = "red"
  26. rect.width = 100
  27. rect.height = 100
  28. }
  29. }

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,C++设计模式,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/81533
推荐阅读
  

闽ICP备14008679号