您的位置:首页 > 产品设计 > UI/UE

Animation and Transitions in Qt Quick | Qt Quick 5

2016-04-24 19:05 483 查看
Animation and Transitions Types

Transition - Animates transitions during state changes 状态变化时的动画过渡

SequentialAnimation - Runs animations sequentially 序列播放动画

ParallelAnimation - Runs animations in parallel 平行播放动画

Behavior - Specifies a default animation for property changes 指定属性改变时的默认动画

PropertyAction - Sets immediate property changes during animation 动画过程中立即设置属性

PauseAnimation - Introduces a pause in an animation 暂停动画

SmoothedAnimation - Allows a property to smoothly track a value 平滑的动画

SpringAnimation - Allows a property to track a value in a spring-like motion 弹簧动画

ScriptAction - Runs scripts during an animation 允许允许Javascript 在动画中

Types that animate properties based on data types

AnchorAnimation

Animates changes in anchor values

ColorAnimation

Animates changes in color values

NumberAnimation

Animates changes in qreal-type values

ParentAnimation

Animates changes in parent values

PathAnimation

Animates an item along a path

PropertyAnimation

Animates changes in property values

RotationAnimation

Animates changes in rotation values

Vector3dAnimation

Animates changes in QVector3d values

Animations are created by applying animation types to property values. Animation types will interpolate property values to create smooth transitions. As well, state transitions may assign 分配 animations to state changes.

To create an animation, use an appropriate animation type for the type of the property that is to be animated, and apply the animation depending on the type of behavior that is required.

Triggering Animations 触发动画

There are several ways of setting animation to an object.

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.

Rectangle {

id: flashingblob

width: 75; height: 75

color: “blue”

opacity: 1.0

MouseArea {
anchors.fill: parent
onClicked: { 点击鼠标时执行的动画
animateColor.start()
animateOpacity.start()
}
}

PropertyAnimation {id: animateColor; target: flashingblob; properties: "color"; to: "green"; duration: 100}

NumberAnimation {
id: animateOpacity
target: flashingblob
properties: "opacity"
from: 0.99
to: 1.0
loops 循环 : Animation.Infinite  无限
easing {type: Easing.OutBack; overshoot: 500}


}

}

Specialized property animation types have more efficient implementations than the PropertyAnimation type. 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.

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 on 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 } // 这种方法在图形加载时就执行了,对PropertyAnimation没有意义


}

The animation starts as soon as the rectangle is loaded, 动画当图形加载的时候启动 and will automatically be applied to its x and y values. Since the on 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.

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:

import QtQuick 2.0

Rectangle {

width: 100; height: 100

color: “red”

SequentialAnimation on color { //   Predined 方式 可以应用于 SequentialAnimation 指定每个动画的时间
ColorAnimation { to: "yellow"; duration: 1000 }
ColorAnimation { to: "blue"; duration: 1000 }
}


}

Since the SequentialAnimation object has been specified on the color property using the on syntax, its child ColorAnimation objects are also automatically applied to this property and do not need to specify target or property animation values.

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 type can contain animation types 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.

Rectangle {

width: 75; height: 75

id: button

state: “RELEASED”

MouseArea {
anchors.fill: parent
onPressed: button.state = "PRESSED"
onReleased: button.state = "RELEASED"
}

states: [
State {
name: "PRESSED"
PropertyChanges { target: button; color: "lightblue"}
},
State {
name: "RELEASED"
PropertyChanges { target: button; color: "lightsteelblue"}
}
]

transitions: [
Transition {
from: "PRESSED"
to: "RELEASED"
ColorAnimation { target: button; duration: 100}
},
Transition {
from: "RELEASED"
to: "PRESSED"
ColorAnimation { target: button; duration: 100}
}
]


}

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.

transitions:

Transition {

to: “*” // 应用于任何状态变化

ColorAnimation { target: button; duration: 100}

}

Default Animation as Behaviors 默认的动画叫做行为

Default property animations are set using behavior animations. Animations declared in Behavior types apply to the property and animates any property value changes. However, Behavior types 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 declaration is a convenient way of assigning a behavior animation onto a property.

See the Qt Quick Examples - Animation for a demonstration 示范 of behavioral animations.

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 type, the opacity animations will play after the preceding animation finishes. The ParallelAnimation type 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 type is also useful for playing transition animations because animations are played in parallel inside transitions.

Controlling Animations 动画控制

There are different methods to control animations.

Animation Playback 重放

All animation types inherit from the Animation type. It is not possible to create Animation objects; instead, this type provides the essential properties and methods for animation types. Animation types have start(), stop(), resume(), pause(), restart(), and complete() – all of these methods control the execution 执行 of animations.

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.

Other Animation Types

In addition, QML provides several other types useful for animation:

PauseAnimation: enables pauses during animations

ScriptAction: allows JavaScript to be executed during an animation, and can be used together with StateChangeScript to reused existing scripts

PropertyAction: changes a property immediately during an animation, without animating the property change

These are specialized animation types that animate different property types

SmoothedAnimation: a specialized NumberAnimation that provides smooth changes in animation when the target value changes

SpringAnimation: provides a spring-like animation with specialized attributes such as mass, damping and epsilon

ParentAnimation: used for animating a parent change (see ParentChange)

AnchorAnimation: used for animating an anchor change (see AnchorChanges)

Sharing Animation Instances

Sharing animation instances between Transitions or Behaviors is not supported, and may lead to undefined behavior. In the following example, changes to the Rectangle’s position will most likely not be correctly animated.

Rectangle {

// NOT SUPPORTED: this will not work correctly as both Behaviors

// try to control a single animation instance

NumberAnimation { id: anim; duration: 300; easing.type: Easing.InBack } // 动画实例 行为相同时可以被应用到不同对象

Behavior on x { animation: anim }

Behavior on y { animation: anim }

}

The easiest fix is to repeat the NumberAnimation for both Behaviors. If the repeated animation is rather complex, you might also consider creating a custom animation component and assigning an instance to each Behavior, for example:

//当动画很复杂时,写入一个独立的文件

// MyNumberAnimation.qml

NumberAnimation { id: anim; duration: 300; easing.type: Easing.InBack }

// main.qml

Rectangle {

Behavior on x { MyNumberAnimation {} } // 写在其他文件中的调用方法

Behavior on y { MyNumberAnimation {} }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: