As the .Gif file contains the duration of each frame, my little script has been modified because the duration parameter is not needed.
Name of class and view parameter are also modified because the view in which the gif will be displayed may be any View, not only an ImageView.
I also remove the touch_ended method because it is not needed here.
Sorry for these modifications, I had posted too quickly, just to be sure I'd not forget, I've a so bad memory 😢
import ui
import io
from PIL import Image
class GifInView(ui.View):
def __init__(self,view,gif_file):
self.ImageView = ui.ImageView()
self.add_subview(self.ImageView)
self.pil = Image.open(gif_file)
wi,hi = self.pil.size
wiv = view.width
hiv = wiv * hi / wi
self.frame = (0,0,wiv,hiv)
view.add_subview(self)
# In a GIF file, each frame has its own duration
# assume here all frames have the same duration
self.update_interval = self.pil.info['duration']/1000
self.frame_id = 0
def pil2ui(self,imgIn):
with io.BytesIO() as bIO:
imgIn.save(bIO, 'PNG')
imgOut = ui.Image.from_data(bIO.getvalue())
del bIO
return imgOut
def update(self):
# Display individual frames from the loaded animated GIF file
self.pil.seek(self.frame_id)
self.ImageView.image = self.pil2ui(self.pil)
self.ImageView.content_mode = ui.CONTENT_SCALE_ASPECT_FIT
self.frame_id = self.frame_id + 1
if self.frame_id >= self.pil.n_frames:
self.frame_id = 0
if __name__ == '__main__':
v = ui.load_view('test3')
GifInView(v['imageview1'],'meteo1.gif')
GifInView(v['imageview2'],'meteo2.gif')
v.present('sheet')