QML提供了丰富的动画元素,说起动画,无非是给UI增光添彩罢了。在QML中,动画常常与State和Transition联系在一起,这几个概念(下面的例子中都用到了)都比较简单,相关介绍可查看Qt官方文档,网址如下:

http://doc.qt.io/qt-5/qtquick-statesanimations-topic.html

下面先列举几个QML动画元素,动画效果可“忘文生意”:

PropertyAnimation(常用)

AnchorAnimation

ColorAnimation

NumberAnimation

ParentAnimation

PathAnimation

RotationAnimation

Vector3dAnimation

SequentialAnimation

ParalleAnimation

PauseAnimation

SmoothedAnimation

SpringAnimation

PropertyAction(无动画效果)

ScriptAction

Behavior(设置默认动画)

常见的QML动画有三种表示方式,下面一一说明。

1、使用State和Transition

State改变属性值,Transition实现动画,例子如下:

  1. import QtQuick 2.2
  2. Item {
  3. id: container
  4. width: 360
  5. height: 360
  6. Rectangle {
  7. id: rect
  8. width: 100
  9. height: 100
  10. color: "blue"
  11. MouseArea {
  12. anchors.fill: parent
  13. // state属性值为空字符串时('')即默认状态
  14. onClicked: container.state == 'right' ? container.state = '' : container.state = 'right'
  15. }
  16. }
  17. states: State {
  18. name: "right"
  19. // rect水平移动
  20. PropertyChanges {
  21. target: rect
  22. x: 260
  23. }
  24. }
  25. transitions: Transition {
  26. // 数字(x坐标)动画,设置了easing的回弹效果和动画时间
  27. NumberAnimation {
  28. property: "x"
  29. easing.type: Easing.InOutBounce
  30. duration: 500
  31. }
  32. }
  33. }

2、使用Behavior

直接修改上面的例子,实现同样的动画效果,结果如下:

  1. import QtQuick 2.2
  2. Item {
  3. id: container
  4. width: 360
  5. height: 360
  6. Rectangle {
  7. id: rect
  8. width: 100
  9. height: 100
  10. color: "blue"
  11. // 看这里
  12. Behavior on x {
  13. NumberAnimation {
  14. easing.type: Easing.InOutBounce
  15. duration: 500
  16. }
  17. }
  18. MouseArea {
  19. anchors.fill: parent
  20. // 改变rect的x坐标
  21. onClicked: rect.x = (rect.x == 0 ? 260 : 0)
  22. }
  23. }
  24. }

3、其它

还是在上面例子的基础上修改以实现同样的效果,代码如下:

  1. import QtQuick 2.2
  2. Item {
  3. id: container
  4. width: 360
  5. height: 360
  6. Rectangle {
  7. id: rect
  8. width: 100
  9. height: 100
  10. color: "blue"
  11. // rect水平右移
  12. NumberAnimation on x {
  13. id: right
  14. running: false // false
  15. to: 260
  16. easing.type: Easing.InOutBounce
  17. duration: 500
  18. }
  19. // rect水平左移
  20. NumberAnimation on x {
  21. id: left
  22. running: false // false
  23. to: 0
  24. easing.type: Easing.OutInBounce // 换个easing动画效果
  25. duration: 500
  26. }
  27. MouseArea {
  28. anchors.fill: parent
  29. // 判断移动方向
  30. onClicked: rect.x == 0 ? right.running = true : left.running = true
  31. }
  32. }
  33. }

下面再来看一个有意思的例子,parallel和sequential动画:

  1. import QtQuick 2.2
  2. Item {
  3. id: container
  4. width: 360
  5. height: 360
  6. Rectangle {
  7. id: up
  8. width: 100
  9. height: 100
  10. color: "blue"
  11. // 并行动画,水平移动和颜色变化同时进行
  12. ParallelAnimation {
  13. id: parallel
  14. running: false
  15. PropertyAnimation {
  16. target: up
  17. property: "x"
  18. to: 260
  19. duration: 500
  20. }
  21. PropertyAnimation {
  22. target: up
  23. property: "color"
  24. to: "red"
  25. duration: 500
  26. }
  27. }
  28. MouseArea {
  29. anchors.fill: parent
  30. onClicked: parallel.running = true
  31. }
  32. }
  33. Rectangle {
  34. id: down
  35. width: 100
  36. height: 100
  37. color: "red"
  38. anchors.top: up.bottom
  39. // 串行动画,先进行水平移动,后进行颜色变化
  40. SequentialAnimation {
  41. id: sequential
  42. running: false
  43. PropertyAnimation {
  44. target: down
  45. property: "x"
  46. to: 260
  47. duration: 500
  48. }
  49. PropertyAnimation {
  50. target: down
  51. property: "color"
  52. to: "blue"
  53. duration: 500
  54. }
  55. }
  56. MouseArea {
  57. anchors.fill: parent
  58. onClicked: sequential.running = true
  59. }
  60. }
  61. }

关于QML动画,Qt官网文档也做了详细的介绍:

http://doc.qt.io/qt-5/qtquick-usecase-animations.html

http://doc.qt.io/qt-5/qtquick-statesanimations-animations.html

http://blog.csdn.net/ieearth/article/details/43986559

最新文章

  1. NAIPC-2016
  2. POJ 3624 Charm Bracelet(01背包)
  3. [shell]. 点的含义
  4. [Effective JavaScript 笔记]第53条:保持一致的约定
  5. bzoj 1588营业额统计(HNOI 2002)
  6. hadoop之JobTracker功能分析
  7. Codeforces Round #239 (Div. 2)
  8. 洛谷 P1063 能量项链
  9. MapReduce的数据流程、执行流程
  10. 【转】使用命令行方式创建和启动android模拟器
  11. [置顶] 数据持久层(DAO)常用功能–通用API的实现
  12. Jquery揭秘系列:谈谈bind,one,live,delegate,on事件及实现
  13. CloudXNS首次使用体验
  14. Spring实战——Profile
  15. Pseudo-devices On GNU/Linux
  16. 20175234 2018-2019-2 《Java程序设计》第六周学习总结
  17. Unix的哲学
  18. idea创建maven项目速度慢?别急,这有三种方案
  19. Ubuntu/Linux网络配置常用命令
  20. Baidu软件研发工程师笔试题整理

热门文章

  1. mac下装php5.6
  2. Extension of write anywhere file system layout
  3. R 语言基本操作(基本信息的查看、与本地文件系统交互、编译器版本升级)
  4. Neo4j集群环境建设
  5. Android于popWindow写弹出菜单
  6. 彩色图像--色彩空间 RGB系列
  7. HDU 1618 Oulipo KMP解决问题的方法
  8. OpenGL(二十三) 各向异性纹理过滤
  9. C++第三方日志库Pantheios
  10. 5亿英镑!Imagination同意出售给私募机构Canyon Bridge