I used some code I found for a sliding panel, and basically it works but has a small issue.
The animation doesn’t work on the first time the panel gets open.
here’s the code for the animation:
TranslateAnimation anim = null;
m_isOpen = !m_isOpen;
if (m_isOpen) {
setVisibility(View.VISIBLE);
anim = new TranslateAnimation(0.0f, 0.0f, getHeight(), 0.0f);
} else {
anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, getHeight());
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation animation) {
// not needed
}
public void onAnimationStart(Animation animation) {
// not needed
}
});
}
anim.setDuration(300);
anim.setInterpolator(new AccelerateInterpolator(1.0f));
startAnimation(anim);
why on the first I open the panel there’s no animation but all the other ones there is?
When are you calling this? It is probably only doing it “after the first time” because the panel has not been rendered yet when you first call it, and
getHeight()is returning 0. Try waiting untilgetHeight()has a value and start the animation then. You can also try to hardcode the value to something just to test that my theory is right.