我試圖翻譯一個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的位置會回彈。
這幾乎奏效。當我第二次按下該按鈕時,ImageView快速恢復到原始位置,而不是向下移動一步。 – Teresa