我有这个切换按钮,当它打开时,调用一个设置几个元素可见的动画方法。但是当我关闭它时,元素仍然可见,尽管相反的指令。如何让它们以同样的逻辑消失?我是否必须创建另一种方法?谢谢,这是代码:
drum.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
key1.setVisibility(View.VISIBLE);
key1.startAnimation(fadeInAnimation());
key2.setVisibility(View.VISIBLE);
key2.startAnimation(fadeInAnimation());
key3.setVisibility(View.VISIBLE);
key3.startAnimation(fadeInAnimation());
rocking.setLooping(true);
rocking.start();
Toast.makeText(getApplicationContext(), "Rock and Rolling!", Toast.LENGTH_SHORT).show();
} else {
rocking.setLooping(false);
key1.setVisibility(View.INVISIBLE);// These instrucions are ignored...
key2.setVisibility(View.INVISIBLE);
key3.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Can't keep up? Try the tamborine!", Toast.LENGTH_SHORT).show();
}
}
});
动画方法:
private Animation fadeInAnimation() {
Animation animation = new AlphaAnimation(0f, 1.0f);
animation.setDuration(1000);
animation.setFillEnabled(true);
animation.setFillAfter(true);
return animation;
}
答案 0 :(得分:3)
更改fadeInAnimation并传递一个布尔参数,如果为true则执行淡入淡出动画,否则淡出动画。代码示例如下。用法fadeInnimation(true)表示fadeIn动画,fadeAnimation(false)表示fadeOut动画。希望这会有所帮助。
private Animation fadeAnimation(boolean fadeIn) {
Animation animation = null;
if(fadeIn)
animation = new AlphaAnimation(0f, 1.0f);
else
animation = new AlphaAnimation(1.0f, 0f);
animation.setDuration(1000);
animation.setFillEnabled(true);
animation.setFillAfter(true);
return animation;
}
答案 1 :(得分:0)
结帐以下代码
viewObject.animate()
.alpha(0.0f)
.setStartDelay(10000)
.setDuration(2000)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
// do your stuff if any, after animation ends
}
}).start();
答案 2 :(得分:0)
View.setVisibility(View.INVISIBLE) does not work for animated view可能重复。只要即使取消动画也执行动画,也不会尊重可见性