P4-QML-states与transitions-动画效果制作

状态机

Window {
    visible: true              // 是否隐藏
    width: 640                 // 宽度
    height: 480                // 高度
    title: qsTr("Hello World") // 设置应用程序名称

    /* 状态机 */
    Rectangle {
        id: root
        x: 200
        width: 100; height: 100
        state: "normal"  // 选取的状态

        // 状态数组,每个状态用逗号隔开
        states: [
            State {
                name: "normal"
                PropertyChanges { target: root; color: "black" }
            },
            State {
                name: "red_color"
                PropertyChanges { target: root; color: "red" }
            },
            State {
                name: "blue_color"
                PropertyChanges { target: root; color: "blue" }
            }
        ]

        MouseArea{
            anchors.fill: parent     // 鼠标作用区域充满父类
            onPressed: {
                root.state = "red_color"
            }
            onReleased: {
                root.state = "blue_color"
            }
        }
    }
}

初始状态为黑色,当点击长方形的时候,变为红色,松开后变为蓝色
在这里插入图片描述

transitions:动画效果

Window {
    visible: true              // 是否隐藏
    width: 640                 // 宽度
    height: 480                // 高度
    title: qsTr("Hello World") // 设置应用程序名称

    /* transitions:动画效果,使用start开启动画 */
    Rectangle {
        id: flashingblob
        width: 75; height: 75
        color: "blue"
        opacity: 1.0


        MouseArea {
            anchors.fill: parent
            onClicked: {
                // 开启两个动画效果
                animateColor.start()   // 当鼠标点击长方形从蓝色变为绿色
                animateOpacity.start() // 当鼠标点击长方形,不透明度从0.1到1.0
            }
        }

        PropertyAnimation {
            id: animateColor;
            target: flashingblob;
            properties: "color";
            to: "green";
            duration: 1000  // 把颜色改为绿色,这个时间为1s
        }

        NumberAnimation {
            id: animateOpacity
            target: flashingblob
            properties: "opacity"
            from: 0.1
            to: 1.0
            duration: 2000
       }
    }

}

在这里插入图片描述

PropertyAnimation调用动画

Window {
    visible: true              // 是否隐藏
    width: 640                 // 宽度
    height: 480                // 高度
    title: qsTr("Hello World") // 设置应用程序名称

    /* 使用PropertyAnimation调用动画,使用on立即触发 */
    Rectangle {
        id: rect
        width: 100; height: 100
        color: "red"

        PropertyAnimation on x {
            to: 100
            duration: 2000  // 两秒钟长方形的x坐标从  0->100
        }
        PropertyAnimation on y {
            to: 100
            duration: 2000
        }
        PropertyAnimation on width {
            to: 300
            duration: 2000 // 两秒钟长方形的长变为200
        }
    }

}

在这里插入图片描述

SequentialAnimation顺序执行动画

在这里插入图片描述

transitions动画效果

Window {
    visible: true              // 是否隐藏
    width: 640                 // 宽度
    height: 480                // 高度
    title: qsTr("Hello World") // 设置应用程序名称

    /* 使用transitions可以把直接改变状态变为动画效果 */
    Rectangle {
        width: 75; height: 75
        id: button
        state: "YELLOW"

        MouseArea {
            anchors.fill: parent
            onPressed: button.state = "YELLOW"
            onReleased: button.state = "RED"
        }

        states: [
            State {
                name: "YELLOW"
                PropertyChanges { target: button; color: "yellow"}
            },
            State {
                name: "RED"
                PropertyChanges { target: button; color: "red"}
            }
        ]

        transitions: [
            // 两秒钟实现控件从黄色变为红色
            Transition {
                from: "YELLOW"
                to: "RED"
                ColorAnimation { target: button; duration: 2000}
            },
            Transition {
                from: "RED"
                to: "YELLOW"
                ColorAnimation { target: button; duration: 2000}
            }
        ]
    }

}

鼠标点击长方形的时候,从 YELLOW 状态变为 RED 状态,松开后从 RED 状态变为 YELLOW 状态,这两个过程都经历2s
在这里插入图片描述

全部代码

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2

