我知道这是一个古老的问题,但今天我遇到了这个问题,我想表明我是如何解决它的,因为虽然已经发布的答案有帮助,但他们都没有完全适合我的情况。
在我的情况下,我创建了一个dinamically的自定义视图,应用了2个动画(alpha和translation)并在动画完成后删除了视图。这是我做的:
//Create fade out animation
AlphaAnimation fadeOut = new AlphaAnimation(1f, 0f);
fadeOut.setDuration(1000);
fadeOut.setFillAfter(true);
//Create move up animation
TranslateAnimation moveUp = new TranslateAnimation(0, 0, 0, -getHeight()/6);
moveUp.setDuration(1000);
moveUp.setFillAfter(true);
//Merge both animations into an animation set
AnimationSet animations = new AnimationSet(false);
animations.addAnimation(fadeOut);
animations.addAnimation(moveUp);
animations.setDuration(1000);
animations.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
//Hide the view after the animation is done to prevent it to show before it is removed from the parent view
view.setVisibility(View.GONE);
//Create handler on the current thread (UI thread)
Handler h = new Handler();
//Run a runnable after 100ms (after that time it is safe to remove the view)
h.postDelayed(new Runnable() {
@Override
public void run() {
removeView(view);
}
}, 100);
}
@Override
public void onAnimationRepeat(Animation animation) { }
});
view.startAnimation(animations);
请注意,这是一个自定义视图(其延伸的FrameLayout里),一切都在UI线程上运行内部完成。