你如何在 Kivy 中重置动画的位置
How do you reset the position of an Animation in Kivy
我正在尝试处理动画。我遇到的问题是我的动画播放一次但不重复。
我想做什么:我想让图像向上滑动,然后离开屏幕。然后出现在屏幕底部,再次向上滑动关闭。
实际发生了什么: 图像滑动 "up" 屏幕,从屏幕上消失,仅此而已。它不会重置回原来的位置,也不会重复。
我的问题是:我怎样才能重新设置图像的位置,以便它在一个永无止境的循环中继续(直到单击 "Stop Animation" 按钮) ?
动画对象是用.kv语言设置的Image,直接在MyScreen下
如果您想查看动画的播放方式,只需将 kv lang 中的图像名称 "binary_rain.png" 更改为 w/e 即可:)
import kivy
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, FallOutTransition
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.actionbar import ActionBar
from kivy.factory import Factory
from kivy.animation import Animation
class MyScreen(Screen):
def __init__(self, **kwargs):
super(MyScreen, self).__init__(**kwargs)
Clock.schedule_interval(self.example_func, 20.0)
self._test_bool = False
self.water = Animation(pos=(0, 800)) # It's setup in __init__ ; here
self.water &= Animation(size=(800, 800), duration=2, t='in_back') # and then here
def example_func(self, dt):
if self._test_bool == True:
self.ids.text_input_test_id.foreground_color = 255,0,0,1
self.ids.text_input_test_id.text = "The color has been changed"
self._test_bool = False
elif self._test_bool != True:
self.ids.text_input_test_id.foreground_color = 0, 255, 0, 1
self.ids.text_input_test_id.text = "The color has changed"
self._test_bool = True
def test_func(self):
if self._test_bool == True:
self.ids.text_input_2.foreground_color = 255,0,0,1
self.ids.text_input_2.text = "The color has been changed"
self._test_bool = False
elif self._test_bool != True:
self.ids.text_input_2.foreground_color = 0, 255, 0, 1
self.ids.text_input_2.text = "The color has changed"
self._test_bool = True
def test_func_two(self):
if self._test_bool == True:
self.ids.text_input_3.foreground_color = 255,0,0,1
self.ids.text_input_3.text = "The color has been changed"
self._test_bool = False
elif self._test_bool != True:
self.ids.text_input_3.foreground_color = 0, 255, 0, 1
self.ids.text_input_3.text = "The color has changed"
self._test_bool = True
def start_my_animation(self): # this is what starts the animation
self.water.repeat = True
self.water.start(self.ids.animated_bacground)
def stop_my_animation(self): # this is what should end the animation
self.water.stop(self.ids.animated_bacground)
class MyScreenManager(ScreenManager):
pass
root_widget = Builder.load_string('''
#:import FallOutTransition kivy.uix.screenmanager.FallOutTransition
#:import Config kivy.config
#:import Window kivy.core.window
#:import Clock kivy.clock
#:import ActionBar kivy.uix.actionbar
#:import Animation kivy.animation.Animation
MyScreenManager:
transition: FallOutTransition()
MyScreen:
<MyScreen>:
name: 'example_screen'
Image:
id: animated_bacground
size: self.size
pos: self.pos
source: 'binary_rain.png'
TextInput:
id: text_input_test_id
size_hint: .3, .05
pos_hint: {'x': .35, 'y': .85}
text: 'Hello'
font: '12sp'
TextInput:
id: text_input_2
size_hint: .3, .05
pos_hint: {'x': .35, 'y': .70}
text: 'Hello'
font: '12sp'
TextInput:
id: text_input_3
size_hint: .3, .05
pos_hint: {'x': .35, 'y': .65}
text: 'Button 3'
Button:
id: test_button_id
size_hint: .3, .05
pos_hint: {'x': .5, 'y': .5}
text: 'Click me'
on_press: root.test_func()
ActionBar:
pos_hint: {'top':1}
ActionView:
use_separator: True
ActionPrevious:
title: 'Menu'
with_previous: False
ActionOverflow:
ActionButton:
text: 'click me'
on_press: root.test_func_two()
ActionButton:
text: 'Start Animation'
on_press: root.start_my_animation()
ActionButton:
text: 'Stop Animation'
on_press: root.stop_my_animation()
ActionButton:
text: 'Button 2'
ActionButton:
text: 'Button 3'
ActionGroup:
text: 'Group1'
ActionButton:
text: 'Button 5'
ActionButton:
text: 'Btn6'
ActionButton:
text: 'Btn7'
''')
Factory.unregister('ActionPrevious')
class TestApp(App):
def build(self):
self.title = 'Example App'
return root_widget
TestApp().run()
您的动画设置的是尺寸,而不是位置...
您还使用 &= 同时制作两个动画,您需要 +=
self.water = Animation(pos=(0, 800))
# notice the changes * pos * and * += *
self.water += Animation(pos=(800, 800), duration=2, t='in_back') # and then here
我正在尝试处理动画。我遇到的问题是我的动画播放一次但不重复。
我想做什么:我想让图像向上滑动,然后离开屏幕。然后出现在屏幕底部,再次向上滑动关闭。
实际发生了什么: 图像滑动 "up" 屏幕,从屏幕上消失,仅此而已。它不会重置回原来的位置,也不会重复。
我的问题是:我怎样才能重新设置图像的位置,以便它在一个永无止境的循环中继续(直到单击 "Stop Animation" 按钮) ?
动画对象是用.kv语言设置的Image,直接在MyScreen下
如果您想查看动画的播放方式,只需将 kv lang 中的图像名称 "binary_rain.png" 更改为 w/e 即可:)
import kivy
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, FallOutTransition
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.actionbar import ActionBar
from kivy.factory import Factory
from kivy.animation import Animation
class MyScreen(Screen):
def __init__(self, **kwargs):
super(MyScreen, self).__init__(**kwargs)
Clock.schedule_interval(self.example_func, 20.0)
self._test_bool = False
self.water = Animation(pos=(0, 800)) # It's setup in __init__ ; here
self.water &= Animation(size=(800, 800), duration=2, t='in_back') # and then here
def example_func(self, dt):
if self._test_bool == True:
self.ids.text_input_test_id.foreground_color = 255,0,0,1
self.ids.text_input_test_id.text = "The color has been changed"
self._test_bool = False
elif self._test_bool != True:
self.ids.text_input_test_id.foreground_color = 0, 255, 0, 1
self.ids.text_input_test_id.text = "The color has changed"
self._test_bool = True
def test_func(self):
if self._test_bool == True:
self.ids.text_input_2.foreground_color = 255,0,0,1
self.ids.text_input_2.text = "The color has been changed"
self._test_bool = False
elif self._test_bool != True:
self.ids.text_input_2.foreground_color = 0, 255, 0, 1
self.ids.text_input_2.text = "The color has changed"
self._test_bool = True
def test_func_two(self):
if self._test_bool == True:
self.ids.text_input_3.foreground_color = 255,0,0,1
self.ids.text_input_3.text = "The color has been changed"
self._test_bool = False
elif self._test_bool != True:
self.ids.text_input_3.foreground_color = 0, 255, 0, 1
self.ids.text_input_3.text = "The color has changed"
self._test_bool = True
def start_my_animation(self): # this is what starts the animation
self.water.repeat = True
self.water.start(self.ids.animated_bacground)
def stop_my_animation(self): # this is what should end the animation
self.water.stop(self.ids.animated_bacground)
class MyScreenManager(ScreenManager):
pass
root_widget = Builder.load_string('''
#:import FallOutTransition kivy.uix.screenmanager.FallOutTransition
#:import Config kivy.config
#:import Window kivy.core.window
#:import Clock kivy.clock
#:import ActionBar kivy.uix.actionbar
#:import Animation kivy.animation.Animation
MyScreenManager:
transition: FallOutTransition()
MyScreen:
<MyScreen>:
name: 'example_screen'
Image:
id: animated_bacground
size: self.size
pos: self.pos
source: 'binary_rain.png'
TextInput:
id: text_input_test_id
size_hint: .3, .05
pos_hint: {'x': .35, 'y': .85}
text: 'Hello'
font: '12sp'
TextInput:
id: text_input_2
size_hint: .3, .05
pos_hint: {'x': .35, 'y': .70}
text: 'Hello'
font: '12sp'
TextInput:
id: text_input_3
size_hint: .3, .05
pos_hint: {'x': .35, 'y': .65}
text: 'Button 3'
Button:
id: test_button_id
size_hint: .3, .05
pos_hint: {'x': .5, 'y': .5}
text: 'Click me'
on_press: root.test_func()
ActionBar:
pos_hint: {'top':1}
ActionView:
use_separator: True
ActionPrevious:
title: 'Menu'
with_previous: False
ActionOverflow:
ActionButton:
text: 'click me'
on_press: root.test_func_two()
ActionButton:
text: 'Start Animation'
on_press: root.start_my_animation()
ActionButton:
text: 'Stop Animation'
on_press: root.stop_my_animation()
ActionButton:
text: 'Button 2'
ActionButton:
text: 'Button 3'
ActionGroup:
text: 'Group1'
ActionButton:
text: 'Button 5'
ActionButton:
text: 'Btn6'
ActionButton:
text: 'Btn7'
''')
Factory.unregister('ActionPrevious')
class TestApp(App):
def build(self):
self.title = 'Example App'
return root_widget
TestApp().run()
您的动画设置的是尺寸,而不是位置... 您还使用 &= 同时制作两个动画,您需要 +=
self.water = Animation(pos=(0, 800))
# notice the changes * pos * and * += *
self.water += Animation(pos=(800, 800), duration=2, t='in_back') # and then here