FrameAnimation QML Type

Triggers a handler at every animation frame update. 更多...

import 语句: import QtQuick
Since: Qt 6.4

特性

信号

方法

详细描述

A FrameAnimation can be used to trigger an action every time animations have progressed and an animation frame has been rendered. See the documentation about the 场景图形 for in-depth information about the threaded and basic render loops.

For general animations, prefer using NumberAnimation and other 动画 elements as those provide declarative way to describe the animations.

FrameAnimation on the other hand should be used for custom imperative animations and in use-cases like these:

  • When you need to run some code on every frame update. Or e.g. every other frame, maybe using progressive rendering.
  • When the speed / target is changing during the animation, normal QML animations can be too limiting.
  • When more accurate frame update time is needed, e.g. for fps counter.

Compared to Timer which allows to set the interval time, FrameAnimation runs always in synchronization with the animation updates. If you have used Timer with a short interval for custom animations like below, please consider switching to use FrameAnimation instead for smoother animations.

// BAD
Timer {
    interval: 16
    repeat: true
    running: true
    onTriggered: {
        // Animate something
    }
}
// GOOD
FrameAnimation {
    running: true
    onTriggered: {
        // Animate something
    }
}
					

特性文档编制

currentFrame : int [read-only]

This property holds the number of frame updates since the start. When the frame animation is restarted, currentFrame starts from 0 .

The following example shows how to react on frame updates.

FrameAnimation {
    running: true
    onTriggered: {
        // Run code on every frame update.
    }
}
					

This property can also be used for rendering only every nth frame. Consider an advanced usage where the UI contains two heavy elements and to reach smooth 60fps overall frame rate, you decide to render these heavy elements at 30fps, first one on every even frames and second one on every odd frames:

FrameAnimation {
    running: true
    onTriggered: {
        if (currentFrame % 2 == 0)
            updateUIElement1();
        else
            updateUIElement2();
   }
}
					

默认情况下, frame 为 0。

elapsedTime : qreal [read-only]

This property holds the time (in seconds) since the previous start.

默认情况下, elapsedTime 为 0。

frameTime : qreal [read-only]

This property holds the time (in seconds) since the previous frame update.

The following example shows how to use frameTime to animate item with varying speed, adjusting to screen refresh rates and possible fps drops.

Rectangle {
    id: rect
    property real speed: 90
    width: 100
    height: 100
    color: "red"
    anchors.centerIn: parent
}
FrameAnimation {
    id: frameAnimation
    running: true
    onTriggered: {
        // Rotate the item speed-degrees / second.
        rect.rotation += rect.speed * frameTime
    }
}
					

默认情况下, frameTime 为 0。

paused : bool

If set to true, pauses the frame animation; otherwise resumes it.

paused defaults to false.

另请参阅 pause () 和 resume ().

running : bool

If set to true, starts the frame animation; otherwise stops it.

running defaults to false.

另请参阅 stop (), start (),和 restart ().

smoothFrameTime : qreal [read-only]

This property holds the smoothed time (in seconds) since the previous frame update.

The following example shows how to use smoothFrameTime to show average fps.

Text {
    text: "fps: " + frameAnimation.fps.toFixed(0)
}
FrameAnimation {
    id: frameAnimation
    property real fps: smoothFrameTime > 0 ? (1.0 / smoothFrameTime) : 0
    running: true
}
					

默认情况下, smoothFrameTime 为 0。

信号文档编制

triggered ()

此信号发射当 FrameAnimation has progressed to a new frame.

注意: 相应处理程序是 onTriggered .

方法文档编制

pause ()

Pauses the frame animation

If the frame animation is already paused or not running , calling this method has no effect. The paused property will be true following a call to pause() .

reset ()

Resets the frame animation properties

Calling this method resets the frame and elapsedTime to their initial values (0). This method has no effect on running or paused properties and can be called while they are true or false.

The difference between calling reset() and restart() is that reset() will always initialize the properties while restart() initializes them only at the next frame update which doesn't happen e.g. if restart() is immediately followed by pause() .

restart ()

Restarts the frame animation

FrameAnimation is not running it will be started, otherwise it will be stopped, reset to initial state and started. The running property will be true following a call to restart() .

resume ()

Resumes a paused frame animation

If the frame animation is not paused or not running , calling this method has no effect. The paused property will be false following a call to resume() .

start ()

Starts the frame animation

If the frame animation is already running, calling this method has no effect. The running property will be true following a call to start() .

stop ()

Stops the frame animation

If the frame animation is not running, calling this method has no effect. Both the running and paused properties will be false following a call to stop() .