The animator module provides a simple framwork to create animations.
Example
Animate the size of a window:
from gi.repository import Gtk
from sugar3.graphics.animator import Animator, Animation
# Construct a 5 second animator
animator = Animator(5)
# Construct a window to animate
w = Gtk.Window()
w.connect('destroy', Gtk.main_quit)
# Start the animation when the window is shown
w.connect('realize', lambda self: animator.start())
w.show()
# Create an animation subclass to animate the widget
class SizeAnimation(Animation):
def __init__(self):
# Tell the animation to give us values between 20 and
# 420 during the animation
Animation.__init__(self, 20, 420)
def next_frame(self, frame):
size = int(frame)
w.resize(size, size)
# Add the animation the the animator
animation = SizeAnimation()
animator.add(animation)
# The animation needs to run inside a GObject main loop
Gtk.main()
STABLE.
Bases: object
The animation class is a base class for creating an animation. It should be subclassed. Subclasses should specify a next_frame function to set the required properties based on the animation progress. The range of the frame value passed to the next_frame function is defined by the start and end values.
Parameters: |
|
---|
# Create an animation subclass
class MyAnimation(Animation):
def __init__(self, thing):
# Tell the animation to give us values between 0.0 and
# 1.0 during the animation
Animation.__init__(self, 0.0, 1.0)
self._thing = thing
def next_frame(self, frame):
# Use the `frame` value to set properties
self._thing.set_green_value(frame)
This method is called by the animtor class every frame. This method calculated the frame value to then call next_frame.
Parameters: |
|
---|
This method is called every frame and should be overriden by subclasses.
Parameters: | frame (float) – a value between start and end representing the current progress in the animation |
---|
Bases: gi.overrides.GObject.Object
The animator class manages the the timing for calling the animations. The animations can be added using the add function and then started with the start function. If multiple animations are added, then they will be played back at the same time and rate as each other.
The completed signal is emitted upon the completion of the animation and also when the stop function is called.
Parameters: |
|
---|
Note
When creating an animation, take into account the limited cpu power on some devices, such as the XO. Setting the fps too high on can use signifigant cpu usage on the XO.
Add an animation to this animator
Parameters: | animation (sugar3.graphics.animator.Animation) – the animation instance to add |
---|
Remove all animations and stop this animator
Start the animation running. This will stop and restart the animation if the animation is currently running
Stop the animation and emit the completed signal