【问题标题】:Kivy Gif animation runs too oftenKivy Gif 动画运行太频繁
【发布时间】:2015-06-27 20:42:22
【问题描述】:

我想制作一个带有 gif 动画的 kivy 程序,它运行一次然后停止。 我将 anim_loop 设置为 1 ,但它会一遍又一遍地运行。 代码如下:

Root = Builder.load_string('''
  Image:
    source: 'streifen1.gif'
    size_hint_y: 1
    anim_loop: 1
''')


class TTT(App):
 def build(self):
    Window.clearcolor = (0, 0.5, 1, 1)# sets the backgroundcolor
    return Root #returnst the kv string and therefore the root widget`

【问题讨论】:

  • 您使用的是 kivy 1.9 吗?您是否可以尝试压缩图片而不是 gif,这可能会得到更好的支持?
  • @inclement 我应该怎么做,文档中没有关于它的内容:kivy.org/docs/…
  • 制作一个包含图像的 zip 文件,并将图像源指向该文件名。
  • @inclement,希望它像你描述的那样简单,但我已经尝试过了,但并不快乐。我们已经在无证区域,现在进入巫术。基维,救命!!

标签: python kivy gif animated-gif


【解决方案1】:

anim_loop 属性应该可以在 Kivy 1.9.0 中使用,如果您使用的是旧版本,请考虑升级。

还有另一种方式。您可以使用以下代码停止动画:

myimage._coreimage.anim_reset(False)

要在动画播放后停止动画,观察texture 属性。加载每个帧后都会更改。如果您的 GIF 有 n 帧,则在第 (n+1) 次调用 on_texture 方法后停止动画。

from kivy.app import App
from kivy.uix.image import Image

class MyImage(Image):
    frame_counter = 0
    frame_number = 2 # my example GIF had 2 frames

    def on_texture(self, instance, value):     
        if self.frame_counter == self.frame_number + 1:
            self._coreimage.anim_reset(False)
        self.frame_counter += 1


class MyApp(App):
    def build(self):
        return MyImage(source = "test.gif")

if __name__ == '__main__':
    MyApp().run()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-25
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多