5个解决方案
154 votes
您可以将动画侦听器添加到动画对象中
anim.setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
}
});
blessenm answered 2019-04-02T22:20:42Z
21 votes
功能与接受的答案相同,但更简洁:
/* Add/Remove any animation parameter */
theView.animate().alpha(0).setDuration(2000).withEndAction(new Runnable() {
@Override
public void run() {
theView.setVisibility(View.GONE);
}
});
请享用 :)
Antzi answered 2019-04-02T22:21:12Z
8 votes
您也可以使用Animation.setFillAfter实现此目的
Pavan answered 2019-04-02T22:21:36Z
8 votes
只需获取动画对象并添加动画侦听器即可。这是示例代码:
rotateAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
**// WRITE HERE WHATEVER YOU WANT ON THE COMPLETION OF THE ANIMATION**
}
});
Gaurav Arora answered 2019-04-02T22:22:00Z
0 votes
Kotlin的例子
var fadeOutImage = findViewById(R.id.fade_out_Image)
val fadeOutAnimation = R.anim.fade_out_animation
val animation = AnimationUtils.loadAnimation(this, fadeOutAnimation)
fadeOutImage.startAnimation(animation)
animation.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationStart(p0: Animation?) {
// not implemented
}
override fun onAnimationRepeat(p0: Animation?) {
// not implemented
}
override fun onAnimationEnd(p0: Animation?) {
fadeOutImage.visibility = View.INVISIBLE
}
})
richc answered 2019-04-02T22:22:24Z