qt - 如何在 QML 中创建 'scale up, then down' 动画?

标签 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;
            }
        }
    }
}

最佳答案

您可以将所需的动画创建为在 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
            }
        }
    }
}

关于qt - 如何在 QML 中创建 'scale up, then down' 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8391733/

相关文章:

c++ - qt 我如何将 cFilename 转换为 QString 类型

c++ - QSqlDriver::close 使应用程序崩溃

android - 如何让 QVideoProbe 工作?

javascript - QML:将 Canvas 作为 ShaderEffectSource 传递

qt - 为什么在 QML 中查找窗口大小的代码不起作用?

c++ - QNetworkAccessManager : How to change post data at createRequest function

c++ - Qt-如何在不显式实现线程的情况下同时运行两个槽

qt - 如何在 Qt 中向上移动选定的行

c++ - 从 QML 调用多个 C++ 函数且第一个函数包含连接调用时,QML 在触发回调之前继续

c++ - QML OpenGL 插件不以 60Hz 刷新