QML动画------学习总结

Direct Property Animation

Animations are created by applying animation objects to property values to gradually change the properties over time. These property animations apply smooth movements by interpolating values between property value changes. Property animations provide timing controls and allows different interpolations through easing curves.
  1. Rectangle {
  2. id: flashingblob
  3. width: 75; height: 75
  4. color: "blue"
  5. opacity: 1.0
  6. MouseArea {
  7. anchors.fill: parent
  8. onClicked: {
  9. animateColor.start()
  10. animateOpacity.start()
  11. }
  12. }
  13. PropertyAnimation {id: animateColor; target: flashingblob; properties: "color"; to: "green"; duration: 100}
  14. NumberAnimation {
  15. id: animateOpacity
  16. target: flashingblob
  17. properties: "opacity"
  18. from: 0.99
  19. to: 1.0
  20. loops: Animation.Infinite
  21. easing {type: Easing.OutBack; overshoot: 500}
  22. }
  23. }
Specialized property animation elements have more efficient implementations than the PropertyAnimation element. They are for setting animations to different QML types such as int, color, and rotations. Similarly, the ParentAnimation can animate parent changes.

See the Controlling Animations section for more information about the different animation properties.
小结:
target:      指定目标元素
property:    指定目标元素的属性
from:        指定起始值
to:          指定结束值
loops:       指定循环次数,Animation.Infinite表示无限循环
easing:      指定动画的过渡形式,比如淡入淡出之类的
在这里看不出PropertyAnimation和NumberAnimation的区别(后面还会看见ColorAnimation),都需要指定target和property,另外需要进行触发,比如上面的代码中的XXXXXXXXXXX.start()

--------------------------------------------------------------------------------
Using Predefined Targets and Properties

In the previous example, the PropertyAnimation and NumberAnimation objects needed to specify particular target and properties values to specify the objects and properties that should be animated. This can be avoided by using the <Animation> on <Property> syntax, which specifies the animation is to be applied as a property value source.

Below are two PropertyAnimation objects that are specified using this syntax:

import QtQuick 2.0

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

    PropertyAnimation on x { to: 100 }
    PropertyAnimation on y { to: 100 }
}
The animation starts as soon as the rectangle is loaded, and will automatically be applied to its x and y values. Since the <Animation> on <Property> syntax has been used, it is not necessary to set the target value of the PropertyAnimation objects to rect, and neither is it necessary to set the property values to x and y.

小结:
    这里引入一个语法:<Animation> on <Property>
    和Direct Property Animation作用一样,区别如下:
    不需要指定target,所在元素就是target;
    on后面就是需要发生变化的属性;
    在元素被载入结束后,动画就立即触发

This can also be used by grouped animations to ensure that all animations within a group are applied to the same property. For example, the previous example could instead use SequentialAnimation to animate the rectangle's color first to yellow, then to blue:

  1. import QtQuick 2.0
  2. Rectangle {
  3. width: 100; height: 100
  4. color: "red"
  5. SequentialAnimation on color {
  6. ColorAnimation { to: "yellow"; duration: 1000 }
  7. ColorAnimation { to: "blue"; duration: 1000 }
  8. }
  9. }
Since the SequentialAnimation object has been specified on the color property using the <Animation> on <Property> syntax, its child ColorAnimation objects are also automatically applied to this property and do not need to specify target or property animation values.

小结:SequentialAnimation 作用是序列化多个动画,直白的说就是让多个动画依次发生

--------------------------------------------------------------------------------
Transitions during State Changes

Qt Quick States are property configurations where a property may have different values to reflect different states. State changes introduce abrupt property changes; animations smooth transitions to produce visually appealing state changes.

The Transition element can contain animation elements to interpolate property changes caused by state changes. To assign the transition to an object, bind it to the transitions property.

A button might have two states, the pressed state when the user clicks on the button and a released state when the user releases the button. We can assign different property configurations for each state. A transition would animate the change from the pressed state to the released state. Likewise, there would be an animation during the change from the released state to the pressed state.

  1. Rectangle {
  2. width: 75; height: 75
  3. id: button
  4. state: "RELEASED"
  5. MouseArea {
  6. anchors.fill: parent
  7. onPressed: button.state = "PRESSED"
  8. onReleased: button.state = "RELEASED"
  9. }
  10. states: [
  11. State {
  12. name: "PRESSED"
  13. PropertyChanges { target: button; color: "lightblue"}
  14. },
  15. State {
  16. name: "RELEASED"
  17. PropertyChanges { target: button; color: "lightsteelblue"}
  18. }
  19. ]
  20. transitions: [
  21. Transition {
  22. from: "PRESSED"
  23. to: "RELEASED"
  24. ColorAnimation { target: button; duration: 100}
  25. },
  26. Transition {
  27. from: "RELEASED"
  28. to: "PRESSED"
  29. ColorAnimation { target: button; duration: 100}
  30. }
  31. ]
  32. }
