2010-08-03 78 views
8

我有2 ImageView,我從屏幕頂部翻譯到底部。這些視圖是從xml完成​​的,並且動畫是從java代碼添加的。動畫完美無缺,但我在java代碼中添加的onClickListener似乎不起作用。我使用動畫的fillAfter屬性來使iamges在翻譯後留在他們的到來,但這些圖像不再是可點擊的......但是,他們在翻譯前的位置仍然是可點擊的... 我看不到邏輯這個。任何人都可以給我一些關於這方面的建議嗎?TranslateAnimated ImageView動畫後無法點擊[Android]

+0

好吧,發現我的答案在這裏:http://stackoverflow.com/questions/2125694/android-button-doesnt-respond-after-animation現在我只需要正確設置它的參數來獲取它的位置即可...... – Sephy 2010-08-03 16:26:36

+0

但是,這似乎與setFillAfter不兼容。我添加刪除它來使其工作。 – Sephy 2010-08-03 17:21:56

+0

我有這個問題,經過一番研究,我解決了它。 [在這裏你可以找到答案] [1] [1]:http://stackoverflow.com/questions/18333269/how-to-set-button-clickable-while-animating-on -android/18509197#18509197 – SteBra 2013-09-12 12:03:52

回答

14

這是因爲動畫僅影響小部件的繪圖。但是,真正的位置不受影響 - 它仍然處於上一個位置。

爲了克服這個問題,你需要手動安裝一個動畫監聽器更新的ImageView的佈局參數如下:

Animation.setAnimationListener(new AnimationListener() { 
     public void onAnimationStart(Animation arg0) { 

     } 

     public void onAnimationRepeat(Animation arg0) { 
      //TODO Auto-generated method stub 
     } 

     public void onAnimationEnd(Animation arg0) { 
      android.widget.LinearLayout.LayoutParams params = new LayoutParams(
      android.widget.LinearLayout.LayoutParams.FILL_PARENT, 
      android.widget.LinearLayout.LayoutParams.WRAP_CONTENT); 
      params.topMargin = addLocationButton.getTop()-100; 

      ImageView.setLayoutParams(params); 
     } 
     }); 
+1

不好,因爲在onAnimationEnd後動畫中,當圖像從「setLayoutParams(params)」獲取位置時;「然後我看到一個閃爍。我補充一點,很難設置從屬性toYDelta – oscaroxy 2011-08-11 12:07:23

+4

要消除閃爍的百分比)TOPMARGIN可以使用\t \t \t \t \t \t \t animation.reset(; .clearAnimation(); – slott 2012-05-29 09:11:27