Animation(五-足球动态移动)

我们要实现的效果:

需求分析:

涉及的 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
                }
            }
        }
    }
}

以上是最终代码!

最终效果>>

应该说还是挺惊艳的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柔弱胜刚强.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值