QML学习笔记(六十四)动画相关:State状态、Transition过渡和Gradient渐变

前言

今天学习到一个有关状态的概念,叫State。有趣的事,这个东西在QWidget那套样式表的格式中也有应用,比方说某个控件存在不同的切换状态,我们就可以使用State的方式来写样式表,然后C++代码中进行状态切换。有点扯远了,接下来就记录一下QML中有关State的使用,顺便应用一下Transition过渡

一、State状态

没什么好说的,直接看代码。

完整代码:

cpp 复制代码
import QtQuick 2.15
import QtQuick.Window 2.15

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("StateAndTransition")

    Rectangle{
        id: containerRectId
        anchors.fill: parent

        //Sky
        Rectangle{
            id:skyId
            width: parent.width
            height:200
            color: "blue"
        }

        Rectangle{
            id: groundId
            anchors.top: skyId.bottom
            anchors.bottom: parent.bottom
            width: parent.width
            color:"lime"
        }

        Rectangle{
            id: sunId
            x: parent.width - width - 100
            y:50
            width: 100
            height: 100
            color: "yellow"
            radius: 600
        }

        state: "spring"

        states: [
            //Spring
            State{

                name: "spring"
                PropertyChanges {
                    target: skyId
                    color: "deepskyblue"
                }
                PropertyChanges {
                    target: groundId
                    color: "lime"
                }
                PropertyChanges {
                    target: sunId
                    color:"lightyellow"
                }
            },

            //Summer
            State{

                name:"summer"
                PropertyChanges {
                    target: skyId
                    color:"lightblue"
                }
                PropertyChanges {
                    target:groundId
                    color:"darkkhaki"
                }
                PropertyChanges {
                    target: sunId
                    color:"yellow"
                }
            }
        ]

        transitions: [
            Transition {
                from: "summer"
                to: "spring"

                ColorAnimation {
                    duration: 500
                }
                NumberAnimation{
                    property: "opacity"
                    duration: 500
                }
            },
            Transition {
                from: "spring"
                to: "summer"

                ColorAnimation {
                    duration: 500
                }
                NumberAnimation{
                    property: "opacity"
                    duration: 500
                }
            }
        ]

        MouseArea{
            anchors.fill: parent
            onClicked:function(){
                containerRectId.state = (containerRectId.state === "spring" ? "summer" : "spring")
            }
        }
    }
}

states是一个状态数组,里面添加了两种状态,分别是spring春天和summer夏天。里面会包含几个PropertyChanges 属性变更,然后指定target对象和属性,这里是color颜色。

最后,我们通过MouseArea的点击,还有三元运算符来进行状态的切换。

代码其实比较好理解,说完下面的过渡,再贴上效果图。

二、Transition过渡

以下是过渡效果的代码:

