全部的动画组件都是继承于Animation,因此咱们先来介绍下它们的父组件Animation.dom
1.Animation
须要注意的是若是直接使用Animation将致使错误。它的存在只是为了给子组件提供一组公共属性和方法,这些属性和方法可在继承自它的全部其余动画类型中使用。
Propertieside
Signals函数
Methodsoop
测试alwaysRunToEnd属性
因为Animation是虚组件,不能直接使用,因此咱们以它的子组件PropertyAnimation为例:测试
Column { Button { text: "启动动画" onClicked: { animation.start() } } Button { text: "中止动画" onClicked: { animation.stop() } } Rectangle { id: rect width: 20 height: 20 color: "#000000" PropertyAnimation { id:animation; loops: Animation.Infinite alwaysRunToEnd: true target: rect property: "x" from: 0; to: 100; duration: 500 } } }
当咱们启动动画后,点击中止动画按钮时,此时必须到了终点才中止.
2.PropertyAnimation - 改变property属性时能够产生的动画
PropertiesAniation用来给target目标下的property属性更改分配一个动画的组件.它的父组件是Animation
Properties动画
示例以下所示:rest
Column { Button { text: "启动动画" onClicked: { animation.start() } } Button { text: "中止动画" onClicked: { animation.stop() } } Rectangle { id: rect1 width: 20 height: 20 color: "#0000FF" } Rectangle { id: rect2 width: 20 height: 20 color: "#FF0000" } } PropertyAnimation { id:animation; loops: Animation.Infinite alwaysRunToEnd: true targets: [rect1,rect2] property: "x" easing.type: Easing.InQuad from: 0; to: 100; duration: 2000 }
当咱们点击启动按钮后,就会启动rect1和rect2的x属性从坐标0到100进行移动的动画.对象
假如一个动画只针对一个目标对象的一个属性,那么咱们可使用 XXXAnimation on Property { ... }写法,这样就不须要指定target和property了,更加便捷:继承
Column { Button { text: "启动动画" onClicked: { animation.start() } } Button { text: "中止动画" onClicked: { animation.stop() } } Rectangle { id: rect1 width: 20 height: 20 color: "#00FF00" PropertyAnimation on x { id:animation; loops: Animation.Infinite alwaysRunToEnd: true easing.type: Easing.InQuad running: false from: 0; to: 100; duration: 2000 } } }
须要注意的是该写法的running是默认为true,因此若是不想当即启动动画,则须要初始化为false.get
NumberAnimation和ColorAnimation
它们都是继承于PropertyAnimation组件,而后重写了from和to属性,使用方法其实和PropertyAnimation同样.
示例以下所示:
MouseArea { id: area anchors.fill: parent onPressed: { colorAnimation.to = Qt.rgba(Math.random(),Math.random(),Math.random(),1) // 从新赋一个颜色 numberAnimation.restart() } } Rectangle { id: rect anchors.centerIn: parent color: "#00FF00" width: 20 height: 20 NumberAnimation on width{ id: numberAnimation duration: 300 to: rect.width*1.3 running: false } ColorAnimation on color{ id: colorAnimation duration: 300 to: Qt.rgba(Math.random(),Math.random(),Math.random(),1) running: numberAnimation.running } }
当咱们点击一次,Rectangle的宽度将会变宽一次,而且颜色也会跟着改变.