我们要实现的效果:
需求分析:
涉及的 Animation:
- 从左向右的x坐标转换(X1)。
- 从下往上的y坐标转换(Y1)然后跟着一个从上往下的Y坐标转换(Y2)。
- 整个动画过程中360度旋转
先做背景:
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 480
height: 300
Item {
id: root
anchors.fill: parent
property int duration: 3000
// 天空渐变
Rectangle{
id: sky
width:parent.width
height: 200
gradient: Gradient{
GradientStop{
position: 0.0
color: "#0080FF"
}
GradientStop{
position: 1.0
color: "#66CCFF"
}
}
}
// 大地渐变
Rectangle{
id: ground
x: 0
width:parent.width
anchors.topMargin: -3
anchors.top:sky.bottom
anchors.bottom:root.bottom
gradient: Gradient{
GradientStop{
position: 0.0
color: "#00FF00"
}
GradientStop{
position: 1.0
color: "#00803F"
}
}
}
}
}
运行>>
接下来将图片显示在左下角:
Image {
id: ball
x:20
y:580
source: "qrc:/new/preImg/soccer_ball.png"
MouseArea{
anchors.fill: parent
//
}
}
接下来设置动画:
1)先用一个 SequentialAnimation 容器包含两个“向上移动”和“向下移动”的动画!
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 1300
height: 680
Item {
id: root
anchors.fill: parent
property int duration: 3000
// 天空渐变
Rectangle{
id: sky
width:parent.width
height: 500
gradient: Gradient{
GradientStop{
position: 0.0
color: "#0080FF"
}
GradientStop{
position: 1.0
color: "#66CCFF"
}
}
}
// 大地渐变
Rectangle{
id: ground
x: 0
width:parent.width
anchors.topMargin: -3
anchors.top:sky.bottom
anchors.bottom:root.bottom
gradient: Gradient{
GradientStop{
position: 0.0
color: "#00FF00"
}
GradientStop{
position: 1.0
color: "#00803F"
}
}
}
Image {
id: ball
x:20
y:580
source: "qrc:/new/preImg/soccer_ball.png"
MouseArea{
anchors.fill: parent
//
onClicked: {
ball.x = 20
ball.y = 580
anim.restart()
}
}
// 连续动画容器包含两个“向上移动”“向下移动”的动画
SequentialAnimation{
id:anim
// 向上移动的动画
NumberAnimation{
target: ball
properties: "y"
to:20
duration: root.duration * 0.4
}
// 向下移动的动画
NumberAnimation{
target: ball
properties: "y"
to:580
duration: root.duration * 0.6
}
}
}
}
}
OK,移动事件成功!
2)加入横向移动的动画
我们上一步将向上移动和向下移动的动画加在一个连续动画容器中的原因是因为我们希望足球可以一边向上/向下移动的同时还能向右移动,那么就需要将前面一个动画容器和一个向右移动的动画封装在一个平行动画容器里面!
Image {
id: ball
x:20
y:580
source: "qrc:/new/preImg/soccer_ball.png"
MouseArea{
anchors.fill: parent
//
onClicked: {
ball.x = 20
ball.y = 580
anim.restart()
}
}
ParallelAnimation{
id:anim
// 连续动画容器包含两个“向上移动”“向下移动”的动画
SequentialAnimation{
// 向上移动的动画
NumberAnimation{
target: ball
properties: "y"
to:20
duration: root.duration * 0.4
}
// 向下移动的动画
NumberAnimation{
target: ball
properties: "y"
to:580
duration: root.duration * 0.6
}
}
// 水平移动的动画
NumberAnimation{
target: ball
properties: "x"
to:1200
duration: root.duration
}
}
OK,现在足球能上下移动的同时并且能向右移动,但是足球是不滚动的!
3)加入足球滚动:
// 足球滚动
RotationAnimation{
target: ball
properties: "rotation"
// 一直滚动
to: 720
duration: root.duration
}
加入上面代码足球就能滚动了,这里就不截图了!
4)加入缓冲曲线
import QtQuick 2.9
import QtQuick.Window 2.2
Window {
visible: true
width: 1300
height: 680
Item {
id: root
anchors.fill: parent
property int duration: 3000
// 天空渐变
Rectangle{
id: sky
width:parent.width
height: 500
gradient: Gradient{
GradientStop{
position: 0.0
color: "#0080FF"
}
GradientStop{
position: 1.0
color: "#66CCFF"
}
}
}
// 大地渐变
Rectangle{
id: ground
x: 0
width:parent.width
anchors.topMargin: -3
anchors.top:sky.bottom
anchors.bottom:root.bottom
gradient: Gradient{
GradientStop{
position: 0.0
color: "#00FF00"
}
GradientStop{
position: 1.0
color: "#00803F"
}
}
}
Image {
id: ball
x:20
y:580
source: "qrc:/new/preImg/soccer_ball.png"
MouseArea{
anchors.fill: parent
//
onClicked: {
ball.x = 20
ball.y = 580
anim.restart()
}
}
ParallelAnimation{
id:anim
// 连续动画容器包含两个“向上移动”“向下移动”的动画
SequentialAnimation{
// 向上移动的动画
NumberAnimation{
target: ball
properties: "y"
to:20
duration: root.duration * 0.4
// OutCirc类型缓冲曲线
easing.type: Easing.OutCirc
}
// 向下移动的动画
NumberAnimation{
target: ball
properties: "y"
to:580
duration: root.duration * 0.6
// OutBounce类型缓冲曲线
easing.type: Easing.OutBounce
}
}
// 水平移动的动画
NumberAnimation{
target: ball
properties: "x"
to:1200
duration: root.duration
}
// 足球滚动
RotationAnimation{
target: ball
properties: "rotation"
to: 720
duration: root.duration * 1.1
}
}
}
}
}
以上是最终代码!
最终效果>>
应该说还是挺惊艳的!