ActivityOptions.makeSceneTransitionAnimation doesn

2019-02-05 06:09发布

问题:

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?

回答1:

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.



回答2:

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>


回答3:

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();
        }
    }