Animate Linearlayout with Layoutanimationcontroller

Asked

Viewed 125 times

1

Good Afternoon!
I have a Linearlayout that will be displayed when the user selects a Switch
For this I created two methods, for fadein and fadeOut. Follow:

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 the fadein , when trying to execute the onAnimationStart(Animation animation) mLayout.setVisibility(View.VISIBLE); } The Layout is not displayed!

Someone would know the right way to do it?
From now on agardeço, Greetings!

1 answer

2


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

Note: 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.