如何在 AlphaAnimation 运行 时禁用按钮
How to disable button while AlphaAnimation running
我想在动画 运行 时禁用点击按钮。
代码如下:
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(4000);
anim.setRepeatMode(Animation.REVERSE);
btnTag.startAnimation(anim);
所以我想在动画完成之前不能点击按钮。
我通常使用 AnimationListener 完成这样的事情。它允许您 运行 在动画的各个阶段编写代码。
此代码未经测试,但看起来应该是这样的:
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(4000);
anim.setRepeatMode(Animation.REVERSE);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
btnTag.setClickable(false);
}
@Override
public void onAnimationEnd(Animation animation) {
btnTag.setClickable(true);
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
btnTag.startAnimation(anim);
不确定 btnTag 是您的按钮还是按住按钮的视图,但可以调用按钮的 setClickable(boolean clickable)
方法来启用和禁用按钮。
我想与那些使用数据绑定的人分享我为这个问题找到的解决方案:
binding.ivImage.setOnClickListener {
binding.ivImage.animate().apply {
duration=1000
scaleXBy(.5f)
scaleYBy(.5f)
rotationYBy(360f)
translationYBy(200f)
}.withEndAction {
binding.ivImage.animate().apply {
duration = 1000
scaleXBy(-.5f)
scaleYBy(-.5f)
rotationXBy(360f)
translationYBy(-200f)
}
}
}
binding.ivImage.animate().setListener(object: Animator.AnimatorListener{
override fun onAnimationStart(p0: Animator?) {
binding.ivImage.isClickable=false
binding.ivImage.isEnabled=false
}
override fun onAnimationEnd(p0: Animator?) {
binding.ivImage.isClickable=true
binding.ivImage.isEnabled=true
}
override fun onAnimationCancel(p0: Animator?) {
binding.ivImage.isClickable=true
binding.ivImage.isEnabled=true
}
override fun onAnimationRepeat(p0: Animator?) {
TODO("Not yet implemented")
}
})
我想在动画 运行 时禁用点击按钮。 代码如下:
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(4000);
anim.setRepeatMode(Animation.REVERSE);
btnTag.startAnimation(anim);
所以我想在动画完成之前不能点击按钮。
我通常使用 AnimationListener 完成这样的事情。它允许您 运行 在动画的各个阶段编写代码。
此代码未经测试,但看起来应该是这样的:
AlphaAnimation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(4000);
anim.setRepeatMode(Animation.REVERSE);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
btnTag.setClickable(false);
}
@Override
public void onAnimationEnd(Animation animation) {
btnTag.setClickable(true);
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
btnTag.startAnimation(anim);
不确定 btnTag 是您的按钮还是按住按钮的视图,但可以调用按钮的 setClickable(boolean clickable)
方法来启用和禁用按钮。
我想与那些使用数据绑定的人分享我为这个问题找到的解决方案:
binding.ivImage.setOnClickListener {
binding.ivImage.animate().apply {
duration=1000
scaleXBy(.5f)
scaleYBy(.5f)
rotationYBy(360f)
translationYBy(200f)
}.withEndAction {
binding.ivImage.animate().apply {
duration = 1000
scaleXBy(-.5f)
scaleYBy(-.5f)
rotationXBy(360f)
translationYBy(-200f)
}
}
}
binding.ivImage.animate().setListener(object: Animator.AnimatorListener{
override fun onAnimationStart(p0: Animator?) {
binding.ivImage.isClickable=false
binding.ivImage.isEnabled=false
}
override fun onAnimationEnd(p0: Animator?) {
binding.ivImage.isClickable=true
binding.ivImage.isEnabled=true
}
override fun onAnimationCancel(p0: Animator?) {
binding.ivImage.isClickable=true
binding.ivImage.isEnabled=true
}
override fun onAnimationRepeat(p0: Animator?) {
TODO("Not yet implemented")
}
})