I added animation during transitions between activites ... It’s just that one activates something like another ...

But I have activations that use FrameLayout and depending on what I need, I make this or that FrameLayout visible and the other I hide ...

but it looks not so beautiful ...

Is there any way to add animation when changing FrameLayout ?

I have 2 frames

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?

  • And how is your frame initially hidden which is not visible? if through setVisibility(View.GONE) , then first make it visible, and then animate the alpha, not forgetting before the animated transition to alpha 1f, first set the alpha to 0f - xkor
  • @xkor but is there a difference if I will install with or without f ... Does it automatically convert ...? - Aleksey Timoshchenko
  • Yes, in principle, it is not here, I just love when by the constant that you push the parameter type into the parameter you see. - xkor
  • @xkor and another such question ... Is there a difference in animation, if you animate through a file (create an animation object, specify a file and start this object through the startAnimation(animation) method) or do what you say? I mean that in your case we assign the alpha directly to the view element, and if we use the animation with the parameters specified through the file, will the final values ​​be assigned to the view element? That said it)) Not sure that you understood my point ... - Aleksey Timoshchenko
  • As far as I remember, if through Property Animation (res / animator / folder), they will be there, the properties of the view are animated directly. If through View Animation (res / anim / folder), then no, this is an outdated animation framework and everything is pretty sour. - xkor

1 answer 1

You can, for example, like this:

 frame1.animate().alpha(1f).start(); // то что показываем, изначально должно быть с альфой 0 frame2.animate().alpha(0f).start(); // то что скрываем 

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

 frame1.animate().x(0f).start(); // то что показываем, изначально должно быть скрыто за экраном frame2.animate().x(-frame2.getX()).start(); // то что скрываем 

In general, you probably need fragments to use instead of FrameLayout to switch between different UI options. There and simple animations of transitions between them can literally be included with a couple of lines of code (I don’t remember what the truth is).

  • It seems to work, but not to the end, I can not figure out why ... I supplemented the question, can you please see what I am doing wrong? - Aleksey Timoshchenko