我想单击Button
,它将显示TextView
是可见的(执行向下滑动动画),然后再次单击按钮,它将执行另一个动画(向上滑动)。之后,无需显示TextView
。
我如何解决它?
请任何人有一个答案来帮助我。
bclickss.setOnClickListener(new View.OnClickListener() {
boolean visible;
@Override
public void onClick(View v) {
if( visible = !visible) {
tv2.setVisibility(visible ? View.VISIBLE : View.GONE);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
tv2.startAnimation(anim);
}
else {
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
tv2.startAnimation(anim);
tv2.setVisibility(View.GONE);
}
}
});
答案 0 :(得分:2)
在startAnimation
之前使用此
if (tv2.animation != null) tv2.animation.setAnimationListener(null)//needed not in all cases
tv2.clearAnimation()
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
tv2.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
tv2.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
tv2.startAnimation(anim);
在第二个动画中,您需要这样的内容:
if (tv2.animation != null) tv2.animation.setAnimationListener(null)//needed not in all cases
tv2.clearAnimation()
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
tv2.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {}
});
tv2.startAnimation(anim);
答案 1 :(得分:1)
try this:
//You can add Animation listener to your animation object like
anim .setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
tv2.setVisibility(View.GONE);
}
});
答案 2 :(得分:1)
在将可见性设置为clearAnimation
之前,您应该使用View.GONE
anim .setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
tv2.clearAnimation();
tv2.setVisibility(View.GONE);
}
});
答案 3 :(得分:0)
我建议您使用YOYO Animation lib for android。 它提供了丰富的动画以及回调方法,因此一旦动画在回调中完成,您就可以隐藏textview
答案 4 :(得分:-1)
我也有同样的问题。请遵循以下几行:
Animation slide_up = AnimationUtils.loadAnimation(this, R.anim.slide_up);
Animation slide_down = AnimationUtils.loadAnimation(this, R.anim.slide_down);
使视图可见:
view.setVisibility(View.VISIBLE);
view.startAnimation(slide_down);
隐藏它:
view.startAnimation(slide_up);
view.setVisibility(View.GONE);
如果可行,请接受答案。