在QML中,可以把多个动画组合成一个单一的动画。
- ParallelAnimation 动画同时进行(并行)
- SequentialAnimation 动画按照顺序执行(顺序执行)
注意:将动画分组为“顺序动画”或“并行动画”后,无法单独启动和停止动画;顺序动画或并行动画必须作为一个组启动和停止。
使用时需要使用running开启
开启时,变换圆角、颜色和旋转角度
Rectangle{id:rect1width: 200;height: 200x:150;y:150color: "lightBlue";radius: 10//创建一个并行的动画ParallelAnimation{running: true //开启并行动画NumberAnimation { //修改圆角动画target: rect1properties: "radius"loops: Animation.Infiniteduration:2000from: 10to:50}ColorAnimation { //修改颜色动画target: rect1properties: "color"loops: Animation.Infiniteduration:2000from:"lightBlue"to:"red"}RotationAnimation { //修改角度动画target: rect1//properties: "rect1.rotation"loops: Animation.Infiniteduration:2000from:0to:360}}}
需要使用running开启
开启时每过两秒变换一种颜色
Rectangle{id:rect1width: 200;height: 200x:150;y:150color: "lightBlue";radius: 10SequentialAnimation{running: trueColorAnimation{target: rect1properties: "color"from: "white"to: "black"duration: 2000}ColorAnimation{target: rect1properties: "color"from: "black"to: "red"duration: 2000}ColorAnimation{target: rect1properties: "color"from: "red"to: "green"duration: 2000}ColorAnimation{target: rect1properties: "color"from: "green"to: "lightBlue"duration: 2000}}}
SequentialAnimation和渐变的使用
实现使用彩色动画将天空从白天淡化到黑夜
Rectangle{id:rect1width: 400;height: 600x:150;y:0color: "lightBlue";radius: 10gradient: Gradient {GradientStop {position: 0.0SequentialAnimation on color {loops: Animation.InfiniteColorAnimation { from: "#14148c"; to: "#0E1533"; duration: 5000 }ColorAnimation { from: "#0E1533"; to: "#14148c"; duration: 5000 }}}GradientStop {position: 1.0SequentialAnimation on color {loops: Animation.InfiniteColorAnimation { from: "#14aaff"; to: "#437284"; duration: 5000 }ColorAnimation { from: "#437284"; to: "#14aaff"; duration: 5000 }}}}}