经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
26.Qt Quick QML-RotationAnimation、PathAnimation、SmoothedAnimation、Behavior、PauseAnimation、SequentialAnimation和ParallelAnimation
来源:cnblogs  作者:诺谦  时间:2021/6/7 9:18:31  对本文有异议

1.RotationAnimation
RotationAnimation也是继承于PropertyAnimation组件,但是它有点特殊,它只需要指定taget目标对象,并且不需要指定property,因为rotation就是要绑定的属性.并且它还多了个direction属性:

  • direction : enumeration,设置旋转的方向,取值有:
    • RotationAnimation.Numerical (默认值) - 数值旋转,通过to-from得出要旋转的度数,然后进行旋转,比如from为10,to为100,那么旋转就是90°
    • RotationAnimation.Clockwise - 在两个度数之间进行顺时针旋转,比如from为10,to为100,那么顺旋转就是90°
    • RotationAnimation.Counterclockwise - 在两个度数之间进行逆时针旋转,比如from为10,to为100,那么逆旋转就是90°
    • RotationAnimation.Shortest - 沿最短路径的方向旋转。比如from为10,to为350,那么将逆时针旋转20°

示例如下所示:

  1.   property var rotationModel: [
  2. ["#00FF00", RotationAnimation.Numerical],
  3. ["#FF0000", RotationAnimation.Clockwise],
  4. ["#0000FF", RotationAnimation.Counterclockwise],
  5. ["#00FFFF", RotationAnimation.Shortest],
  6. ]
  7. property var animations: new Array(0)
  8. MouseArea {
  9. id: area
  10. anchors.fill: parent
  11. onPressed: {
  12. for (var i in animations) {
  13. console.log(animations[i].direction)
  14. animations[i].start();
  15. }
  16. }
  17. }
  18. Row {
  19. spacing: 10
  20. Repeater {
  21. model: rotationModel.length
  22. Rectangle {
  23. id: rect
  24. width: 100
  25. height: 100
  26. radius: width / 2
  27. color: rotationModel[index][0]
  28. Text {
  29. anchors.centerIn: parent
  30. text: "model"+ index.toString()
  31. color: "white"
  32. font.pixelSize: 18
  33. font.bold: true
  34. }
  35. RotationAnimation {
  36. running: true
  37. duration: 500
  38. target: rect
  39. direction: rotationModel[index][1]
  40. from : 10
  41. to : 350
  42. Component.onCompleted: animations[index] = this
  43. }
  44. }
  45. }
  46. }

效果如下所示:

 

2.PathAnimation
PathAnimation继承于Animation组件,用来实现一个路径动画.

它的属性如下所示:

  • anchorPoint : point,设置目标瞄点,默认为目标的左上角(其0,0点)将定位到(或跟随)路径。比如10x10的Rectangle,那么anchorPoint为Qt.point(5, 5)时则表示始终让物体中心处于路径上
  • duration : int,持续时间
  • easing.type : enumeration,设置动画的缓和曲线.默认值为Easing.Linear(线性过程),比如我们设置为Easing.InQuad时,动画效果就是从慢到快的过程.
  • easing.amplitude : real,设置缓和曲线的振幅,默认值为1.0,值越大振幅越大,仅当easing.type为Easing.InBounce, Easing.OutBounce, Easing.InOutBounce, Easing.OutInBounce, Easing.InElastic, Easing.OutElastic, Easing.InOutElastic or Easing.OutInElastic才生效
  • orientation : enumeration,设置对象在路径上的方向,取值有以下几种:
    • PathAnimation.Fixed (default) - 在动画中只是移动物体,并不旋转物体
    • PathAnimation.RightFirst - 始终让目标的右侧在前面沿着路径移动
    • PathAnimation.LeftFirst - 始终让目标的左侧在前面沿着路径移动
    • PathAnimation.BottomFirst - 始终让目标的底部在前面沿着路径移动
    • PathAnimation.TopFirst - 始终让目标的顶部在前面沿着路径移动
  • endRotation : real,设置目标移动路径结束后的旋转角度,如果orientationEntryDuration未设置,物体则会直接跳转到该值,否则就以动画的形式结束
  • orientationEntryDuration : real,设置启动时的旋转过渡时间(毫秒单位),比如启动动画时,如果当前目标方向和orientation值方向不一致,则需要一个过渡时间才行
  • orientationExitDuration : real,设置结束时的旋转过渡时间(毫秒单位),如果endRotation已设置,那么就会有个过渡时间,让物体最终旋转到endRotation角度.
  • target : Item ,设置动画的目标对象
  • path : Path,设置动画的路径。

