Animate a LinearLayout with the LayoutAnimationController

1
Good afternoon! Home I have a LinearLayout that will be displayed when the user selects a Switch For this I have created two methods, for fadeIn and fadeOut. Following:

private LayoutAnimationController fadeIn(){
    Animation fadeIn = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
    AnimationSet set=new AnimationSet(true);
    fadeIn.setDuration(155);
    set.addAnimation(fadeIn);
    set.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            mLayout.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animation animation) {}
        @Override
        public void onAnimationRepeat(Animation animation) {}
    });
    return  new LayoutAnimationController(set);
}

private LayoutAnimationController fadeOut(){
    AnimationSet set=new AnimationSet(true);
    Animation fadeOut = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out);
    fadeOut.setDuration(255);
    set.addAnimation(fadeOut);
    set.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) { }

        @Override
        public void onAnimationEnd(Animation animation) {
            mLayout.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {}
    });
    return  new LayoutAnimationController(set);
}

The problem occurs in fadeIn , when you try to run onAnimationStart(Animation animation) mLayout.setVisibility(View.VISIBLE); } Layout is not displayed!

Would anyone know the right way to do this? Home From now on, greetings!

    
asked by anonymous 08.07.2015 / 20:14

1 answer

2

You can use android: animateLayoutChanges="true" in your parent layout. this will make the fadeIn and fadeOut animations of all child views, when you put them as VISIBLE , GONE , INVISIBLE and views added or removed.

Note: it works from Android 3.0

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            android:orientation="horizontal" >

    <Switch
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <LinearLayout
            android:id="@+id/layoutFilho"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:visibility="gone" >

            <Button

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TEXTO" />

        </LinearLayout>
</LinearLayout>
    
09.07.2015 / 16:21