13

此问题仅在 Jelly Bean 4.1 和 4.2 上发生(在 Galaxy Nexus 和 Nexus 4 上测试)。这是我的使用方法overridePendingTransition

什么时候开始一个新的活动:

Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
overridePendingTransition(R.anim.transition_right_to_left,
                    R.anim.transition_right_to_left_out);

当完成一个活动回到上一个

finish();
overridePendingTransition(R.anim.transition_left_to_right, R.anim.transition_left_to_right_out);

transition_left_to_right

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="-100%p"
android:toXDelta="0" 
android:duration="@integer/transition_duration"/>

transition_left_to_right_out

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXDelta="0"
android:toXDelta="100%p" 
android:duration="@integer/transition_duration"/>

transition_right_to_left

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="100%p"
android:toXDelta="0" 
android:duration="@integer/transition_duration"/>

transition_right_to_left_out

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="-100%p" 
android:duration="@integer/transition_duration"/>

这就是屏​​幕闪烁的方式:http: //youtu.be/TUKRz2yVF6Q(仅从 01:00 开始)

请告诉我您是否知道我的代码有什么问题以及为什么设备屏幕有时会闪烁?谢谢你。

编辑:尝试在 Jelly Bean 上使用 ActivityOptions 但没有帮助

4

5 回答 5

5

我有完全相同的问题。我能够通过将插值器更改为linear_interpolator而不是accelerate_decelerate_interpolator来解决它。

于 2013-03-20T23:40:27.577 回答
2

尝试将以下行添加到您的两个动画 XML 文件中

 android:fillAfter="true"
于 2013-03-04T04:17:15.997 回答
2

您是否有机会在设置的“开发人员选项”部分中打开“不保留活动”选项?

于 2013-03-08T18:46:53.170 回答
0

您可以通过延迟 Activity 转换并使用某些视图元素作为共享元素转换“锚”来解决此问题。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_match_score);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setEnterTransition(null);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Postpone the shared element enter transition.
        postponeEnterTransition();
    }
}

现在您只需要从应用程序中应在屏幕上显示所有内容的位置调用“scheduleStartPostponedTransition()”。这种技术效果很好,解决了切换活动时的屏幕闪烁问题。

//
// Schedules the shared element transition to be started immediately
// after the shared element has been measured and laid out within the
// activity's view hierarchy. Some common places where it might make
// sense to call this method are:
//
// (1) Inside a Fragment's onCreateView() method (if the shared element
//     lives inside a Fragment hosted by the called Activity).
//
// (2) Inside a Picasso Callback object (if you need to wait for Picasso to
//     asynchronously load/scale a bitmap before the transition can begin).
//
// (3) Inside a LoaderCallback's onLoadFinished() method (if the shared
//     element depends on data queried by a Loader).
//
public void scheduleStartPostponedTransition(final ImageView sharedElement) {
    LogHelper.v(TAG, "scheduleStartPostponedTransition");
    sharedElement.getViewTreeObserver().addOnPreDrawListener(
            new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    sharedElement.getViewTreeObserver().removeOnPreDrawListener(this);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        startPostponedEnterTransition();
                    }
                    return true;
                }
            });
}

http://www.androiddesignpatterns.com/2015/03/activity-postponed-shared-element-transitions-part3b.html

于 2017-02-10T02:10:14.280 回答
-1

愚蠢的建议,但您是否尝试过更改动画/完成()的顺序?

像这样:

overridePendingTransition(R.anim.transition_left_to_right, R.anim.transition_left_to_right_out);
finish();
于 2013-03-11T02:06:25.667 回答