Binding the to and from properties to the state's name will assign that particular transition to the state change. For simple or symmetric transitions, setting the to to property to the wild card symbol, "*", denotes that the transition applies to any state change.

  1. transitions:
  2. Transition {
  3. to: "*"
  4. ColorAnimation { target: button; duration: 100}
  5. }
小结:
状态间的过渡(Transitions during State Changes)

紫色部分是为元素指定默认状态(state)
红色部分是state的定义,里面有PropertyChanges来对属性进行调整
粉色部分是对过渡的定义,from和to分别用于指定起始和结束状态
--------------------------------------------------------------------------------
Default Animation as Behaviors

Default property animations are set using behavior animations. Animations declared in Behavior elements apply to the property and animates any property value changes. However, Behavior elements have an enabled property to purposely enable or disable the behavior animations.

A ball component might have a behavior animation assigned to its x, y, and color properties. The behavior animation could be set up to simulate an elastic effect. In effect, this behavior animation would apply the elastic effect to the properties whenever the ball moves.

Rectangle {
    width: 75; height: 75; radius: width
    id: ball
    color: "salmon"

    Behavior on x {
        NumberAnimation {
            id: bouncebehavior
            easing {
                type: Easing.OutElastic
                amplitude: 1.0
                period: 0.5
            }
        }
    }
    Behavior on y {
        animation: bouncebehavior
    }
    Behavior {
        ColorAnimation { target: ball; duration: 100 }
    }
}
There are several methods of assigning behavior animations to properties. The Behavior on <property> declaration is a convenient way of assigning a behavior animation onto a property.

See the Behaviors example for a demonstration of behavioral animations.

小结:
Behavior 
Behavior on <Property>
可以直接在Behavior里面直接定义动画,Behavior里面还有animation属性,用于指定动画,达到代码复用的作用。如果有on <Property>,那么Behavior内部的动画就不需要再次指定

------------------------------------------------------------------------------------------

Playing Animations in Parallel or in Sequence

Animations can run in parallel or in sequence. Parallel animations will play a group of animations at the same time while sequential animations play a group of animations in order: one after the other. Grouping animations in SequentialAnimation and ParallelAnimation will play the animations in sequence or in parallel.

A banner component may have several icons or slogans to display, one after the other. The opacity property could transform to 1.0 denoting an opaque object. Using the SequentialAnimation element, the opacity animations will play after the preceding animation finishes. The ParallelAnimation element will play the animations at the same time.

Rectangle {
    id: banner
    width: 150; height: 100; border.color: "black"

    Column {
        anchors.centerIn: parent
        Text {
            id: code
            text: "Code less."
            opacity: 0.01
        }
        Text {
            id: create
            text: "Create more."
            opacity: 0.01
        }
        Text {
            id: deploy
            text: "Deploy everywhere."
            opacity: 0.01
        }
    }

    MouseArea {
        anchors.fill: parent
        onPressed: playbanner.start()
    }

    SequentialAnimation {
        id: playbanner
        running: false
        NumberAnimation { target: code; property: "opacity"; to: 1.0; duration: 200}
        NumberAnimation { target: create; property: "opacity"; to: 1.0; duration: 200}
        NumberAnimation { target: deploy; property: "opacity"; to: 1.0; duration: 200}
    }
}
Once individual animations are placed into a SequentialAnimation or ParallelAnimation, they can no longer be started and stopped independently. The sequential or parallel animation must be started and stopped as a group.

The SequentialAnimation element is also useful for playing transition animations because animations are played in parallel inside transitions.

See the Animation basics example for a demonstration of creating and combining multiple animations in QML.

------------------------------------------------------------------------------------------
Controlling Animations动画控制


Animation Playback

All animation types inherit from the Animation element. It is not possible to create Animation objects; instead, this element provides the essential properties and methods for animation elements. Animation elements have start(), stop(), resume(), pause(), restart(), and complete() -- all of these methods control the execution of animations.
除了start()外,还可以有stop(), resume(), pause(), restart(), and complete()
Easing

Easing curves define how the animation will interpolate between the start value and the end value. Different easing curves might go beyond the defined range of interpolation. The easing curves simplify the creation of animation effects such as bounce effects, acceleration, deceleration, and cyclical animations.

A QML object may have different easing curve for each property animation. There are also different parameters to control the curve, some of which are exclusive to a particular curve. For more information about the easing curves, visit the easing documentation.

The easing example visually demonstrates each of the different easing types.
通过指定各种函数实现各种过渡效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值