当前位置: 首页 > news >正文

QML动画(其他的动画)

PauseAnimation (暂停动画)

为动画提供暂停

Rectangle{id:rect1width: 100;height: 100;x:100;y:100color: "lightBlue"SequentialAnimation{running: trueColorAnimation {target: rect1;property: "color";from: "white";to: "black";duration: 2000}PauseAnimation {duration: 2000  //暂停2s}ColorAnimation {target: rect1;property: "color";from: "black";to: "white";duration: 2000}}}

 SmoothedAnimation(平滑动画)

平滑动画使用缓入/缓出四缓曲线将属性值动画化为设定的目标值。当目标值更改时,用于在新旧目标值之间进行动画处理的缓动曲线将平滑地拼接在一起,以创建到保持当前速度的新目标值的平滑运动。

属性:

duration持续时间
maximumEasingTime最大缓动时间
reversingMode缓动模型
velocity移动速度(默认速度为 200 个单位/秒)

reversingMode:enumeration

SmoothedAnimation.Eased (default) 动画将平滑减速,然后反转方向
SmoothedAnimation.Immediate动画将立即开始向相反方向加速,从速度 0 开始
SmoothedAnimation.Sync 属性立即设置为目标值

按下上下左右按键,移动小方块。

import QtQuick 2.9
import QtQuick.Window 2.2Window {id:window1visible: truewidth: 700height: 700title: qsTr("Hello World")Rectangle{anchors.fill:parentRectangle {id:rect1width: 60; height: 60x: 50; y: 50color: "green"Behavior on x {SmoothedAnimation { velocity: 500 } }Behavior on y { SmoothedAnimation { velocity: 500 } }}focus: trueKeys.onRightPressed: rect1.x = rect1.x + 100Keys.onLeftPressed: rect1.x = rect1.x - 100Keys.onUpPressed: rect1.y = rect1.y - 100Keys.onDownPressed: rect1.y = rect1.y + 100}
}

PropertyAction(属性操作)

属性操作用于指定动画期间的即时属性更改。属性更改不会以动画形式显示。它可用于在动画期间设置非动画属性值。

属性:

exclude排除属性

property

properties

属性

target

targets

对象
value数值

 动画中修改透明度和缩放

Rectangle{id:rect1width:100;height: 100;x:100;y:100;color: "lightBlue"SequentialAnimation{running: trueColorAnimation {target: rect1;property: "color";from: "white";to: "black";duration: 2000}PropertyAction{target: rect1;property: "opacity";value: 0.2//透明度}PropertyAction{target: rect1;property: "scale";value: 0.5//缩放}ColorAnimation {target: rect1;property: "color";from: "black";to: "white";duration: 2000}}}

使用State来定义过渡时出现的问题:

采用 State 来定义过渡结束时的值。动画将以默认值旋转,然后跳转到指定位置。

Item {width: 400; height: 400Rectangle {id: rectwidth: 200; height: 100color: "red"states: State {name: "rotated"//围绕右下角旋转PropertyChanges { target: rect; rotation: 180; transformOrigin: Item.BottomRight }}transitions: Transition {//设置过渡动画RotationAnimation { duration: 1000; direction: RotationAnimation.Counterclockwise }}MouseArea {anchors.fill: parentonClicked: rect.state = "rotated"}}
}

解决方法:

在旋转动画开始前插入一个属性操作,这会立即将属性设置为过渡的结束状态中定义的值,以便旋转动画从正确的变换原点开始。

transitions: Transition {SequentialAnimation {PropertyAction { target: rect; property: "transformOrigin" }RotationAnimation { duration: 1000; direction: RotationAnimation.Counterclockwise }}
}

ScriptAction (脚本操作)

脚本操作可用于在动画中的特定点运行脚本。

属性: 

script脚本
scriptName脚本名称

 直接使用脚本操作:

function fun1(){console.log("脚本已执行")}Rectangle{id:rect1width:100;height: 100;x:100;y:100;color: "lightBlue"SequentialAnimation{running: trueNumberAnimation{}ScriptAction{script:fun1()} //执行脚本NumberAnimation{}}}

使用state使用动画:

function fun1(){console.log("脚本已执行")}Rectangle{id:rect1focus:truewidth:100;height: 100;x:100;y:100;color: "lightBlue"states: State {name: "some"StateChangeScript{name:"myScript"script: fun1()//执行脚本}}transitions: Transition {to:"some"SequentialAnimation{NumberAnimation{}ScriptAction{scriptName: "myScript"}NumberAnimation{}}}Keys.onSpacePressed: {rect1.state="some"}}

SpringAnimation(弹簧动画)

SpringAnimation模仿弹簧的振荡行为,使用适当的弹簧常数来控制加速度,使用阻尼来控制效果消失的速度。您还可以限制动画的最大速度。

 属性:

damping保存弹簧阻尼值,

描述类似弹簧的运动停止的速度。默认值为 0。

有用的值范围为 0 - 1.0。值越低,休息得越快

epsilon

值的变化率和变化量,该值足够接近 0 以被视为等于零

对于像素位置,0.25 就足够了。对于比例,0.005 就足够了。

默认值为 0.01。

mass质量,默认情况下,该值为 1.0
modulus保存模值。默认值为 0,设置模数会强制目标值在数处“环绕”。例如,将模数设置为 360 将导致值 370 环绕为 10。
spring

描述目标被拉向源的强度。默认值为 0(即禁用类似弹簧的运动)。有用值范围为 0 - 5.0。

如果设置此属性并且速度值大于 0,则速度会限制最大速度。

velocity

此属性保持跟踪源时允许的最大速度。默认值为 0(无最大速度)

鼠标点到哪里,图形移动到哪里,有点震荡的效果。

Item{anchors.fill:parentRectangle{id:rect1width: 100;height: 100;color: "lightBlue"Behavior on x{SpringAnimation{spring: 2;damping: 0.2}}Behavior on y{SpringAnimation{spring: 2;damping: 0.2}}}MouseArea{anchors.fill:parentonPressed: {rect1.x=mouse.xrect1.y=mouse.y}}}

AnchorAnimation(锚点动画)

锚点动画用于对锚点更改进行动画处理,锚点动画只能在过渡中以及与锚点更改结合使用。它不能用于行为和其他类型的动画。

属性:

duration持续时间
easing缓和
targets项目列表

使用AnchorChageds(锚点更改)

Item{id:item1width: 500;height: 500focus:trueRectangle{id:rect1width: 100;height: 100;color: "lightBlue"}states:State{name:"some"AnchorChanges{    //锚的修改target: rect1anchors.top:item1.topanchors.bottom:item1.bottom}PropertyChanges {target: rect1anchors.topMargin: 10anchors.bottom: 10}}Keys.onPressed: {item1.state="some"}}

使用锚点动画:

Item{id:item1width: 500;height: 500focus:trueRectangle{id:rect1width: 100;height: 100;color: "lightBlue"}states:State{name:"some"AnchorChanges{    //锚的修改target: rect1anchors.top:item1.topanchors.bottom:item1.bottom}PropertyChanges {target: rect1anchors.topMargin: 10anchors.bottom: 10}}transitions: Transition {AnchorAnimation{duration: 1000}//设置持续时间}Keys.onPressed: {item1.state="some"}}

ParentAnimation(父动画)

对父值中的更改进行动画处理,父动画可以包含任意数量的动画,父动画一般在过渡中与父更改一起使用,以这种方式使用时,它会对状态更改期间发生的任何父级更改进行动画处理。这可以通过使用目标属性设置特定目标项来覆盖。

 属性:

parent父项
target对象
via通过其重新设置父级的项目。这提供了一种在剪切旧父项和新父项时执行未剪裁动画的方法

例子:

Item{id:item1width: 500;height: 500Rectangle{id:rect1width: 100;height: 100;color: "lightBlue"}Rectangle{id:rect2focus:truewidth:50;height:50;x:200;y:200;color:"lightGreen"states:State{name:"some"ParentChange{target: rect2;parent: rect1;x:50;y:50}}transitions: Transition {ParentAnimation{ //使用父类动画NumberAnimation{properties: "x,y";duration:2000}ColorAnimation{property:"color";from:"lightGreen";to:"red";duration: 2000}}}Keys.onPressed: {rect2.state="some"}}}

 

PathAnimation(路径动画)

在过渡中使用时,可以指定不带起点或终点的路径

属性:

anchrorPoint锚点
duration持续时间,默认值为 250
easing缓和
endRotation结束旋转
orientation方向
oritationEntryDuration方向进入的持续时间
oritationExitDuration方向退出的持续时间
path保存要沿其进行动画处理的路径。
target项目

orientation:enumerate

PathAnimation.Fixed (default) 不会控制项目的旋转
PathAnimation.RightFirst 项目的右侧将沿着路径引导
PathAnimation.LeftFirst项目的左侧将沿着路径引导
PathAnimation.BottomFirst项目的底部将沿着路径引导
PathAnimation.TopFirst项目的顶部将沿着路径引导

例子:

Window {id:window1visible: truewidth: 700height: 700title: qsTr("Hello World")Rectangle{id:rect1width: 100;height: 100;color: "lightBlue"}PathAnimation{id:pathtarget:rect1duration: 4000orientationEntryDuration: 2000orientationExitDuration: 2000easing.type: Easing.InQuartpath:Path{startX: 0  //起点startY: 0pathElements: PathArc{ //绘制圆弧x:360   //终点y:0useLargeArc: trueradiusX: 160radiusY: 160direction: PathArc.Counterclockwise}}}MouseArea {anchors.fill: parentonClicked: {path.start()//启动路径绘制}}}

参考文章:

Animation and Transitions in Qt Quick | Qt Quick 5.15.12

 QML之PathAnimation路径动画_炫彩灵感的博客-CSDN博客

http://www.lryc.cn/news/33781.html

相关文章:

  • Spark 配置项
  • 掌握Vue3模板语法,助你轻松实现高效Web开发
  • Jmeter+Ant+Jenkins接口自动化测试平台搭建
  • ncnn部署(CMakelists.txt)
  • SQL分库分表
  • 大数据分析案例-基于逻辑回归算法构建微博评论情感分类模型
  • 0105深度优先搜索算法非递归2种实现对比-无向图-数据结构和算法(Java)
  • 传统手工数据采集耗时耗力?Smartbi数据填报实现数据收集分析自动化
  • 《Spring源码深度分析》第5章 Bean的加载
  • 华为OD机试真题Java实现【求最大数字】真题+解题思路+代码(20222023)
  • Java——异常机制
  • 【大数据实时数据同步】超级详细的生产环境OGG(GoldenGate)12.2实时异构同步Oracle数据部署方案(下)
  • ESP32设备驱动-土壤湿度传感器驱动
  • 公网远程连接MongoDB数据库【内网穿透】
  • SQL注入——floor报错注入
  • P6入门:在EPS下创建项目(P6Professional)
  • Linux安装及管理应用和账号和权限管理 讲解
  • 【JDK1.8 新特性】Stream API
  • Springboot Maven打包跳过测试的五种方式总结 -Dmaven.test.skip=true
  • 静态链接和动态链接的区别
  • MATLAB学习笔记1
  • Gorm -- 查询记录
  • 「Python 基础」错误、调试与测试
  • 17万字 JUC 看这一篇就够了(一) (精华)
  • C++右值引用/移动语义
  • 小樽C++ 多章⑧ (叁) 指针与字符串、(肆) 函数与指针
  • Mybatis-Plus
  • yolov8行人识别教程(2023年毕业设计+源码)
  • CAD指令框找不到了怎么调出来?CAD指令框调出方法
  • 一般用哪些工具做大数据可视化分析?