tôi cuối cùng đã tìm thấy một giải pháp cho câu hỏi này. Nó hoạt động hoàn hảo.
Các thành phần quan trọng được sử dụng trong câu trả lời này:
- BackActivity hoạt động = nền
- FrontActivity = hoạt động trượt trước
- BackgroundView = các ViewGroup cơ sở trong FrontActivity bạn
Các giải pháp là animate bố cục của FrontActivity trước khi đóng FrontActivity. Điều này chỉ có thể xảy ra nếu bạn sử dụng thanh công cụ được tích hợp trong bố cục làm thanh tác vụ!
Tôi sẽ sao chép mã của mình tại đây. Hoạt ảnh của tôi là một hoạt động trượt ở phía trước từ phía dưới, và biến mất một lần nữa trượt ra phía dưới ở phía trước của hoạt động trước đó. Bạn có thể dễ dàng có được hiệu ứng này theo bất kỳ hướng nào bằng cách thay đổi hình động.
1) Trượt FrontActivity tại hơn BackActivity
Chỉ cần gọi overridePendingTransition
khi bắt đầu FrontActivity từ BackActivity.
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) Khi đi trở lại từ FrontActivity để BackActivity, animate bố trí từ FrontActivity ra trước khi bạn đóng FrontActivity!
Tôi đã làm điều này bằng cách gọi phương pháp sau đây trong onOptionsSelected()
tôi và onBackPressed()
tôi trong FrontActivity
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) Bây giờ chúng ta phải đảm bảo BackActivity hiển thị phía sau FrontActivity khi nó hoạt hình.
Chúng tôi cần làm việc với các chủ đề minh bạch cho việc này.
styles.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) Áp dụng các chủ đề trong suốt đối với FrontActivity trong biểu hiện của bạn:
AndroidManifest.xml
<activity
android:name=".FrontActivity"
android:theme="@style/Theme.Transparent"
android:parentActivityName=".BackActivity" />
5) Vì hoạt động của bạn bây giờ là trong suốt, bạn sẽ cần phải thêm một nền cho BackgroundView. Các nền tiêu chuẩn là:
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>
Vậy là xong. Hoạt ảnh phải hoạt động ngay bây giờ.
EDIT
Tôi đã tìm thấy một giải pháp mà không nhấp nháy. Các hình ảnh động trông hoàn hảo.
Khi đóng FrontActivity, hãy gọi finish
thay vì NavUtils.navigateUpFromSameTask(activity)
. Tôi đã thay đổi điều này trong câu trả lời của tôi.
Vấn đề với việc triển khai này ở đâu? – TheRedFox
@gauravsapiens bạn có tìm thấy câu trả lời cho câu trả lời này không? –