2014-06-06 32 views
1

這是我的代碼到目前爲止。所以當我點擊圖片時,它會按預期移動。涼。但是當我再次單擊圖像時,它不會移回原始位置。有人可以幫忙嗎?僅供參考,變量「取決於」處於全球範圍。謝謝!如何在調用圖像後返回圖像?

depends = false; 

image.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     if (depends){ 
      TranslateAnimation noob = 
       new TranslateAnimation(0, 0, 0, +90); 

      noob.setDuration(1000); 
      noob.setFillAfter(true); 
      image.startAnimation(noob); 
     } else{ 
      TranslateAnimation translateAnimation = 
       new TranslateAnimation(0, 0, 0, -90); 
      depends = true; 
      translateAnimation.setDuration(1000); 
      translateAnimation.setFillAfter(true); 
      image.startAnimation(translateAnimation); 
     } 
    } 
}); 

回答

1

這是因爲舊的動畫API只會翻譯View的Drawable,而不是View本身(及其觸摸區域)。如果你點擊其中原始圖像「是」,你應該看到它搬回

解決方案:使用新的API Animatorherenineoldandrois回兼容性

+0

你是對的!那麼更好的解決方案是什麼? – user3683815

+0

我更新了我的答案。除了我的頭頂,沒有更多的上下文,我會說類似以下內容:'ObjectAnimator anim = ObjectAnimator.ofFloat(image,「translationY」,0.f,90.f); anim.setDuration(1000); anim.start();' –

0

只是在黑暗中拍攝。如果這沒有幫助,我會刪除我的答案。

  if (depends){ 
       TranslateAnimation noob = new TranslateAnimation(0, 0, 0, +90); 



       noob.setDuration(1000); 

       noob.setFillAfter(true); 

       image.startAnimation(noob); 

       depends = false; <-- ADD THIS 
      } else{ 
       TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, -90); 

        depends = true; 

       translateAnimation.setDuration(1000); 

       translateAnimation.setFillAfter(true); 

       image.startAnimation(translateAnimation); 
      } 
+1

什麼也沒發生。欣賞嘗試壽! – user3683815

+0

我是否至少在假設你的「else」塊沒有被擊中時正確? – bstar55

+1

是的,你是。實際上,我沒有告訴你我在初始範圍初始化依賴布爾變量爲false。見修訂。 – user3683815

相關問題