【发布时间】:2017-03-06 15:48:06
【问题描述】:
考虑这个例子:
import QtQuick 2.7
import QtQuick.Controls 2.0
ApplicationWindow {
id: appWindow
width: 1024
height: 800
visible: true
Rectangle {
id: rect1
property bool active: true
opacity: active ? 1 : 0
height: 300 * opacity
width: 300 * opacity
Behavior on opacity { NumberAnimation { duration: 1000 } }
MouseArea { anchors.fill: parent; onClicked: parent.active = false }
color: 'cornflowerblue'
}
Rectangle {
id: rect2
property bool active: true
x: 305
opacity: active ? 1 : 0
height: active ? 300 : 0
width: active ? 300 : 0
Behavior on opacity { NumberAnimation { duration: 1000 } }
Behavior on height { NumberAnimation { duration: 1000 } }
Behavior on width { NumberAnimation { duration: 1000 } }
MouseArea { anchors.fill: parent; onClicked: parent.active = false }
color: 'steelblue'
}
}
我有两个 Rectangles 具有相同的可观察行为:单击时,它们的不透明度和大小都会消失。
在内部,Animations 的数量不同,它们同时运行 - 1 或 3:
到目前为止,我主要使用rect1 的模式形式,并且仅在绑定变得不必要的复杂rect2 的情况下使用。但是我想知道,如果动画系统有一些魔法,可以优化单个属性的动画,而绑定可能会降低性能。
在哪些用例中使用模式rect1 是有益的,什么时候使用rect2 的方法更明智?
EDIT 还有第三个选项,可以通过OpacityAnimator 移动到渲染线程。现在我不能再绑定不透明度了,因为它会在动画结束时跳转到 0。
Rectangle {
id: rect3
property bool active: true
opacity: active ? 1 : 0
height: active ? 300 : 0
x: 610
width: height
Behavior on opacity { OpacityAnimator { duration: 1000 } }
Behavior on height { NumberAnimation { duration: 1000 } }
MouseArea { anchors.fill: parent; onClicked: parent.active = false }
color: 'dodgerblue'
}
EDIT 2 解决 Ansh Kumar 的答案:
这是QML Profiler 的摘录。您可以看到,在 rect2 的动画期间,既没有绑定也没有运行 JavaScript,这与 height 和 width 被(有效地)绑定到 opacity 的时候不同rect1 或 width (有效地)绑定到 rect3 中的 height。
此外,动画源几乎没有JS 的踪迹。我无法深入研究它,但似乎只有ScriptAction 得到QQMLScriptString,其余的只有将输入从var 转换为正确类型的成本(如果类型是使用具体动画指定,例如NumberAnimation)。
此外,据我所见,每个动画都没有循环,但所有动画都具有某种update()-function 左右,由单个循环调用(在运行/注册时)(AnimationTimer )。但这是我已经不确定的地方。
现在问题仍然存在:动画的实现是否比优化的 JS 环境更有效,尤其是在创建多个对象和填充时。
【问题讨论】: