Problem with transition animation between Fragments

Asked

Viewed 470 times

3

I’m working on an app that consists of several fragments and decided to put animations between them. Looking, I found that I should use the command setCustomAnimations to get the desired result, but I can only get two possible animations: fade-in and fade-out.

Searching the platform folders I’m using (android-19, or Android 4.4.2) saw that there are other animations that are not available on my SDK are present in the folder (fragment_close_enter/exit, fragment_fade_enter/exit, fragment_open_enter/exit).

Why isn’t my SDK recognizing them? This is the code I’m using to create the fragment:

FragmentTransaction transFrag = getFragmentManager().beginTransaction();
Fragment login = new LoginFragment();
transFrag.setCustomAnimations(android.R.animator.fade_in,
        android.R.animator.fade_out);
transFrag.addToBackStack(null);
transFrag.replace(R.id.container_login, login);
transFrag.commit();

1 answer

5


Why don’t you create your own animations using an xml with an objectAnimator?

Seven animations like this:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment login = new LoginFragment();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.container_login, login, "login");
ft.commit();

An example of the slide_in_left animation using the objectAnimator:

<?xml version="1.0" encoding="utf-8"?>
<set>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="x" 
    android:valueType="floatType"
    android:valueFrom="-1280"
    android:valueTo="0" 
    android:duration="500"/>
</set>

For the slide_out_right animation just invert the values. The two animations (slide_in_left and slide_out_right) are in the res/anim folder

  • I was thinking about it, but I don’t know exactly how to "convert" from anim for animator, because I realized there’s a difference in syntax. Is there anywhere I can read more about this?

  • Yes, objectAnimator works from API 11. I don’t know where you can read to "convert" from the old framework to the new one or vice versa. If not find, I advise to study both and understand what does what in each of them.

  • I got it. I managed to insert the animations I wanted just by correcting a few mistakes, so I don’t think I’ll need to convert the others. Thank you!

Browser other questions tagged

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