【发布时间】:2016-02-21 18:54:21
【问题描述】:
通过使用此动画R.anim.anim_left_to_right,我成功地在我的活动中轻松完成了从右到左的幻灯片:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="100%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="300"/>
</set>
在Activity A中,我覆盖了pendingTransition:
overridePendingTransition(R.anim.anim_left_to_right, 0);
在 Activity B 中,在 onBackPressed 方法中,我像这样覆盖了 pendingTransition:
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(0, R.anim.anim_right_to_left);
}
R.anim.anim_right_to_left 看起来像这样:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0%" android:toXDelta="100%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="300" />
</set>
这种类型的动画的问题在于 Activity A 和 B 之间的动画非常平坦 - Activity B 从字面上看是在 Activity A 上滚动,并且在 Activity 中没有深度。我想在活动过渡时实现某种类型的深度,其中退出的活动 A 将在后台按比例缩小,而活动 B 将在其扩展时滑过它。当您退出 Activity B 时,Activity B 应该向左滑动,而 Activity A 会缩小到正常大小。
来自 Google 的视频实际上在 ViewPager 上演示了这一点,但我想为 *Activity** 制作类似的效果:
http://developer.android.com/training/animation/anim_page_transformer_depth.mp4
我试图把它放在Activity A中:
overridePendingTransition(R.anim.anim_left_to_right, R.anim.anim_scale_out);
这在活动 B 中:
overridePendingTransition(R.anim.anim_scale_in, R.anim.anim_right_to_left);
R.anim.anim_scale_out:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0.8"
android:toYScale="0.8"
android:duration="300"
android:fillBefore="false" />
</set>
R.anim.anim_scale_in:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="0.8"
android:fromYScale="0.8"
android:toXScale="1"
android:toYScale="1"
android:duration="300"
android:fillBefore="false" />
</set>
动画仍然没有给我活动的深度。 从活动 A 到 B 时,我需要活动 A 缩小,活动 B 从右向左滑动。从 Activity B 转到 A 时,我希望 Activity B 从左向右滑动,而 Activity A 向后缩放。
【问题讨论】:
-
您可以使用 Transition 类进行活动转换,但它已添加到 API 19 以及其他转换(如 Fade、Explode 和 Slide)中,但这些已添加到 API 21 检查此链接 link 这可能不是您正在寻找的答案。
-
不幸的是,这不是我要寻找的答案。我的应用程序的目标是 api 16。我不明白为什么这种效果很难做到,因为它在很多应用程序中都有使用。如果您查看 Facebook 应用程序,如果您从一项活动转移到另一项活动,您会看到这种效果。
标签: android animation android-xml android-transitions overridependingtransition