QML 组件抖动效果实现

该教程介绍了如何在QML中创建对象的抖动效果,包括水平、垂直和旋转抖动。关键代码涉及PropertyAnimation组件,用于定义动画效果,通过改变对象的x、y或rotation属性实现抖动,并在动画结束后恢复原位。示例中,抖动效果与ListView和AbstractButton结合,点击按钮触发不同类型的抖动动画。
摘要由CSDN通过智能技术生成

《QML基础教程》总目录


一、简述

如图所示
在这里插入图片描述


二、关键代码分析

	//被抖动的对象  不能使用锚定位方式布局,xy抖动会无效。应使用xy手动定位位置
    Rectangle{
        id: rect
        x: parent.width / 2 - rect.width / 2
    }
	//属性动画,对目标rect做抖动动画,水平抖动操作x属性,垂直操作y,旋转操作rotation
    PropertyAnimation {
        id: shakeAnimation
        property int originVal //记录抖动前的原始值
        target: rect
        property: targetProperty // "x""y""rotation"
        from: originVal - model.offset //动画开始属性的值  如果是旋转抖动,可以是-10度
        to: originVal + model.offset //动画结束时属性的值 如果是旋转抖动,抖动到10度
        duration: 60
        easing.type: Easing.InOutQuad
        loops: 3 //循环3次
        onFinished: target[targetProperty] = originVal //显示归位
    }
    
    
    AbstractButton{
        id: btn
        ...
        onClicked: {
            shakeAnimation.originVal = rect[targetProperty]
            shakeAnimation.start() //开始动画
        }
    }

二、完整代码

import QtQuick
import Qt5Compat.GraphicalEffects
import QtQuick.Controls

Window {
    width: 640
    height: 480
    visible: true
    //    color: "#00000000"
    title: " "


    ListView{
        id: listView
        anchors.fill: parent
        orientation: ListView.Horizontal
        model: ListModel{
            ListElement{action:"旋转抖动";offset:10;targetProperty:"rotation"}
            ListElement{action:"水平抖动";offset:10;targetProperty:"x"}
            ListElement{action:"垂直抖动";offset:10;targetProperty:"y"}
            ListElement{action:"缩放抖动";offset:0.5;targetProperty:"scale"}
        }

        delegate: Item{
            width: 100
            height: listView.height
            Column{
                anchors.centerIn: parent
                spacing: 10
                Item{
                    width: 40
                    height: width
                    anchors.horizontalCenter: parent.horizontalCenter
                    Rectangle{
                        id: rect
                        x: parent.width / 2 - rect.width / 2
                        width: 40
                        height: width
                        radius: 6
                        border{
                            color: "black"
                            width: 2
                        }

                        Rectangle{
                            y: -16
                            z: rect.z - 1
                            anchors.horizontalCenter: rect.horizontalCenter
                            width: 20
                            height: width
                            radius: 6
                            border{
                                color: "black"
                                width: 2
                            }
                        }
                    }
                }//Item

                PropertyAnimation {
                    id: shakeAnimation
                    property int originVal
                    target: rect
                    property: targetProperty
                    from: originVal - model.offset
                    to: originVal + model.offset
                    duration: 60
                    easing.type: Easing.InOutQuad
                    loops: 3
                    onFinished: target[targetProperty] = originVal
                }


                AbstractButton{
                    id: btn
                    text: model.action
                    anchors.horizontalCenter: parent.horizontalCenter
                    contentItem: Text{
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                        color: "white"
                        text: btn.text
                    }
                    background: Rectangle{
                        implicitHeight: 28
                        implicitWidth: 60
                        color: btn.pressed ? "#2b45d3" : "#2436ad"
                        radius: 6
                    }
                    onClicked: {
                        shakeAnimation.originVal = rect[targetProperty]
                        shakeAnimation.start()
                    }
                }
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是唐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值