Qt之Qml第二章states与transitions动画效果制作

5、QML-states与transitions动画效果制作

5.1 states 设置颜色状态 状态机的使用

用法1:通过鼠标点击和释放事件,改变矩形框颜色状态。
示例代码:

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("MY QML")
    color: "white"

    Rectangle{
        id:root
        width: 100
        height: 100
        color: "black"  //生成一个黑色的100*100矩形框
        state: "normal" //设置默认状态
        states: [       // 注意这里这个符号不要写错[]
            State {
                name: "normal"
                PropertyChanges {  target: root; color: "black"}   //还可以改变宽度和高度状态 width:200 height:200
            },
            State {
                name: "red_color"
                PropertyChanges {  target: root; color: "red"}//通过PropertyChanges改变当前控件颜色
            },
            State {
                name: "blue_color"
                PropertyChanges {
                    target: root
                    color: "blue"
                }
            }
        ]
    }

    MouseArea{ // 鼠标点击事件
        anchors.fill: parent
        onPressed: {  //鼠标点击改变颜色
            root.state = "red_color" //设置状态
        }
        onReleased: { //鼠标松开改变颜色
            root.state = "blue_color"
        }
    }

用法2:设置点击矩形框颜色从蓝色渐变为绿色,时间为两秒 手动启动状态改变
示例代码:

Rectangle{
        id: flashingblod
        width: 100
        height: 100
        color: "blue"
        opacity: 1.0     //设置透明度

        MouseArea{  //鼠标的捕获事件
            anchors.fill: parent
            onClicked: {
                animateColor.start()   //开启了两个动画效果
                animateOpacity.start()
            }
        }
        PropertyAnimation{   //动画效果的属性
            id:animateColor
            target: flashingblod
            properties: "color"
            to:"green"      //设置颜色
            duration: 1000   //设置持续时间
        }

        NumberAnimation{   //动画效果的数值
            id:animateOpacity
            target: flashingblod
            properties: "opacity"  //透明度从0.1渐变为1.0持续时间为两秒
            from: 0.1
            to:1.0
            duration: 2000  //持续时间两秒
        }
    }

用法3:自动启动状态改变可以通过on属性来使属性立即修改
示例代码:

Rectangle{
        id:rect
        width: 100
        height: 100
        color: "red"
        //通过on属性直接使当前控件属性立即修改
        PropertyAnimation on x{ //x和y就是修改当前控件的位置
            to: 100
            duration: 1000 // 修改持续时间
        }
        PropertyAnimation on y{
            to: 100
            duration: 1000 // 修改持续时间
        }
    }

用法4:按顺序执行我们需要改变的动画
示例代码:

Rectangle{
        width: 100
        height: 100
        color: "red"

        SequentialAnimation on color { //SequentialAnimation按顺序来播放我们需要执行的动画

            ColorAnimation {   //ColorAnimation来控制颜色
                //from: "white"
                to: "yellow"
                duration: 1000
            }

            ColorAnimation {
                //from: "white"
                to: "blue"
                duration: 1000
            }
        }
    }

5.2 transitions

通过transitions做一个动画效果,加上他之后颜色不会直接变化,而是有一秒钟的恢复效果,产生一种渐变效果
示例代码:

Rectangle{
        width: 75;
        height: 75;
        id: button
        state: "RELEASED"

        MouseArea{ //鼠标事件
            anchors.fill: parent
            onPressed: button.state = "PRESSED"  //通过点击释放切换状态
            onReleased: button.state = "RELEASED"
        }
        states: [
            State {
                name: "PRESSED"
                PropertyChanges {
                    target: button
                    color: "lightblue"
                }
            },
            State {
                name: "RELEASED"
                PropertyChanges {
                    target: button
                    color: "lightsteelblue"
                }
            }
        ]
        transitions: [   //状态过度动画
            Transition {
                from: "PRESSED" //表示两个状态的切换
                to: "RELEASED"  //从某一个状态切换到某一个状态他的效果是什么
                ColorAnimation {
                    target: button
                    duration: 100
                }
            },
            Transition {
                from: "RELEASED"
                to: "PRESSED"
                ColorAnimation {
                    target: button
                    duration: 100
                }
            }
        ]
    }

5.3 Behavior 预先做一效果在值修改后就会触发

加入了弧度变化,使一个方形变成圆形,并使用Behavior做一个预处理,值发生改变后,做出一个动画效果
示例代码:

Rectangle{
            width: 75;
            height: 75;
            radius: width //做一个弧度形成一个圆
            id: ball
            color: "salmon"
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    ball.x += 50;
                    ball.y += 50;
                }
            }
            //Behavior 预先做一效果在值修改后就会触发
            Behavior on x{
                NumberAnimation{
                    id:bouncebehavior
                    easing{
                        type: Easing.OutElastic
                        amplitude:  1.0
                        period: 0.5
                    }
                }
            }
            Behavior on y{
                animation: bouncebehavior
                }
            Behavior{

                ColorAnimation {
                    target: ball
                    duration: 100
                }
            }
    }
            animation: bouncebehavior
                }
            Behavior{

                ColorAnimation {
                    target: ball
                    duration: 100
                }
            }
    }
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值