接下来我们便来讲解Path路径

3.Path
一个Path可以由下面多个Path段组成:

  • PathLine : 由一个坐标指定的直线路径
  • PathPolyline : 由一个path坐标列表指定的多段路径
  • PathQuad : 由一个控制点生成的二次贝塞尔曲线路径
  • PathCubic : 由两个控制点生成的三次贝塞尔曲线路径
  • PathArc : 由结束坐标,以及一个radiusX和radiusY半径实现的一个圆弧(顺时针绘画)
  • PathAngleArc : 由中心点、半径和起始角度startAngle、旋转角度sweepAngle指定的圆弧。
  • PathCurve :由一个坐标点生成的curve曲线路径(通常需要配合多个PathCurve才行)
  • PathSvg : 由SVG路径字符串实现的路径。你可以用它创建线条, 曲线, 弧形等等

显示一个Path路径
我们可以通过设置context2D的path属性,来显示出我们绘画的Path路径,不然都不知道绘制的路径到底对不对,真是"半夜吃黄瓜-摸不着头尾"
我们以PathArc为例,示例如下所示:

  1. Canvas {
  2. id: canvas
  3. anchors.fill: parent
  4. antialiasing: true
  5. onPaint: {
  6. var context = canvas.getContext("2d")
  7. context.clearRect(0, 0, width, height)
  8. context.strokeStyle = "black"
  9. context.path = path
  10. context.stroke()
  11. }
  12. }
  13. Path {
  14. id: path
  15. startX: 100; startY: 100
  16. PathArc {
  17. x: 100; y: 140
  18. radiusX: 100; radiusY: 50
  19. useLargeArc: true
  20. xAxisRotation: 30
  21. }
  22. }

效果如下所示:

 

这里我们设置弧线的起始位置为(100,100),终点位置为(100,140).
它的useLargeArc为true,表示使用角度大的那个圆弧,如果设置为false,则将会使用小的那个圆弧
它的xAxisRotation表示x水平方向是否倾斜,我们这里按顺时针倾斜了30°,该值仅在x和y半径不同时有用,这意味着圆弧是椭圆形的

接下来我们便来学习PathSvg路径.
4.PathSvg
PathSvg支持的命令如下所示:

  • M = moveto,移动画笔到指定点处(只是移动位置,不画线),写法:M x y
  • L = lineto,从当前点绘制一条直线到目的点,写法:L x y
  • H = horizontal lineto,绘制一条平行线,写法:H x
  • V = vertical lineto,绘制一条垂直线,写法:V y
  • C = curveto,绘制一条三次贝塞尔曲线,写法:C x1 y1, x2 y2, x y (x和y为终点,其它两个为控制点)
  • S = smooth curveto,用来写在C命令后面时来延长贝塞尔曲线,当然也可以跟在S命令后面,写法:S x1 y1, x y(x1和y1是控制点,x和y是终点)
  • Q = quadratic Bézier curve,绘制一条二次贝塞尔曲线,写法:Q x1 y1, x y
  • T = smooth quadratic Bézier curveto,用来写在Q命令后面时来延长贝塞尔曲线,当然也可以跟在T命令后面,写法:T x y
  • A = elliptical Arc,绘制一条圆弧,写法:A rx ry x-axis-rotation large-arc-flag sweep-flag x y
    • rx、ry:表示圆弧的半径
    • x-axis-rotation: 表示x水平方向是否倾斜,比如设置为-45,则圆弧会逆时针倾斜45°,注意:该值仅在x和y半径不同时有用,这意味着圆弧是椭圆形的
    • large-arc-flag:设置为0表示绘制小圆弧,为1表示大圆弧
    • sweep-flag: 表示弧线的方向,0表示从起点到终点沿逆时针画弧,1表示从起点到终点沿顺时针画弧
    • x、y:表示绘制的终点位置
  • Z = closepath,关闭路径,将会从当前点绘制一条直线到开始点.

