ActivityOptions.makeSceneTransitionAnimation doesn't seem to exist

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-30 02:25:07

问题


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


来源:https://stackoverflow.com/questions/24517620/activityoptions-makescenetransitionanimation-doesnt-seem-to-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!