Android ActivityOptions.makeSceneTransitionAnimation doesn't seem to exist
Asked Answered
C

3

6

Android L introduced a new animations feature: animating between similar Views in different activities. It's documented here.

I've tried to use ActivityOptions.makeSceneTransitionAnimation, but it doesn't seem to be visible in the SDK (or in the jar at all), so I tried using reflection, and it returns a null value.

Has anyone else got it working?

Cleanser answered 1/7, 2014 at 19:11 Comment(1)
Say, does the support library have this cool feature?Misplay
C
10

Okay, I got it working.

It seems like setting the value in styles.xml is completely ignored for now.

You'll need to do this in each Activity's onCreate till that's fixed

getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
Transition transition = // load transition here.
getWindow().setSharedElementEnterTransition(transition);
getWindow().setSharedElementExitTransition(transition);

As per the same bug ViewAnimationUtils has, you'll see errors in Android Studio, it'll compile and run fine though.

Cleanser answered 1/7, 2014 at 19:42 Comment(4)
Yeah it's a bug that android-l components are shown in android studio as errors but run without any problem. I created an issue for that & they already fixed it yesterday. They will bring an update soon :)Jaenicke
Can u please share the code for loading the transition..? I can't find a full code example of shared element transitions done in activity..Chiles
How can you make it work with Listview ? @CleanserFetterlock
@Chiles TransitionInflater.from(context).inflateTransition(R.transition.your_transition));Tortuous
J
5

We can got it working with theme config for v21. Put these items into res/values-v21/styles.xml

 <item name="android:windowContentTransitions">true</item>
 <item name="android:windowAllowEnterTransitionOverlap">true</item>
 <item name="android:windowAllowReturnTransitionOverlap">true</item>
Janusfaced answered 5/7, 2014 at 14:44 Comment(3)
In which file did you define the above values? res/values/styles.xml? or res/values-v21/styles.xml? or somewhere else?Voluptuous
res/values-v21/styles.xmlJanusfaced
the third one is meant to be: <item name="android:windowAllowReturnTransitionOverlap">true</item>Shaina
S
2

Here is something work with 5.0 sdk got after 10/17/14.

But not sure what is the expected behavior if enable window content transitions and called setEnterTransition/setExitTransition in both mainActivity and secondActivity. If they are different (e.g one choose Explode and other choose Slide), which one would be applied?

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*
        To enable window content transitions in your code instead, call the Window.requestFeature() method:
         */
        getWindow().requestFeature(android.view.Window.FEATURE_CONTENT_TRANSITIONS);
        Transition ts = new Explode();  //Slide(); //Explode();

        ts.setStartDelay(2000);
        ts.setDuration(5000);

        /*
        If you have set an enter transition for the second activity,
        the transition is also activated when the activity starts.
         */

        getWindow().setEnterTransition(ts);
        getWindow().setExitTransition(ts);


        setContentView(R.layout.activity_main_view);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, mainViewFragment.newInstance())
                    .commit();
        }
    }

    public void launchSecondActivity() {

            /*
            If you enable transitions and set an exit transition for an activity,
            the transition is activated when you launch another activity as follows:
             */

            Intent listIntent = new Intent(this, secondActivity.class);
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());

        }

}

//===

public class secondActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ///
        getWindow().requestFeature(android.view.Window.FEATURE_CONTENT_TRANSITIONS);
        Transition ts = new Slide();  //Slide(); //Explode();

        ts.setDuration(3000);

        getWindow().setEnterTransition(ts);
        getWindow().setExitTransition(ts);

        ///

        setContentView(R.layout.activity_scene_transition);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }
Suggestibility answered 21/10, 2014 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.