5.PathAnimation使用PathSvg示例

  1. Canvas {
  2. id: canvas
  3. anchors.fill: parent
  4. antialiasing: true
  5. onPaint: {
  6. var context = getContext("2d")
  7. context.clearRect(0, 0, width, height)
  8. context.strokeStyle = "red"
  9. context.path = pathAnim.path
  10. context.stroke()
  11. }
  12. }
  13. PathAnimation {
  14. id: pathAnim
  15. running: true
  16. loops: Animation.Infinite
  17. duration: 5000
  18. easing.type: Easing.InQuad
  19. target: car
  20. orientation: PathAnimation.RightFirst
  21. anchorPoint: Qt.point(car.width/2, car.height)
  22. path: Path {
  23. startX: 100; startY: 100
  24. PathSvg {
  25. path: "M100 100 C600 50, 50 300, 550 250"
  26. }
  27. }
  28. }
  29. Item {
  30. id: car
  31. x: 25; y: 25
  32. width: 80; height: 38
  33. Canvas {
  34. id: canvas2
  35. anchors.fill: parent
  36. antialiasing: true
  37. onPaint: {
  38. var context = getContext("2d")
  39. context.clearRect(0, 0, width, height)
  40. context.fillStyle = "blue"
  41. context.fillRect(0,0,width,height-10)
  42. context.fillStyle = "yellow"
  43. context.ellipse(10,height-20,20,20)
  44. context.ellipse(width-30,height-20,20,20)
  45. context.fill()
  46. console.log(width,height)
  47. }
  48. }
  49. Text {
  50. anchors.centerIn: parent
  51. text: "小车"
  52. color: "white"
  53. }
  54. }

效果如下所示:

 

6.SmoothedAnimation
SmoothedAnimation继承于NumberAnimation组件,它比NumberAnimation更加人性化,不仅可以设置duration时间,还可以设置滑动速率。
并且SmoothedAnimation的easing.type默认值为Easing.InOutQuad(慢->快->慢).
SmoothedAnimation如果同时指定了velocity速度和duration持续时间,则会选择导致动画速度最快的那个所需时间.
例如:

  • 如果velocity速度设置为200,如果duration设置为5000.
  • 假如此时需要将一个物体从0位移到800,那么此时将会选择velocity速度的时间,因为只需要4秒时间.
  • 假如此时需要将一个物体从0位移到2000,那么此时将会选择duration时间,因为只需要5秒时间.

它的属性如下所示:

  • duration : int,持续时间,默认值为-1.表示禁用持续时间值。
  • reversingMode : enumeration,设置如果动画方向被突然被反转后的行为(比如当前动画正在往左边移动时,突然用户让动画往右边移动),取值如下所示:
  • SmoothedAnimation.Eased(默认)-动画将按照速度平滑减速到0后,开始反转方向加速
  • SmoothedAnimation.Immediate-动画将立即以0的速度开始反向加速
  • SmoothedAnimation.Sync-立即将属性设置为目标值
  • velocity : real,设置速度,默认值为200(200像素点每秒),如果将此设置为-1(默认值)将禁用速度值。

示例如下所示:

  1. MouseArea {
  2. anchors.fill: parent
  3. hoverEnabled: true
  4. onPositionChanged: {
  5. animation1.to = mouseX - rect.width/2
  6. animation2.to = mouseY - rect.height/2
  7. animation1.restart()
  8. animation2.restart()
  9. }
  10. }
  11. Rectangle {
  12. id: rect
  13. color: "red"
  14. width: 60; height: 60
  15. radius: width/2
  16. SmoothedAnimation on x{
  17. id: animation1
  18. velocity: 100
  19. reversingMode: SmoothedAnimation.Immediate
  20. }
  21. SmoothedAnimation on y{
  22. id: animation2
  23. velocity: 100
  24. reversingMode: SmoothedAnimation.Immediate
  25. }
  26. }

