Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one create a 'scale up, then down' animation in QML?

Tags:

qt

qml

How does one create an animation where an item scales up in size, then scales down to its original size (think of a "bouncing ball" from a top/bird's eye view). So far I have only figured out how to create a one-way animation using "Behavior on x/y" by modifying the parent.x and parent.y

For example...

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;
            }
        }
    }
}
like image 858
nipponese Avatar asked Dec 05 '11 20:12

nipponese


1 Answers

You can create the animation you want as a SequenceAnimation which is started in the onClicked handler.

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
            }
        }
    }
}
like image 106
blakharaz Avatar answered Nov 11 '22 18:11

blakharaz