QT Quick QML 动画——SpringAnimation弹簧动画、NumberAnimation数字动画和Behavior


所有的热爱都要不遗余力,真正喜欢它便给它更高的优先级,和更多的时间吧!

QML其它文章请点击这里:     QT QUICK QML 学习笔记


1. SpringAnimation 弹簧动画

SpringAnimation 弹簧动画,通过适当的弹簧常数模拟了弹簧的振荡行为。

1)继承关系:

Animation
PropertyAnimation
NumberAnimation
SpringAnimation

SpringAnimation 继承了NumberAnimation,NumberAnimation又继承了PropertyAnimation(后面用到再记录),PropertyAnimation又继承了基类Animation。Animation继承了一大堆,就不说了。

2)实例一,参考安神的例程

参考了安晓辉老师的《QT Quick 核心编程》一书。
当单击鼠标时,矩形会从原始点移动到目标点,移动的过程就像弹簧一样晃动。SpringAnimation具体属性定义点击这里

import QtQuick 2.0

Rectangle{
    width: 400;		height: 200

    Rectangle{
        id: rect
        width: 40	height: 40
        //anchors.centerIn: parent;  //不能用锚布局, 不然就固定死了
        x: 0;	y: 0;
        color: "red"
    }

    SpringAnimation{
        id: springX
        target: rect    //绑定的Item目标
        property: "x"   //绑定的Item目标的属性"x"
        spring: 5       //X轴移动的加速度(具体请看下文的属性介绍)
        damping: 0.06   //值越大,越不会振荡;越小越振荡
        epsilon: 0.25
    }

    SpringAnimation{
        id: springY
        target: rect
        property: "y"   //y轴加速度
        spring: 0.1
        damping: 0.06
        epsilon: 0.25
    }

    MouseArea{
        anchors.fill: parent
        onClicked: {
            springX.from = rect.x
            springX.to = mouse.x -rect.width/2; 
            springX.start();
            
            springY.from  = rect.y
            springY.to = mouse.y -rect.height/2;
            springY.start();
        }
    }
}

SpringAnimation动画,适用于x、y值发生更改时,使用时需绑定,如上述绑定在rect的"x"和“y”上。

运行结果如下:
开始初始化界面——>晃动界面(动态的不会上传呀…)——>鼠标执行的目标界面
在这里插入图片描述

3)实例二:使用一个按键来弹出一个有弹簧动画的窗口

import QtQuick 2.0
import QtQuick.Controls 1.4

Item {
    id : root;
    width: 400;  height: 200

    Rectangle{
        id: rect;
        visible: false;
        width: root.width/2;	height: root.height/2;
        anchors.horizontalCenter: root.horizontalCenter;
        y: 0;
        color: "red"
        
        Text {
            id: txt;
            text: "SpringAnimation";
            anchors.centerIn: parent;
            font.pixelSize: 24;
            color: "blue";
        }
    }

    SpringAnimation {
        id: rectAnimation;
        target: rect;    property: "y";   //绑定Item的“y”属性。
        spring: 2;
        damping: 0.1;
        duration: 2000;   //弹出的延续时间2s
        onStarted: {
            rect.visible = true;
        }
    }

    Button {
        id: btn;
        text: "Button";
        onClicked: {
            rectAnimation.from = -root.height;
            rectAnimation.to = root.height * 0.25;
            rectAnimation.start();
        }
    }
}

点击按键,会弹出一个有弹簧动态效果的矩形框,如下:
在这里插入图片描述

4)实例三:官方历程

  import QtQuick 2.0

  Item {
      width: 300; height: 300

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

          Behavior on x { SpringAnimation { spring: 2; damping: 0.2 } }
          Behavior on y { SpringAnimation { spring: 2; damping: 0.2 } }
      }

      MouseArea {
          anchors.fill: parent
          onClicked: {
              rect.x = mouse.x - rect.width/2
              rect.y = mouse.y - rect.height/2
          }
      }
  }

官方用到了Behavior on 来绑定。运行结果同"例1"

5)具体属性

点击返回
● damping : real
弹簧阻尼值。越大,越不会振荡;越小越振荡
默认值为0,取值0~1.0。

● epsilon : real
值的变化率和变化量,它接近于0就可以认为等于0。这将取决于值的使用情况。对于像素位置,0.25。对于scale,0.005足够。
默认值是0.01。调整这个值可以提供较小的性能改进。

● mass : real
属性的“质量”。
当物体静止时,更大的质量会导致更慢的移动和更大的弹簧般的运动。默认值为1.0。

modulus : real
模值,默认值为0。
比如设为x方向上设为1,那么x方向最大运动量就是1。设为360,但是目标为370,那么就始终差10才能达到目标值。

spring : real
此属性描述将目标拉向源的强度。 用来控制动画的加速度,有效值范围是0~5.0。默认值为0(禁用了spring的动作)。

velocity : real
设定动画的最大速率。 默认值是0(没有最大速度)。

2. Behavior

Behavior为Item的属性变化绑定一个默认的动画对象。一个属性只能绑定一个“behavior”;

1)属性介绍

● animation : Animation
在触发行为时要运行的动画类型。

● enabled : bool
当属性变化时是否触发该行为;默认为true。

2)实例

使用behavior给width绑定了NumberAnimation动画,当红色鼠标区域被点击时,红色宽度变为原来的一半:

import QtQuick 2.0

Rectangle {
    id: rect
    width: 400;  height: 200
    color: "red"

    Behavior on width {
        NumberAnimation { duration: 1000 }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: rect.width = rect.width/2;
    }
}

运行结果:
在这里插入图片描述

使用Behavior定义动画时,不需要设置之前用的target、property、from、to等属性,方便快捷了很多。
要在一个behavior中执行多个动画,可以使用ParallelAnimation或SequentialAnimation。


QML其它文章请点击这里:     QT QUICK QML 学习笔记

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值