QML动画概述(几十篇相关博客)

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实现动画,例子如下:

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

2、使用Behavior

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

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

3、其它

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

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

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

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

关于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

原文地址:https://www.cnblogs.com/findumars/p/6090797.html