【问题标题】:Why my close activity animation doesn't work on Android 4.0 (ICS)为什么我的关闭活动动画在 Android 4.0 (ICS) 上不起作用
【发布时间】:2016-08-29 15:02:49
【问题描述】:

我用自定义动画制作了一个主题(向上滑动和向下滑动)。 动画在较旧的 android 版本上运行良好.. 但是,当我在 Android 4.0 (ICS) 上试用时,关闭动画不起作用。只有上滑动画在 ICS 上运行良好。

这是我用于动画的主题:

<style name="myTheme" parent="android:Theme.Black">
    <item name="android:windowTitleSize">45dip</item>
    <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
    <item name="android:windowAnimationStyle">@style/myTheme.Window</item>
</style>

<style name="myTheme.Window" parent="@android:style/Animation.Activity">
    <item name="android:activityOpenEnterAnimation">@anim/push_up_in_no_alpha</item>
    <item name="android:activityOpenExitAnimation">@anim/no_anim</item>
    <item name="android:activityCloseEnterAnimation">@anim/no_anim</item>
    <item name="android:activityCloseExitAnimation">@anim/push_down_out_no_alpha</item>
</style>

这里是 push_down_out_no_alpha.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="0" android:toYDelta="100%p"
        android:duration="@android:integer/config_longAnimTime"/>
</set>

当我在代码中设置动画时,它在 ICS 上也可以正常工作,但为什么不作为主题呢?

 this.overridePendingTransition(R.anim.no_anim,R.anim.push_down_out_no_alpha);

有人知道为什么它不能在 Android 4.0 (ICS) 上运行吗?

【问题讨论】:

  • 你是在模拟器还是真机上试过?如果您在真实设备上尝试,您是否在两个不同的设备上使用 ICS 进行了检查?
  • 是的,我在 2 个不同的真实设备和模拟器上尝试过,都遇到了同样的问题。
  • 我在 View 而不是 Activity 上试了一下,效果很好。一定是我不太擅长的风格和主题。
  • 你在代码中试过了,效果很好,但为什么不作为主题呢?

标签: android animation themes android-4.0-ice-cream-sandwich


【解决方案1】:

从清单中指定动画似乎在 ICS 中被破坏:-( 覆盖动画解决方案工作正常,但您可能不想对动画进行硬编码。从清单中获取它们会很好,就像从其他版本的平台获取它们一样......所以......

向您的活动添加几个成员字段以保存附加到您的活动的动画的 ID。

protected int activityCloseEnterAnimation;
protected int activityCloseExitAnimation;

在你的 onCreate 中的某个地方...

// Retrieve the animations set in the theme applied to this activity in the
// manifest..
TypedArray activityStyle = getTheme().obtainStyledAttributes(new int[] {android.R.attr.windowAnimationStyle});
int windowAnimationStyleResId = activityStyle.getResourceId(0, 0);      
activityStyle.recycle();

// Now retrieve the resource ids of the actual animations used in the animation style pointed to by 
// the window animation resource id.
activityStyle = getTheme().obtainStyledAttributes(windowAnimationStyleResId, new int[] {android.R.attr.activityCloseEnterAnimation, android.R.attr.activityCloseExitAnimation});
activityCloseEnterAnimation = activityStyle.getResourceId(0, 0);
activityCloseExitAnimation = activityStyle.getResourceId(1, 0);
activityStyle.recycle();

那么无论您的活动完成/应该应用动画的地方包括...

overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation);

并且您的活动应该正确地遵循您在清单中附加到活动的主题/样式中设置的动画。

【讨论】:

    【解决方案2】:

    我也尝试过,但在这里不起作用。不知道是什么问题,但this.overridePendingTransition(R.anim.no_anim,R.anim.push_down_out_no_alpha); 这段代码工作正常

    【讨论】:

    • 我确实通过在每个活动中调用它来解决它。但仍然不知道为什么在 ICS 上的 xml 中不起作用..
    猜你喜欢
    • 2013-06-11
    • 2015-02-20
    • 2017-08-29
    • 2023-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2013-03-29
    相关资源
    最近更新 更多