只要当我们鼠标位置发生改变就会启动SmoothedAnimation动画进行平滑移动,但是我们这样写,代码显得有点繁琐,可以使用Behavior组件来代替设置from和to的行为来节省代码量
接下来我们来给大家讲讲Behavior.


7.Behavior
Behavior用来让动画绑定到对象一个属性上,Behavior中文翻译就是行为的意思,所以当绑定属性发生改变时,就会自动触发Behavior行为,Behavior就会自动设置动画的to属性,并启动动画.
Behavior只有两个属性animation和enabled, animation保存着要触发行为的哪个动画,enabled表示是否使能行为,默认为true.
Behavior示例如下所示:

  1. MouseArea {
  2. anchors.fill: parent
  3. hoverEnabled: true
  4. onPositionChanged: {
  5. rect.x = mouseX - rect.width/2
  6. rect.y = mouseY - rect.height/2
  7. }
  8. }
  9. Rectangle {
  10. id: rect
  11. color: "red"
  12. width: 60; height: 60
  13. radius: width/2
  14. Behavior on x {
  15. SmoothedAnimation {
  16. velocity: 100
  17. reversingMode: SmoothedAnimation.Immediate
  18. }
  19. }
  20. Behavior on y {
  21. SmoothedAnimation {
  22. velocity: 100
  23. reversingMode: SmoothedAnimation.Immediate
  24. }
  25. }
  26. }

这里我们使用Behavior分别为属性绑定了SmoothedAnimation动画,然后我们只需要改变x,y属性,就会自动触发行为,设置我们改变的值到动画的to属性中,并启动动画

 

8.PauseAnimation
PauseAnimation顾名思义就是暂停动画的意思,只有一个duration属性,用来设置该期间内什么都不做.
并且只能在SequentialAnimation动画中使用,接下来我们便来学习SequentialAnimation和ParallelAnimation,并在代码中使用它


9.SequentialAnimation和ParallelAnimation
SequentialAnimation用来将多个动画串行地执行下去.而ParallelAnimation是用来将多个动画并行地执行下去.
并且SequentialAnimation和ParallelAnimation还可以互相嵌套.
需要注意的是:

  • SequentialAnimation或ParallelAnimation中定义的动画不能单独启动和停止;必须作为一个组来启动和停止。
  • 并且SequentialAnimation和ParallelAnimation的running默认为false.如果要想加载组件后立即启动的话,我们需要手动设置(不需要全部设置,只需要设置最顶层动画的running值即可)

示例如下所示:

  1. Window{
  2. id: window
  3. width: 480
  4. height: 320
  5. visible: true
  6. SequentialAnimation {
  7. SequentialAnimation {
  8. ParallelAnimation {
  9. NumberAnimation {
  10. target: ball1
  11. property: "y"
  12. from: 20
  13. to: height - ball1.height - 20
  14. easing.type: Easing.OutBounce;
  15. duration: 1200
  16. }
  17. NumberAnimation {
  18. target: ball2
  19. property: "y"
  20. from: 20
  21. to: height - ball2.height - 20
  22. easing.type: Easing.OutBounce;
  23. duration: 800
  24. }
  25. }
  26. }
  27. PauseAnimation { duration: 500 }
  28. running: true
  29. loops: Animation.Infinite
  30. }
  31. Rectangle {
  32. id: ball1
  33. width: 40; height: 40;
  34. radius: 20
  35. color: "blue"
  36. x: 30
  37. }
  38. Rectangle {
  39. id: ball2
  40. width: 60; height: 60;
  41. radius: 30
  42. color: "red"
  43. x: 200
  44. }
  45. }

效果如下所示:

如上图所示,可以看到每当小球落到地后,就会启动PauseAnimation暂停动画,等待500ms

 

原文链接:http://www.cnblogs.com/lifexy/p/14851811.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号