NumberAnimation数字类动画

本文通过一个实例演示了如何在QML中使用NumberAnimation实现元素的移动、旋转和缩放动画。实验证明,多个NumberAnimation可以并行作用于同一对象的不同属性,无需使用ParallelAnimation组合。此外,还介绍了如何监听动画结束事件。

转自:https://blog.youkuaiyun.com/lvmengzou/article/details/86692112

import QtQuick 2.3
import QtQuick.Window 2.2

Window {
    id: win
    visible: true
    width: 400
    height: 400
    Rectangle{
        y: win.height/2
        id: source
        width: 100
        height: 100
        color: "red"
    }
    NumberAnimation {
        id: xChange
        target: source
        to: win.width - source.width
        property: "x"
        duration: 5000
        easing.type: Easing.InOutQuad
    }
    NumberAnimation {
        id: rotationChange
        target: source
        to: 360*5
        property: "rotation"
        duration: 5000
        easing.type: Easing.InOutQuad
    }
    NumberAnimation {
        id: radiusChange
        target: source
        to: 100
        property: "radius"
        duration: 5000
        easing.type: Easing.InOutQuad
    }

    MouseArea {
        anchors.fill: parent
        onClicked: {
            xChange.start()
            rotationChange.start()
            radiusChange.start()
        }
    }

    Connections  {
        target: xChange
        onStopped : {
            console.log("xChange animation stopped---------------------")
        }
    }

    Connections  {
        target: rotationChange
        onStopped : {
            console.log("rotationChange animation stopped---------------------")
        }
    }

    Connections  {
        target: radiusChange
        onStopped : {
            console.log("radiusChange animation stopped---------------------")
        }
    }
}

运行效果:(截图软件的问题,程序运行是没有残影的)
在这里插入图片描述
加的onStopped是为了看清楚这几个动画是串行的,还是并行的。
结果发现是并行的,也就是说几个NumberAnimation如果作用在同一个对象,且目标属性不同时,是可以并行,而不需要ParallelAnimation的,这一点只有做了试验才知道。
并行动画组ParallelAnimation,串行动画组:SequentialAnimation

参考:
ParallelAnimation组合动画
https://blog.youkuaiyun.com/xuancailinggan/article/details/50902736

// FocusButton.qml import QtQuick 2.15 import QtQuick.Controls 2.15 FocusScope { id: buttonRoot width: 200 * rootWindow.sizeFactor height: 60 * rootWindow.sizeFactor focus: true // === 暴露给外部的属性接口 === // 1. 按钮显示的文字 property string text: "按钮" // 2. 按钮的详细描述(可选) property string description: "" // 3. 按下Tab键后,焦点会移到的下一个组件 property Item nextItem: null // 4. 按钮被点击时发出的信号 signal clicked() // 5. 按钮的默认颜色和获得焦点时的颜色 property color color: "#3498db" property color focusedColor: "#2980b9" // 6. 是否在组件可见时自动获取焦点 property bool autoFocus: false // === 核心功能:键盘导航 === // 设置Tab键导航。如果指定了nextItem,则按Tab键会跳转到下一个组件 KeyNavigation.tab: nextItem // 处理Enter键按下事件,触发点击信号 Keys.onReturnPressed: clicked() Keys.onEnterPressed: clicked() // 组件创建完成或autoFocus属性变化时,自动请求焦点 Component.onCompleted: { if (autoFocus) { forceActiveFocus() } } // === 按钮的视觉呈现 === Rectangle { id: background anchors.fill: parent radius: 8 * rootWindow.sizeFactor // 颜色根据是否获得焦点变化,并有平滑过渡动画 color: buttonRoot.activeFocus ? buttonRoot.focusedColor : buttonRoot.color border { color: buttonRoot.activeFocus ? "#FF0000" : "transparent" width: buttonRoot.activeFocus ? 3 * rootWindow.sizeFactor : 0 } // 获得焦点时有一个轻微的放大效果 scale: buttonRoot.activeFocus ? 1.05 : 1.0 // 颜色和缩放变化时的动画,让交互更流畅 Behavior on color { ColorAnimation { duration: 100 } } Behavior on scale { NumberAnimation { duration: 100 } } // 按钮内部的内容布局 Column { anchors.centerIn: parent spacing: 4 * rootWindow.sizeFactor Text { anchors.horizontalCenter: parent.horizontalCenter text: buttonRoot.text font { pixelSize: 18 * rootWindow.sizeFactor bold: true family: "微软雅黑" } color: "white" } // 描述文字可选显示 Text { anchors.horizontalCenter: parent.horizontalCenter text: buttonRoot.description font.pixelSize: 12 * rootWindow.sizeFactor color: "white" opacity: 0.9 visible: buttonRoot.description !== "" // 如果没有描述,则不显示 } } } // === 鼠标交互 === MouseArea { anchors.fill: parent onClicked: { // 点击时先让按钮获取焦点,再触发点击事件 buttonRoot.forceActiveFocus() buttonRoot.clicked() } } } QQmlApplicationEngine failed to load component qrc:/main.qml: Type MainView unavailable qrc:/MainView.qml: Type FocusButton unavailable qrc:/FocusButton.qml: Invalid property assignment: number expected root@mxcavp-cm503:/data#
最新发布
10-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值