如何在QML中创建“向上扩展,然后向下”动画?

时间:2011-12-05 20:54:21

标签: qt qml

如何创建动画,其中项目的大小可以缩放,然后缩小到原始大小(从顶部/鸟瞰图中看出“弹跳球”)。到目前为止,我只想通过修改parent.x和parent.y来了解如何使用“x / y上的行为”创建单向动画

例如......

Rectangle {
id: container;
    width: 700
    height: 700
    function goForIt(parent) {
        parent.x = (Math.floor(Math.random()*600));
        parent.y = (Math.floor(Math.random()*600));
        parent.width += 100;
        parent.height += 100;
    }
    Image {
        id: head;
        source: "vlad.png";
        height: 80;
        width: 90;
        MouseArea {
            anchors.fill: parent
            onClicked: goForIt(parent);
        }
        Behavior on x {
            PropertyAnimation {
                target: head;
                properties: "x";
                duration: 1000;
            }
        }
        Behavior on y {
            PropertyAnimation {
                target: head;
                properties: "y";
                duration: 1000;
            }
        }
        Behavior on height {
            PropertyAnimation {
                target: head;
                properties: "height";
                duration: 1000;
            }
        }
        Behavior on width {
            PropertyAnimation {
                target: head;
                properties: "width";
                duration: 1000;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:7)

您可以创建所需的动画作为在onClicked处理程序中启动的SequenceAnimation。

import QtQuick 1.0

Rectangle {
    id: container;
    width: 700
    height: 700
    function goForIt(parent) {
        parent.x = (Math.floor(Math.random()*600));
        parent.y = (Math.floor(Math.random()*600));
        bounceAnimation.start();
    }

    Image {
        id: head;
        source: "vlad.png";
        x: 0
        y: 0
        height: 80;
        width: 90;
        MouseArea {
            anchors.fill: parent
            onClicked: goForIt(parent);
        }
        Behavior on x {
            PropertyAnimation {
                target: head;
                properties: "x";
                duration: 1000;
            }
        }
        Behavior on y {
            PropertyAnimation {
                target: head;
                properties: "y";
                duration: 1000;
            }
        }

        transform: Scale {
            id: scaleTransform
            property real scale: 1
            xScale: scale
            yScale: scale
        }

        SequentialAnimation {
            id: bounceAnimation
            loops: 1
            PropertyAnimation {
                target: scaleTransform
                properties: "scale"
                from: 1.0
                to: 2.0
                duration: 500
            }
            PropertyAnimation {
                target: scaleTransform
                properties: "scale"
                from: 2.0
                to: 1.0
                duration: 500
            }
        }
    }
}