Animations and setVisibility
I have a LinearLayout that I want to be able to show/hide by clicking on a "more details" link. I do this by calling
moreDetailsSection.setVisibility(View.VISIBLE);
or
moreDetailsSection.setVisibility(View.GONE);
to show/hide it. This works fine, but I wanted to add an animation that makes the layout fields slide in nicely, but this is only run the first time the field is made visible, if I hide it and show it again the field simply appears all of a sudden. Here is the animation code (moreDetailsSection is the Layout in question):
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(250);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(150);
set.addAnimation(animation);
LayoutAnimationController controller =
开发者_如何学Go new LayoutAnimationController(set, 0.25f);
moreDetailsSection.setLayoutAnimation(controller);
Any advice on how to make this run EACH time I show the layout and not only the first time?
I assume that the moreDetailsSection is INVISIBLE at first. you just have to create the Animation object and call the following code when the more details link is clicked.
moreDetailsSection.startAnimation(animation);
moreDetailsSection.setVisibility(View.VISIBLE);
You can use this line in your view for that layout:
android:animateLayoutChanges="true"
Use animation class
fun setVisibilityWithAnimation(textView:TextView, visibility: Int) {
if(visibility == View.VISIBLE){
val animation = ValueAnimator.ofFloat(0.0f, 1.0f)
animation.duration = 1000
animation.addUpdateListener {
textView.alpha = it.animatedValue as Float
}
animation.addListener(
onStart = {
textView.visibility = View.VISIBLE
}
)
}else if (visibility == View.GONE){
val animation = ValueAnimator.ofFloat(1.0f, 0.0f)
animation.duration = 1000
animation.addUpdateListener {
textView.alpha = it.animatedValue as Float
}
animation.addListener(
onEnd = {
textView.visibility = View.GONE
}
)
}
}
use this way:
setVisibilityWithAnimation(labelTextView,View.GONE)
精彩评论