Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animations and setVisibility

Tags:

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 =
          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?

like image 246
Alexandru Cristescu Avatar asked Jun 30 '11 15:06

Alexandru Cristescu


People also ask

How to make view visible with animation android?

Crossfade the views For the view that is fading in, set the alpha value to 0 and the visibility to VISIBLE . (Remember that it was initially set to GONE .) This makes the view visible but completely transparent. For the view that is fading in, animate its alpha value from 0 to 1 .

How do you animate a view?

Create ImageView in the activity_main. xml along with buttons that will add animation to the view. Navigate to the app > res > layout > activity_main. xml.

What is crossfade animation?

Crossfade animations (also know as dissolve) gradually fade out one UI component while simultaneously fading in another. This animation is useful for situations where you want to switch content or views in your app. Crossfades are very subtle and short but offer a fluid transition from one screen to the next.

What is Animatelayoutchanges?

Android offers pre-loaded animation that the system runs each time you make a change to the layout. All you need to do is set an attribute in the layout to tell the Android system to animate these layout changes, and system-default animations are carried out for you.


2 Answers

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);
like image 101
wasaig Avatar answered Nov 07 '22 02:11

wasaig


You can use this line in your view for that layout:

android:animateLayoutChanges="true"
like image 25
Cabezas Avatar answered Nov 07 '22 04:11

Cabezas