简体   繁体   中英

Android view visibility after animation

I have 2 simple animation, fade_in.xml and fade_out.xml . I want login form to be invisible when click login button. And I want it to be visible after when request finished. First part works as I expected, but when request finished, before animation works and login form is invisible.

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromAlpha="0.0"
android:toAlpha="1.0" />

This is fade in and fade out xml is

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromAlpha="1.0"
android:toAlpha="0.0" />

My animation methods are

void animateStart() {
    Animation animationFadeOut;
    animationFadeOut = AnimationUtils.loadAnimation(context, R.anim.fade_out);
    animationFadeOut.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

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

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    formLayout.startAnimation(animationFadeOut);
}

and

void animateFinish() {
    Animation animationFadeIn;
    animationFadeIn = AnimationUtils.loadAnimation(context, R.anim.fade_in);
    animationFadeIn.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            formLayout.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    formLayout.startAnimation(animationFadeIn);
}

My form as xml

<LinearLayout
    android:id="@+id/container_login_form"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:animateLayoutChanges="true"
    android:layout_centerVertical="true"
    android:orientation="horizontal">..</LinearLayout>

And screen 登入画面

How can I fix it?

Remove the animation listeners and add setFillAfter(true) to your animations like this

void animateStart() {
    Animation animationFadeOut;
    animationFadeOut = AnimationUtils.loadAnimation(context, R.anim.fade_out);
    animationFadeOut.setFillAfter(true);
    formLayout.startAnimation(animationFadeOut);
}

void animateFinish() {
    Animation animationFadeIn;
    animationFadeIn = AnimationUtils.loadAnimation(context, R.anim.fade_in);
    animationFadeIn.setFillAfter(true);
    formLayout.startAnimation(animationFadeIn);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM