2013-01-02 68 views
0

我試圖翻譯一個ImageView,每次點擊一次向下移動它。但是,該動畫僅適用於第一次點擊按鈕;所有後續點擊僅在沒有動畫的情況下更改ImageView的位置。點擊按鈕上的Animate ImageView

這裏是我的move_down.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<translate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromYDelta="0%" 
    android:toYDelta="100%" 
    android:duration="500" 
/> 

這裏是我的main.xml中我的按鈕聲明:

<Button 
    android:id="@+id/bGo" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Go" 
android:onClick="startAnimation" /> 

這裏是我的startAnimation功能:

public void startAnimation(View view) { 
     Animation test = AnimationUtils.loadAnimation(this, R.anim.move_down); 
     character.startAnimation(test);//This is the ImageView I'm trying to animate 
     test.setAnimationListener(new AnimationListener() { 
      public void onAnimationStart(Animation animation) {} 
      public void onAnimationRepeat(Animation animation) {} 
      public void onAnimationEnd(Animation animation) { 
       character.setY(character.getY() + character.getHeight());   
       } 
     });  
} 

當我註釋掉該行

character.setY(character.getY() + character.getHeight()); 

該動畫可以工作,但動畫完成後ImageView的位置會回彈。

回答

1

取出

character.setY(character.getY() + character.getHeight()); 

使用動畫的fillAfter屬性,使其留在它是當動畫結束的。

像這樣:

Animation test = AnimationUtils.loadAnimation(this, R.anim.move_down); 
test.setFillAfter(true); 
+0

這幾乎奏效。當我第二次按下該按鈕時,ImageView快速恢復到原始位置,而不是向下移動一步。 – Teresa

0

也許你應該嘗試這樣

public void startAnimation(View view) 
{ 
    Animation test = AnimationUtils.loadAnimation(this, R.anim.move_down); 
    character.startAnimation(test); 
    character.setVisibility(View.GONE);//when returns to original position, make it invisible 
    character.setY(character.getY() + character.getHeight());//move to new location 
    character.setVisibility(View.VISIBLE);//make it visible 
} 

動畫結束後,返回到原來的位置,所以你需要使它看不見,然後移動它到它移動到動畫中的新位置,然後使其可見。運行時,它應該顯示無縫。