在 Android 上更改活动动画的 z 顺序 [英] Change the z-order of activity animations on Android

查看:19
本文介绍了在 Android 上更改活动动画的 z 顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法反转活动动画的 z 顺序?基本上,当您开始新活动时,它会被添加到后台堆栈中,并且它的窗口会添加到前一个活动的窗口上.有时稍后返回时,从后台堆栈中获取的活动会在顶部活动下方显示其窗口.大多数情况下它是完全有道理的,但我对这种行为有以下问题.

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.

我有一个小部件,它指向应用程序结构的深处.因此,当用户单击操作栏中的向上按钮时,我必须手动创建后退堆栈.但后来我实际上调用 startActivity ,新意图指向一个新的活动,并附有后堆栈..在这里我们解决问题..活动实际上是从顶部动画,而不是像往常一样从后堆栈弹出动画标准方式.

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

这是我如何重新创建任务返回堆栈并将用户导航到父活动的代码片段.同样在下方,您可以看到动画的屏幕截图.基本上,顶部的窗口应该在底部(在应用程序层次结构中较高).仅供参考,所有这些动画都是通过标准 XML 窗口动画完成的,并通过 overridePendingTransition 调用触发.我试图在这些动画中修改 z 调整,但它似乎只影响一个特定动画中的各个层..

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

}

推荐答案

老问题,但我也遇到了同样的问题.解决方案是在我们的动画定义中添加属性 android:zAdjustment="top"android:zAdjustment="bottom".示例:

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

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

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

这篇关于在 Android 上更改活动动画的 z 顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