添加或删除对象时的动画
Animation on adding or removal of an object
当对象从布局 added/removed
时,如何使用可重复使用的方法为对象设置动画
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory
kv='''
<Image_1@BoxLayout>:
orientation:'vertical'
#id:img_1
Image:
source:"/storage/emulated/0/Download/download (37).jpeg"
Button:
text:"remove"
on_press: self.parent.remove()
BoxLayout:
orientation:'vertical'
GridLayout:
cols:1
id:sc_grid
Button:
size_hint:None,None
text:"add"
on_press:
app.Add()
'''
class MyApp(MDApp):
def build(self):
return Builder.load_string(kv)
def Add(self):
Image=Factory.Image_1()
Image.remove = lambda: self.root.ids.sc_grid.remove_widget(Image)
self.root.ids.sc_grid.add_widget(Image)
MyApp().run()
在上面的代码中,Add 方法将 Factory 对象 Image1 添加到布局中
您可以使用Animation
(参见documentation)。在 Image_1
class 中添加一些方法可以使它更容易。这是使用 Animation
:
的代码的修改版本
from kivy.animation import Animation
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivy.lang import Builder
kv = '''
<Image_1>:
orientation:'vertical'
#id:img_1
Image:
source:"/storage/emulated/0/Download/download (37).jpeg"
Button:
text:"remove"
on_press: self.parent.remove()
BoxLayout:
orientation:'vertical'
GridLayout:
cols:1
id:sc_grid
Button:
size_hint:None,None
text:"add"
on_press:
app.Add()
'''
class Image_1(BoxLayout):
def on_parent(self, *args):
if self.parent:
self.opacity = 0
anim = Animation(opacity=1)
anim.start(self)
def remove(self):
anim = Animation(opacity=0)
anim.start(self)
anim.on_complete = self.do_actual_remove
def do_actual_remove(self, widget):
self.parent.remove_widget(self)
class MyApp(MDApp):
def build(self):
return Builder.load_string(kv)
def Add(self):
Image = Image_1()
self.root.ids.sc_grid.add_widget(Image)
MyApp().run()
当对象从布局 added/removed
时,如何使用可重复使用的方法为对象设置动画
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory
kv='''
<Image_1@BoxLayout>:
orientation:'vertical'
#id:img_1
Image:
source:"/storage/emulated/0/Download/download (37).jpeg"
Button:
text:"remove"
on_press: self.parent.remove()
BoxLayout:
orientation:'vertical'
GridLayout:
cols:1
id:sc_grid
Button:
size_hint:None,None
text:"add"
on_press:
app.Add()
'''
class MyApp(MDApp):
def build(self):
return Builder.load_string(kv)
def Add(self):
Image=Factory.Image_1()
Image.remove = lambda: self.root.ids.sc_grid.remove_widget(Image)
self.root.ids.sc_grid.add_widget(Image)
MyApp().run()
在上面的代码中,Add 方法将 Factory 对象 Image1 添加到布局中
您可以使用Animation
(参见documentation)。在 Image_1
class 中添加一些方法可以使它更容易。这是使用 Animation
:
from kivy.animation import Animation
from kivy.uix.boxlayout import BoxLayout
from kivymd.app import MDApp
from kivy.lang import Builder
kv = '''
<Image_1>:
orientation:'vertical'
#id:img_1
Image:
source:"/storage/emulated/0/Download/download (37).jpeg"
Button:
text:"remove"
on_press: self.parent.remove()
BoxLayout:
orientation:'vertical'
GridLayout:
cols:1
id:sc_grid
Button:
size_hint:None,None
text:"add"
on_press:
app.Add()
'''
class Image_1(BoxLayout):
def on_parent(self, *args):
if self.parent:
self.opacity = 0
anim = Animation(opacity=1)
anim.start(self)
def remove(self):
anim = Animation(opacity=0)
anim.start(self)
anim.on_complete = self.do_actual_remove
def do_actual_remove(self, widget):
self.parent.remove_widget(self)
class MyApp(MDApp):
def build(self):
return Builder.load_string(kv)
def Add(self):
Image = Image_1()
self.root.ids.sc_grid.add_widget(Image)
MyApp().run()