Thanks to @xkor who suggested how you can make an animation for FrameLayout

You can, for example, like this:

frame1.animate (). alpha (1f) .start (); // what we show should initially be with alpha 0 frame2.animate (). alpha (0f) .start (); // what is hidden

This is through transparency, if you want to move something like this:

frame1.animate (). x (0f) .start (); // what we show should initially be hidden behind the screen frame2.animate (). x (-frame2.getX ()). start (); // what is hidden

I have 2 FrameLayout

here in this markup

 <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="60" android:orientation="horizontal"> <FrameLayout android:id="@+id/registrationFrame" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center|top" android:orientation="vertical"> <com.example.android.camera2basic.tools.fontView.ExButton style="@style/ExButton" android:layout_marginBottom="25dp" android:layout_marginTop="5dp" android:background="@drawable/stylelogbutton" android:onClick="RegMe" android:text="@string/registration_by_mail" android:textColor="@color/color_white" /> <com.example.android.camera2basic.tools.fontView.ExTextView style="@style/ExTextViewStyle" android:layout_width="200dp" android:layout_height="wrap_content" android:gravity="start" android:text="@string/agree" android:textColor="@color/color_white" android:textSize="15dp" /> </LinearLayout> <com.example.android.camera2basic.tools.fontView.ExTextView android:id="@+id/tvAutAct3" style="@style/ExTextViewStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:onClick="goToLogIn" android:text="@string/i_am_already_user" android:textColor="@color/color_white" /> </RelativeLayout> </FrameLayout> <FrameLayout android:id="@+id/loginFrame" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <com.example.android.camera2basic.tools.fontView.ExTextView android:id="@+id/tvLogAct3" style="@style/ExTextViewStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/log_in_by_e_mail" android:textColor="@color/color_white" /> <android.support.design.widget.TextInputLayout android:id="@+id/tilEmailLog" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginStart="16dp"> <com.example.android.camera2basic.tools.fontView.ExEditText android:id="@+id/etEmailLog" style="@style/ExTextViewStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="start" android:hint="@string/email" android:inputType="textEmailAddress" android:textColor="@color/color_white" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/tilPasswordLog" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginStart="16dp"> <com.example.android.camera2basic.tools.fontView.ExEditText android:id="@+id/etPasswordLog" style="@style/ExTextViewStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="start" android:hint="@string/password" android:inputType="textPassword" android:textColor="@color/color_white" /> </android.support.design.widget.TextInputLayout> <com.example.android.camera2basic.tools.fontView.ExButton android:id="@+id/bLogIn" style="@style/ExButton" android:layout_marginBottom="5dp" android:layout_marginTop="15dp" android:background="@drawable/stylelogbutton" android:onClick="userLogIn" android:text="@string/log_in" android:textStyle="bold" /> <com.example.android.camera2basic.tools.fontView.ExTextView style="@style/ExTextViewStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:onClick="forgotPassword" android:text="@string/forgot_your_password" android:textColor="@color/color_white" /> </LinearLayout> </FrameLayout> </LinearLayout> 

I have a button when I click on which frames change. Like this

 registrationFrame.animate().alpha(0).start(); loginFrame.animate().alpha(1).start(); 

But the problem is that when I install registrationFrame alpha(0) , it disappears beautifully, but nothing appears behind it ... The code does not react at all to the fact that loginFrame set to alpha(1) ...

I did it differently, I put only the function on one button

 registrationFrame.animate().alpha(0).start(); 

And on another button set the function

 loginFrame.animate().alpha(1).start(); 

I press on the first everything disappears, I press on the second nothing appears. ((

Then I tried the same function to put the same loginFrame loginFrame on these 2 buttons, only in the first case the function sets alpha(0) and in the second alpha(1) ...

Everything works for this frame, it disappears beautifully, then it appears.

But why does this work only for the same frame? Why is it impossible to make one disappear and the other appear?

  • Try a listener to end the animation on the hang up animation and at the end of the disappearance of the view make it visible to View.GONE - YuriySPb
  • one
    @ YuriySPb Thank you very much for the board. I screwed up this idea a bit and everything turned out. Now publish - Aleksey Timoshchenko

1 answer 1

In the end, that's how it all worked for me

 public static void switch(final Context context) { firstFrame.setVisibility(View.VISIBLE); firstFrame.setAlpha(1); Animation anim = AnimationUtils.loadAnimation(context, R.anim.alpha_frame_off); registrationFrameOff.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { firstFrame.setVisibility(View.GONE); secondFrame.setVisibility(View.VISIBLE); Animation animNext = AnimationUtils.loadAnimation(context, R.anim.open_next); secondFrame.startAnimation(animNext); } @Override public void onAnimationRepeat(Animation animation) { } }); firstFrame.startAnimation(anim); } 

It turns out the first frame when the animation ends. The animation for the second frame is called.