我终于找到了这个问题的解决方案。它工作完美。
该答案中使用的重要组成部分:
- BackActivity = 后台活动
- FrontActivity = 前面滑动的 Activity
- BackgroundView = FrontActivity 中的基本 ViewGroup
解决方案是在关闭 FrontActivity 之前将 FrontActivity 的布局动画化。仅当您使用布局中集成的工具栏作为操作栏时,这才有可能!
我将在这里复制我的代码。我的动画是一个从底部滑到前面的activity,在上一个activity前面再次滑出到底部消失。只需更改动画,您就可以轻松地在任何方向获得这种效果。
1)将FrontActivity滑过BackActivity
只需致电overridePendingTransition
从 BackActivity 启动 FrontActivity 时。
Intent intent = new Intent(activity, FrontActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.slide_in_bottom, 0);
Slide_in_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%p"
android:toYDelta="0%p"
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:anim/decelerate_interpolator">
</translate>
2)从FrontActivity返回到BackActivity时,在关闭FrontActivity之前将FrontActivity中的布局动画化!
我通过在我的中调用以下方法来做到这一点onOptionsSelected()
and my onBackPressed()
在前面的活动
private void animateOut() {
Animation slideAnim = AnimationUtils.loadAnimation(this,R.anim.slide_out_bottom);
slideAnim.setFillAfter(true);;
slideAnim.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation paramAnimation) { }
public void onAnimationRepeat(Animation paramAnimation) { }
public void onAnimationEnd(Animation paramAnimation) {
finish();
// if you call NavUtils.navigateUpFromSameTask(activity); instead,
// the screen will flicker once after the animation. Since FrontActivity is
// in front of BackActivity, calling finish() should give the same result.
overridePendingTransition(0, 0);
}
});
BackgroundView.startAnimation(slideAnim);
}
Slide_out_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%p"
android:toYDelta="100%p"
android:duration="@android:integer/config_mediumAnimTime"
android:interpolator="@android:anim/accelerate_interpolator">
</translate>
3) 现在我们必须确保 BackActivity 在 FrontActivity 动画结束时在 FrontActivity 后面可见。
为此,我们需要使用透明的主题。
样式.xml
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- your theme -->
</style>
<style name="Theme.Transparent" parent="AppTheme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
</resources>
4) 将透明主题应用到清单中的 FrontActivity:
AndroidManifest.xml
<activity
android:name=".FrontActivity"
android:theme="@style/Theme.Transparent"
android:parentActivityName=".BackActivity" />
5) 因为您的活动现在是透明的,所以您需要向BackgroundView 添加背景。标准背景是:
android:background="@android:color/background_light"
android:background="@android:color/background_dark"
front_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This is BackgroundView and can be any ViewGroup -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@android:color/background_light" >
<android.support.v7.widget.Toolbar
android:layout_height="@dimen/height_toolbar"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<!-- rest of layout -->
</FrameLayout>
就是这样。动画现在应该可以运行了。
EDIT
我找到了一个不闪烁的解决方案。动画看起来完美无缺。
关闭FrontActivity时,调用finish
代替NavUtils.navigateUpFromSameTask(activity)
。我在答案中改变了这一点。