基本的にはアニメーションの終了後に何かをするには、前もってAnimationListenerをセットしておけば良いはず。
AnimationListener の onAnimationEnd というメソッドをオーバーライドして処理を書く事になる。
この onAnimationEnd の中でアニメーション対象のViewを親のViewから削除しようとして、次の様に書いたら parentView.removeView の所で エラーで落ちた。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final ScaleAnimation scale = new ScaleAnimation(1.0f, 0.1f, 1.0f, 0.1f, | |
Animation.RELATIVE_TO_SELF, 0.5f, | |
Animation.RELATIVE_TO_SELF, 0.5f); | |
final AlphaAnimation alpha = new AlphaAnimation(0.9f, 0.1f); | |
final AnimationSet set = new AnimationSet(true); | |
set.addAnimation(scale); | |
set.addAnimation(alpha); | |
set.setFillAfter(true); | |
set.setDuration(1500); | |
set.setAnimationListener(new AnimationListener() { | |
@Override | |
public void onAnimationEnd(final Animation animation) { | |
parentView.removeView(imgView); | |
}); | |
} | |
}); | |
imgView.setAnimation(animation); |
調べてみると、どうやら onAnimationEnd 内でUIの処理をするとまずいみたいだ。parentView.post() を呼んでUIスレッドに処理をRunnableの形で渡せば問題なく実行出来る様になった。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final ScaleAnimation scale = new ScaleAnimation(1.0f, 0.1f, 1.0f, 0.1f, | |
Animation.RELATIVE_TO_SELF, 0.5f, | |
Animation.RELATIVE_TO_SELF, 0.5f); | |
final AlphaAnimation alpha = new AlphaAnimation(0.9f, 0.1f); | |
final AnimationSet set = new AnimationSet(true); | |
set.addAnimation(scale); | |
set.addAnimation(alpha); | |
set.setFillAfter(true); | |
set.setDuration(1500); | |
set.setAnimationListener(new AnimationListener() { | |
@Override | |
public void onAnimationEnd(final Animation animation) { | |
parentView.post(new Runnable() { | |
@Override | |
public void run() { | |
parentView.removeView(imgView); | |
} | |
}); | |
} | |
}); | |
imgView.setAnimation(animation); |