Window {
    visible: true              // 是否隐藏
    width: 640                 // 宽度
    height: 480                // 高度
    title: qsTr("Hello World") // 设置应用程序名称

    /* 状态机 */
    Rectangle {
        id: root
        x: 200
        width: 100; height: 100
        state: "normal"  // 选取的状态

        // 状态数组,每个状态用逗号隔开
        states: [
            State {
                name: "normal"
                PropertyChanges { target: root; color: "black" }
            },
            State {
                name: "red_color"
                PropertyChanges { target: root; color: "red" }
            },
            State {
                name: "blue_color"
                PropertyChanges { target: root; color: "blue" }
            }
        ]

        MouseArea{
            anchors.fill: parent     // 鼠标作用区域充满父类
            onPressed: {
                root.state = "red_color"
            }
            onReleased: {
                root.state = "blue_color"
            }
        }

    }

    /* transitions:动画效果,使用start开启动画 */
    Rectangle {
        id: flashingblob
        width: 75; height: 75
        color: "blue"
        opacity: 1.0


        MouseArea {
            anchors.fill: parent
            onClicked: {
                // 开启两个动画效果
                animateColor.start()   // 当鼠标点击长方形从蓝色变为绿色
                animateOpacity.start() // 当鼠标点击长方形,不透明度从0.1到1.0
            }
        }

        PropertyAnimation {
            id: animateColor;
            target: flashingblob;
            properties: "color";
            to: "green";
            duration: 1000  // 把颜色改为绿色,这个时间为1s
        }

        NumberAnimation {
            id: animateOpacity
            target: flashingblob
            properties: "opacity"
            from: 0.1
            to: 1.0
            duration: 2000
       }
    }

    /* 使用PropertyAnimation调用动画,使用on立即触发 */
    Rectangle {
        id: rect
        width: 100; height: 100
        color: "red"

        PropertyAnimation on x {
            to: 100
            duration: 2000  // 两秒钟长方形的x坐标从  0->100
        }
        PropertyAnimation on y {
            to: 100
            duration: 2000
        }
        PropertyAnimation on width {
            to: 300
            duration: 2000 // 两秒钟长方形的长变为200
        }
    }

    /* SequentialAnimation顺序执行动画 */
    Rectangle {
        width: 100; height: 100
        color: "red"

        // 控件先变黄再变蓝
        SequentialAnimation on color {
            ColorAnimation { to: "yellow"; duration: 1000 }
            ColorAnimation { to: "blue"; duration: 1000 }
        }
    }

    /* 使用transitions可以把直接动画变为动画效果 */
    Rectangle {
        width: 75; height: 75
        id: button
        state: "YELLOW"

        MouseArea {
            anchors.fill: parent
            onPressed: button.state = "YELLOW"
            onReleased: button.state = "RED"
        }

        states: [
            State {
                name: "YELLOW"
                PropertyChanges { target: button; color: "yellow"}
            },
            State {
                name: "RED"
                PropertyChanges { target: button; color: "red"}
            }
        ]

        transitions: [
            // 两秒钟实现控件从黄色变为红色
            Transition {
                from: "YELLOW"
                to: "RED"
                ColorAnimation { target: button; duration: 2000}
            },
            Transition {
                from: "RED"
                to: "YELLOW"
                ColorAnimation { target: button; duration: 2000}
            }
        ]
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QML中可以通过使用动画来实现各种特效,包括进场动画。其中,百叶窗特效是一种常见的进场动画,可以让界面元素以一定的节奏和方式出现。下面是一个简单的实现示例: ```qml import QtQuick 2.0 Rectangle { width: 300 height: 300 color: "white" Repeater { model: 5 Image { id: image source: "image.png" width: parent.width / 5 height: parent.height x: index * width clip: true transform: Scale { id: scale origin.x: width / 2 origin.y: height / 2 xScale: 1 yScale: 0 } Behavior on transform { PropertyAnimation { duration: 500 easing.type: Easing.InOutQuad } } } } Component.onCompleted: { for (var i = 0; i < repeater.count; i++) { var image = repeater.itemAt(i); image.scale.y = 1; } } } ``` 在这个示例中,我们使用了一个Repeater来创建了5个相同的Image元素,每个元素的宽度都是父元素宽度的1/5。我们将这些元素放置在一起,然后通过使用clip属性来将它们裁剪成相同大小。接着,我们为每个元素添加了一个缩放变换,初始时y轴的比例为0,这样它们就会“收缩”起来。最后,在组件完成时,我们将每个元素的缩放比例y设置为1,这样它们就会“展开”出现。 在这个示例中,我们使用了PropertyAnimation来控制变换的动画效果。该动画持续500ms,并且使用了Easing.InOutQuad缓动函数,使它看起来更加平滑。你可以根据需要对这些参数进行调整,以达到更好的效果。 总体来说,百叶窗特效是一种简单而又实用的进场动画,可以轻松地让你的应用程序变得更加生动有趣。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值