2015-09-02 39 views
-1

我正在嘗試爲屏幕的某個區域設置動畫。包括圖像在內的整個背景在Kivy都有動畫效果。我如何只動畫圖像?

start_screen.py

import kivy 
kivy.require('1.9.0') 

from kivy.app import App 
from kivy.uix.floatlayout import FloatLayout 
from kivy.clock import Clock 
from functools import partial 
from kivy.uix.behaviors import ButtonBehavior 
from kivy.uix.image import Image 
from kivy.animation import Animation 
from kivy.core.window import Window 
import time 

def set_color(button, color, *args): 
    button.color = color 

class ImageButton(ButtonBehavior, Image): 
    pass 

class StartScreen(FloatLayout): 
    def __init__(self, **kwargs): 
     super(StartScreen, self).__init__(**kwargs) 

     def reset_color(*args): 
      Clock.schedule_once(partial(set_color, 
       self.ids.pstart_button, 
       (1,1,1,1)), 1) 
      Clock.schedule_once(partial(set_color, 
       self.ids.pstart_button, 
       (0,1,0,1)), 2) 
     Clock.schedule_interval(reset_color, 3) 

    #def on_touch_down(self, touch): 
     #if self.collide_point(*touch.pos): 
      #self.to_menu() 

    def to_menu(self): 
     sb=self.ids.pstart_button 
     anim = Animation(pos =(Window.width * .05, Window.height *.60)) 
     anim &= Animation(size =(Window.width * .15, Window.height * .15)) 
     anim.start(sb) 

class TestApp(App): 
    def build(self): 
     return StartScreen() 

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

test.kv

#:kivy 1.9 
<StartScreen>: 
    orientation: 'horizontal' 
    size: root.size 
    canvas.before: 
     Rectangle: 
      pos:self.pos 
      size: self.size 
      source: 'bg.png' 

    FloatLayout: 
     size: root.size 
     orientation: 'horizontal' 

     ImageButton: 
      id: pstart_button 
      size_hint: (.30,.30) 
      pos: root.center_x - (self.width/2), root.center_y - (self.height/2) 
      source: 'start_button.png' 
      on_touch_down: if pstart_button.collide_point(pstart_button.x, pstart_button.y): root.to_menu() 

<MenuScreen>: 
    orientation: 'horizontal' 
    size: root.size 
    canvas.before: 
     Rectangle: 
      pos: self.pos 
      size: self.size 
      source: 'bg2.png' 

    FloatLayout: 
     size: root.size 
     orientation: 'horizontal' 
     ImageButton: 
      id: start_button 
      size_hint: (.15, .15) 
      pos: root.width * .05, root.height *.60 
      source: 'start_button.png' 


     ImageButton: 
      id:usb_button 
      size_hint: (None, None) 
      width: start_button.width * .80 
      height: start_button.height * .80 
      x: start_button.width *.75 
      y: start_button.y - (start_button.height * .99) 
      source: r'usb_button.png' 

     ImageButton: 
      id:remote_button 
      size_hint: (None, None) 
      width: start_button.width * .80 
      height: start_button.height * .80 
      x: start_button.width *.75 
      y: usb_button.y - (start_button.height * .99) 
      source: r'remote_button.png' 

     ImageButton: 
      id:sdcard_button 
      size_hint: (None, None) 
      width: start_button.width * .80 
      height: start_button.height * .80 
      x: start_button.width *.75 
      y: remote_button.y - (start_button.height * .99) 
      source: r'sdcard_button.png' 

     ImageButton: 
      id:home_button 
      size_hint: (.1,.1) 
      pos: root.width * .73, root.height *.88 
      source: 'home_button.png' 

     ImageButton: 
      id:settings_button 
      size_hint: (.1, .1) 
      pos: home_button.x +(home_button.width * .89), root.height * .88 
      source: 'settings_button.png' 

     ImageButton: 
      id:back_button 
      size_hint: (.1, .1) 
      pos: settings_button.x +(settings_button.width * .89), root.height * .88 
      source: 'back_button.png' 

的問題是當我嘗試動畫和使用 「自我」 它結束了我的動畫背景的一起小部件。我只是想要動畫的部件。我想我確定爲什麼自我移動整個事情,因爲自我是整個小部件佈局。我的代碼顯示了我剛剛嘗試使小部件動起來的位置,但它不起作用。

另外碰撞點似乎也沒有工作。如果我點擊屏幕上的任何地方,它會動畫。我有一種感覺,如果我可以讓圖像移動,那麼它也可以解決這個問題。

回答

1

我需要分離功能。我試圖在主佈局中設置動畫效果。我應該做的是將小部件(和任何小部件)的功能放在它自己的類中。菜鳥的錯誤。我將to_menu()移到了ImageButtton類中,它工作。當我將行爲移動到按鈕時,碰撞點也起作用。