前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【QML】简单动画实现

【QML】简单动画实现

作者头像
半生瓜的blog
发布2023-05-13 13:58:29
4460
发布2023-05-13 13:58:29
举报
文章被收录于专栏:半生瓜のblog半生瓜のblog

QML动画

**示例1:**动画作为属性值的来源

代码语言:javascript
复制
import QtQuick 2.0

//动画作为属性值的来源
//语法: 动画on属性
//easing属性来实现缓和曲线
Rectangle{
    width: 100
    height: 100
    color: "red"
    PropertyAnimation on x{
        to:50
        duration: 1000
        loops: Animation.Infinite //无限循环
        easing.type: Easing.OutBounce//反弹效果
    }
    PropertyAnimation on y{
        to:50
        duration: 1000
        loops: Animation.Infinite
        easing.type: Easing.OutBounce
    }
}

**示例2:**行为动画

代码语言:javascript
复制
import QtQuick 2.0

//行为动画
//Behavior为一个属性来指定默认的动画
Item{
    width: 100
    height: 100

    Rectangle{
        id:rect1
        width: 100
        height: 100
        color:"green"

        Behavior on x{
            PropertyAnimation{duration: 500}
        }
        Behavior on y{
            PropertyAnimation{duration: 500}
        }
    }

    MouseArea{
        anchors.fill:parent
        onClicked: {
            rect1.x = mouse.x
            rect1.y = mouse.y

        }

    }
}

**示例3:**信号处理器中的动画

代码语言:javascript
复制
import QtQuick 2.0

//行为动画
//Behavior为一个属性来指定默认的动画

Rectangle{
        id:rect1
        width: 100
        height: 100
        color:"green"

        MouseArea{
            anchors.fill:parent
            onClicked:PropertyAnimation{
                target: rect1
                properties: "x,y"
                to:50
                duration:2000
            }
        }
}

**示例4:**独立动画

代码语言:javascript
复制
import QtQuick 2.0

//独立动画(动画作为普通的QML对象来创建)
Rectangle{
    id:rect1
    width: 100
    height: 100
    color:"green"
    PropertyAnimation{
        id:rect1_animation
        target:rect1
        properties: "x,y"
        duration: 1000

    }
    MouseArea{
        anchors.fill: parent
        onClicked: {
            rect1_animation.to = 50
            rect1_animation.running = true
        }
    }
}

**示例5:**状态切换

代码语言:javascript
复制
import QtQuick 2.0

//状态切换
Rectangle{
    id:rect1
    width: 100
    height: 100
    color:"green"

    MouseArea{
        anchors.fill: parent
        onClicked: {
            rect1.state = "moved"
        }
        onReleased: {
            rect1.state = "moved_2"
        }
    }

    //状态列表
    //states:[]
    states:[
        State {
            name:"moved"
            PropertyChanges {
                target: rect1
                x:50
                y:50
            }
        },
        State {
            name:"moved_2"
            PropertyChanges {
                target: rect1
                x:20
                y:20
            }
           }
    ]

    transitions: Transition {
            PropertyAnimation{
                properties:"x,y"
                duration: 1000

            }
    }
}

**示例6:**动画元素

代码语言:javascript
复制
  import QtQuick 2.0

//动画元素
Rectangle{
    width: 200
    height: 200


    //颜色
    ColorAnimation on color{
         from:"yellow"
         to:"red"
         duration: 2000
    }

    //旋转
    RotationAnimation on rotation {
        to:90
        duration: 3000
        direction: RotationAnimation.Clockwise//顺时针方向
    }
}

**示例7:**组合动画

代码语言:javascript
复制
import QtQuick 2.0

//组合动画
Rectangle{
    width: 100
    height: 100

    Image{
        source:"picture.jpg"
        anchors.horizontalCenter: parent.horizontalCenter
        y:0
        SequentialAnimation on y{
            loops:Animation.Infinite
            NumberAnimation{
                to:250
                easing.type: Easing.OutBounce
                duration: 1000
            }

            PauseAnimation {
                duration: 1000
            }

            NumberAnimation {
                to:0
                easing.type: Easing.OutBounce
                duration: 1000
            }
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-03-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • QML动画
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档