Qml学习——动画

最近在学习Qml,但对Qml的各种用法都不太熟悉,总是会搞忘,所以写几篇文章对学习过程中的遇到的东西做一个记录。
学习参考视频:https://www.bilibili.com/video/BV1Ay4y1W7xd?p=1&vd_source=0b527ff208c63f0b1150450fd7023fd8
其他文章:
Qml学习——动态加载控件
Qml学习——控件状态
Qml学习——使用JavaScript
Qml学习——动画
Qml学习——鼠标事件处理MouseArea
Qml学习——布局
Qml学习——基本控件


1 Behavior

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    visible: true; width: 200; height: 120

    Rectangle {
        width: 50; height: 40
        color: 'red'

        Behavior on width {
            NumberAnimation {
                duration: 500
            }
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                if (parent.width == 50) {
                    parent.width = 100
                } else {
                    parent.width = 50
                }
            }
        }
    }
}

请添加图片描述

2 Animation

2.1 动画组

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    visible: true; width: 200; height: 120

    Rectangle {
        id: rect
        width: 50; height: 40
        color: 'red'

        SequentialAnimation {
            loops: Animation.Infinite
            running: true
            NumberAnimation {
                property: 'width'
                target: rect
                to: 100
                duration: 500
            }

            NumberAnimation {
                property: 'width'
                target: rect
                to: 70
                duration: 300
            }

            NumberAnimation {
                property: 'width'
                target: rect
                to: 150
                duration: 500
            }

            NumberAnimation {
                property: 'width'
                target: rect
                to: 50
                duration: 500
            }
        }
    }
}

请添加图片描述

2.1 并行动画

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    visible: true; width: 200; height: 120

    Rectangle {
        id: rect
        width: 50; height: 50
        color: 'red'

        SequentialAnimation {
            loops: Animation.Infinite
            running: true

            ParallelAnimation {
                running: true
                NumberAnimation {
                    property: 'width'
                    target: rect
                    to: 100
                    duration: 500
                }

                NumberAnimation {
                    property: 'height'
                    target: rect
                    to: 100
                    duration: 500
                }
            }

            ParallelAnimation {
                running: true
                NumberAnimation {
                    property: 'width'
                    target: rect
                    to: 50
                    duration: 500
                }

                NumberAnimation {
                    property: 'height'
                    target: rect
                    to: 50
                    duration: 500
                }
            }
        }
    }
}

请添加图片描述

3 State切换的过渡动画(Transition)

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Window {
    visible: true; width: 200; height: 120

    Rectangle {
        id: rect
        width: 50; height: 40
        color: 'red'

        transitions: [
            Transition {
                NumberAnimation {
                    property: 'width'
                    duration: 500
                }
            }
        ]


        states: State {
            name: 'demo'
            PropertyChanges {
                target: rect
                width: 100

            }
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                rect.state = rect.state ? '' : 'demo'
            }
        }
    }
}

请添加图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值