cpp 复制代码
transitions: [
            Transition {
                from: "summer"
                to: "spring"

                ColorAnimation {
                    duration: 500
                }
                NumberAnimation{
                    property: "opacity"
                    duration: 500
                }
            },
            Transition {
                from: "spring"
                to: "summer"

                ColorAnimation {
                    duration: 500
                }
                NumberAnimation{
                    property: "opacity"
                    duration: 500
                }
            }

它也是数组和单个过渡元素的设置,from和to则不是实际的组件元素,而是状态名称,这一点还是有点不同的。这里终于添加了有关动画的效果,分别是颜色动画和数值动画。最后看一下效果,我觉得还挺舒服的。

在我跟学的视频中,还有树木图片的状态切换。但我没有图片资源,我也懒得搞了。

三、带渐变的状态

使用这种方式,可以设置颜色渐变:

cpp 复制代码
            gradient: Gradient{
                GradientStop{
                    id: groundStartColorId
                    position: 0.0
                    color: "lime"
                }
                GradientStop{
                    id: groundEndColorId
                    position: 1.0
                    color: "lightyellow"
                }
            }

0.0到1.0,意味着y轴上从0到1范围,也就是全部范围的颜色渐变。也可以在中间多插入几个渐变点进行控制。

下面是完整代码:

cpp 复制代码
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Shapes 1.10

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("StateAndTransition")

    Rectangle{
        id: containerRectId
        anchors.fill: parent

        //Sky
        Rectangle{
            id:skyId
            width: parent.width
            height:200
            // color: "blue"
            gradient: Gradient{
                GradientStop{
                    id: skyStartColorId
                    position: 0.0
                    color: "blue"
                }
                GradientStop{
                    id: skyEndColorId
                    position: 1.0
                    color: "#66CCFF"
                }
            }
        }

        Rectangle{
            id: groundId
            anchors.top: skyId.bottom
            anchors.bottom: parent.bottom
            width: parent.width
            // color:"lime"
            gradient: Gradient{
                GradientStop{
                    id: groundStartColorId
                    position: 0.0
                    color: "lime"
                }
                GradientStop{
                    id: groundEndColorId
                    position: 1.0
                    color: "lightyellow"
                }
            }
        }

        Rectangle{
            id: sunId
            x: parent.width - width - 100
            y:50
            width: 100
            height: 100
            color: "yellow"
            radius: 600
        }

        state: "spring"

        states: [
            //Spring
            State{

                name: "spring"
                // PropertyChanges {
                //     target: skyId
                //     color: "deepskyblue"
                // }
                PropertyChanges {
                    target: skyStartColorId
                    color: "deepskyblue"
                }
                PropertyChanges {
                    target: skyEndColorId
                    color: "#AACCFF"
                }
                // PropertyChanges {
                //     target: groundId
                //     color: "lime"
                // }
                PropertyChanges {
                    target: groundStartColorId
                    color: "lime"
                }
                PropertyChanges {
                    target: groundEndColorId
                    color: "#66CCFF"
                }
                PropertyChanges {
                    target: sunId
                    color:"lightyellow"
                }
            },

            //Summer
            State{

                name:"summer"
                // PropertyChanges {
                //     target: skyId
                //     color:"lightblue"
                // }
                PropertyChanges {
                    target: skyStartColorId
                    color: "lightblue"
                }
                PropertyChanges {
                    target: skyEndColorId
                    color: "#EECCFF"
                }
                // PropertyChanges {
                //     target:groundId
                //     color:"darkkhaki"
                // }
                PropertyChanges {
                    target: groundStartColorId
                    color: "lime"
                }
                PropertyChanges {
                    target: groundEndColorId
                    color: "yellow"
                }
                PropertyChanges {
                    target: sunId
                    color:"yellow"
                }
            }
        ]

        transitions: [
            Transition {
            from: "*"
            to: "*"

            ColorAnimation {
                duration: 500
            }
            NumberAnimation{
                property: "opacity"
                duration: 500
            }
        }
        ]

        MouseArea{
            anchors.fill: parent
            onClicked:function(){
                containerRectId.state = (containerRectId.state === "spring" ? "summer" : "spring")
            }
        }
    }
}

运行效果:

四、总结

State状态、Transition过渡和Gradient渐变,无疑更加丰富了QML中的动态效果。其实状态和渐变作为基础ui效果,在qwidget中也有应用,但配合上过渡的动画效果,给人的体验感还是挺不一样的。希望后续实际开发中也有机会使用到这个东西。

相关推荐
蜡笔小谢1 小时前
DeepSeek Harness Java 实战笔记(五)|插件、MCP 与 Skill
java·笔记·ai·agent·deepseek·harness
FRing032 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
wefg12 小时前
【项目】C++ 基于 Vibe Coding 的 OJ 平台
开发语言·c++
半扇排骨5 小时前
特别篇:在线 XSS 靶场 haozi 分享与通关笔记
学习·安全·靶场·xss
琳少_6 小时前
java程序设计笔记
java·开发语言·数据库·笔记
Helix2508 小时前
JDK高频报错速查手册:javac不是内部命令、内存溢出、版本不兼容等12类问题解决方法
java·开发语言·内存溢出·java异常处理·jdk版本兼容·jdk报错·javac报错
郝学胜-神的一滴9 小时前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
CoderYanger9 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
摇滚侠10 小时前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记
虎符饼干11 小时前
用WebHook把AI文章推送到自建官网,接收端需要具备哪些处理与发布能力?
笔记