Xen Non
Xen Non

Reputation: 91

wx.Timer for animation

Simple animation, this works but would the replaced image data stay in the buffer? And is using timer for animation harmful to CPU? I mean like self.timer.Start(25) does some cool animations from individual image files.

self.nm = ['01.png', '02.png', '03.png', '04.png']
self.stop = 0

def time(self, event):
    self.count += 1
    if self.count == 1:
        self.anime = wx.StaticBitmap(self, -1, wx.Bitmap(self.nm[self.stop], wx.BITMAP_TYPE_ANY))
    if self.count == 2:
        self.anime.Show(0)
        if self.stop == 3:
            self.timer.Stop()
        else:
            self.stop+=1
            self.count = 0
    event.Skip()

Upvotes: 2

Views: 644

Answers (1)

Etienne Perot
Etienne Perot

Reputation: 4882

First, you're indeed reloading each image every time it needs to be shown. Yes, this is very bad, because that is a lot of disk I/O, and the disk is probably the slowest device your program has to interact with. Avoid using the disk at all costs when you can.

Now, to alleviate this, a few options:

  • Create your own subclass of wx.animate.Animation to match your animation (need to override some methods from wx.animate.Animation but also some from wx.animate.AnimationBase. This is the proper solution to this problem, but isn't the simplest.
  • Use wx.animate.AnimationCtrl if your animation can be converted to a .gif without acceptable loss of detail. This is slightly easier than the previous solution, since you don't have to write any animation code.
  • Keep the bitmaps in memory; simply store them in a list somewhere ("somewhere" does not mean inside the time function itself, as that would still get reloaded every time the function time is called; "somewhere" means as a member of the object that the time function is a part of).
  • Keep all bitmaps in the UI, and simply toggle visibility between them when you need to change frame.

Upvotes: 1

Related Questions