在Jelly Bean上使用overridePendingTransition时,屏幕会随机闪烁

时间:2013-03-01 17:21:44

标签: android animation transition

这个问题有时只发生在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但它没有帮助

5 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

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

 android:fillAfter="true"

答案 2 :(得分:2)

您是否有可能在设置的“开发者选项”部分中启用“不要保留活动”选项?

答案 3 :(得分:0)

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

@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

答案 4 :(得分:-1)

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

像这样:

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