android动画结束,结束动画事件android

这篇博客提供了5种不同的方法来在Android中处理视图动画的开始、重复和结束事件。通过设置AnimationListener或者使用View.animate() API,开发者可以精确控制动画的生命周期,包括在动画结束后改变视图的可见性。此外,还提到了Animation.setFillAfter()方法的作用,并给出了Kotlin实现的示例。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值