我正在考虑建立一个gstreamer插件,使动画QML图形,以便稍后叠加到播放视频。到目前为止,使用QML的想法在我看来是非常有希望的,除了一个问题。我需要能够在视频中寻找,动画也必须倒带和跳跃到所需的时间点。现在我已经阅读了QML文档,我看到所有动画通常都是有时间限制的。
因此,我的问题是:是否可以将QML动画绑定到一些“时间源”,而不是真实世界的时间,这可能不是单调的(实际上,它可以被应用程序操作)。或者,更笼统地说,我是否可以将QML动画绑定到一个数值X,这样当它发生变化时,我的动画就会进步,并且X和动画状态之间有着严格的关系。我希望你明白这个想法。
发布于 2015-11-10 13:37:53
这个答案只适用于NumberAnimation对象。可能也可以使用类似的方法来替换其他Animation对象。
正如干涉仪已经说过的那样,除了工作之外,没有别的办法了。
这是我对这个问题的解决办法。它可能看起来很复杂,但我可以保证它很容易使用。在这个答案的末尾,我使用这个代码放置了一个指向示例项目源代码的链接。你可以试试。
将这些文件添加到项目中:
#ifndef EASINGVALUEFORPROGRESS_H
#define EASINGVALUEFORPROGRESS_H
#include <QObject>
#include <QEasingCurve>
class EasingValueForProgress : public QObject
{
Q_OBJECT
public:
explicit EasingValueForProgress(QObject *parent = 0);
Q_INVOKABLE double getValue(int easingEnum, double progress){
QEasingCurve easing((QEasingCurve::Type)easingEnum);
return easing.valueForProgress(progress);
}
signals:
public slots:
};
#endif // EASINGVALUEFORPROGRESS_H
#include "easingvalueforprogress.h"
EasingValueForProgress::EasingValueForProgress(QObject *parent) : QObject(parent)
{
}
import QtQuick 2.0
Item {
id: xValueAnimator
property Item target
property string targetProperty
property double from
property double to
property int easing: Easing.Linear
property double xValue
onXValueChanged: {
if (target.hasOwnProperty(targetProperty)) {
target[targetProperty] = calculateCurrentValue(
from, to, easing, xValue);
}
else
console.error("XValueAnimator: target:", target,
"does not have property", targetProperty)
}
function calculateCurrentValue(
defaultFrom, defaultTo, animationEasing, xValue) {
return defaultFrom + (defaultTo - defaultFrom)
* easingValueForProgress.getValue(animationEasing, xValue)
}
}
将此添加到您的main.cpp中:
#include <QQmlContext>
#include "easingvalueforprogress.h"
EasingValueForProgress easingValueForProgress;
engine.rootContext()->setContextProperty(
"easingValueForProgress", &easingValueForProgress);现在您可以这样使用它(而不是NumberAnimation对象):
XValueAnimator {
target: object_you_want_to_affect // for example id of the object
targetProperty: "property_to_affect" // for example "x"
from: 100
to: 500
easing: Easing.OutQuad // omit to use Easing.Linear
xValue: myXValue // your property holding values from 0 to 1
}
这里 I提供了工作示例项目。请随意下载并测试它。
https://stackoverflow.com/questions/33598187
复制相似问题