Change the z-order of activity animations on Android

寵の児 提交于 2019-12-06 05:52:49

问题


Is there a way how to invert the z-order of activity animations? Basically when you start new activity it is added to the backstack and its window is added over the window of the previous activity. When returning back sometimes later the activity which which is taken from the backstack show its window underneath the top activity. Most time it makes completely sense, but I have a following issue with that behavior.

I have a widget which points somewhere deep into the app structure. So I have to manually create the back stack when the user clicks on the up button in action bar. But then I actually call startActivity with new intent pointing at a new activity with back stack attached to it..and here we go to the issue..The actvity actually animates from the top, instead of being animated as usual when poping from back stack the standard way.

you can see the flow on the following picture

here is a code snippet of how I am recreating the task back stack and navigating the user to the parent activity. also way below you can see a screenshot of the animation..Basically the window which is on top should be on bottom (it is higher in the app hiearchy). FYI all those animations are done via standard XML window animations and fired with overridePendingTransition calls. I tried to modify z-adjustment within those animations, but it seems like it only does affect various layers in one particular animation..

if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
       NavUtils.navigateUpTo(this, upIntent);
} else {
      TaskStackBuilder.create(this)
                        .addNextIntentWithParentStack(upIntent)
                        .startActivities();

}


回答1:


Old question, but I've had the same problem. The solution is to add the attribute android:zAdjustment="top" or android:zAdjustment="bottom" in our animation definitions. Example:

nothing_background.xml

<?xml version="1.0" encoding="utf-8"?>
  <translate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:zAdjustment="bottom"
    android:duration="400"
    android:fromXDelta="0%p"
    android:toXDelta="0%p" >
  </translate>

fade_in_foreground.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:zAdjustment="top">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:duration="400" />
</set>

In you code that triggers the transition:

Intent intent = new Intent(FromActivity.this, ToActivity.class);
Bundle animate = ActivityOptions.makeCustomAnimation(getApplicationContext(),
  R.anim.nothing_background, R.anim.fade_in_foreground).toBundle();
startActivity(intent, animate);


来源:https://stackoverflow.com/questions/19630576/change-the-z-order-of-activity-animations-on-android

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