【发布时间】:2015-02-06 16:38:11
【问题描述】:
我正在我的 Kivy 应用程序中制作动画,但在所有可用的类中,只有 scatter 似乎可以正确制作动画。问题是,当我触摸散点图时,我的 on_touch_move() 事件根本不起作用。我想用
制作不可选择的散点图self.do_rotation = False
self.do_scale = False
self.do_translation = False
但这似乎不起作用。散点图没有像预期的那样移动,但仍然可以选择。或者也许有一个类可以正确动画?我试过 Image、AsyncImage、Widget 和 Layouts。有什么想法吗?
@EDIT:这是我所说的最简单的例子:
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.scatter import Scatter
from kivy.animation import Animation
from kivy.graphics import Color, Rectangle
class ExampleWidget(Widget):
def __init__(self, **kwargs):
super(ExampleWidget, self).__init__(**kwargs)
self.size = (100,100)
self.pos = (100,100)
with self.canvas:
Color(1,0,0)
self.texture = Rectangle(size=self.size, pos=self.pos)
def on_pos(self, obj, value):
try: self.texture.pos = value
except: pass
class ExampleScatterTexture(Widget):
def __init__(self, **kwargs):
super(ExampleScatterTexture, self).__init__(**kwargs)
with self.canvas:
Color(0,1,0)
texture = Rectangle(size=self.size, pos=self.pos)
class ExampleScatter(Scatter):
def __init__(self, **kwargs):
super(ExampleScatter, self).__init__(**kwargs)
self.do_rotation = False
self.do_scale = False
self.do_translation = False
self.size = (100,100)
self.pos = (100,300)
texture = ExampleScatterTexture(size=self.size)
self.add_widget(texture)
class ExampleScreen(Widget):
def __init__(self, **kwargs):
super(ExampleScreen, self).__init__(**kwargs)
self.size = Window.size
example_widget = ExampleWidget()
self.add_widget(example_widget)
example_scatter = ExampleScatter()
self.add_widget(example_scatter)
#SCATTER IS GREEN, WIDGET IS RED
example_widget_animation = Animation(pos=(300,100), duration=2., t='in_bounce') + Animation(pos=(100,100), duration=2., t='in_bounce')
example_scatter_animation = Animation(pos=(300,300), duration=2., t='in_bounce') + Animation(pos=(100,300), duration=2., t='in_bounce')
example_widget_animation.repeat = True
example_scatter_animation.repeat = True
example_widget_animation.start(example_widget)
example_scatter_animation.start(example_scatter)
class BadAnimationExample(App):
def build(self):
root = ExampleScreen()
return root
if __name__ == "__main__":
BadAnimationExample().run()
【问题讨论】:
-
如果您正在为该小部件上存在的属性设置动画,则每个小部件都会正确设置动画。
-
我对小部件所做的是向它添加图像并调用 on_pos() 函数以在每次小部件位置发生如下变化时更改图像位置: self.image.pos = self.pos .我做错了什么?
-
发布一些演示问题的工作示例代码。我的第一个猜测是小部件 pos 永远不会改变,可能是因为它位于您尚未考虑的布局中,但如果没有完整的示例,就不可能知道。
-
我对小部件移动没有任何问题,我的问题是除了 scatter 之外的每个类的动画对象都完全忽略了过渡属性,这就是我所说的不正确的动画。
标签: python animation widget kivy scatter