【问题标题】:Screen flashes randomly when using overridePendingTransition on Jelly Bean在 Jelly Bean 上使用 overridePendingTransition 时屏幕随机闪烁
【发布时间】:2013-02-16 06:49:25
【问题描述】:

此问题仅在 Jelly Bean 4.1 和 4.2 上有时会发生(在 Galaxy Nexus 和 Nexus 4 上测试)。 这是我如何使用overridePendingTransition

什么时候开始一个新的Activity:

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 但没有帮助

【问题讨论】:

  • 问题出现在一个特定的 ANDROID 设备还是每个支持果冻豆的 Android 设备上?
  • 我不知道,因为除了 Galaxy Nexus 和 Nexus 4 之外,我没有很多设备要测试。
  • 但是你可以在不同操作系统版本的不同模拟器上试用
  • 尝试增加动画持续时间并检查是否可以重现闪光
  • 目前transition_duration = 400 ms,改成800后(非常非常慢)问题依旧。

标签: android animation transition


【解决方案1】:

您可以通过延迟 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()”。这种技术效果很好,解决了切换activity时的闪屏问题。

//
// 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

【讨论】:

  • 您建议的转换 API 已添加到 API v20 - Jelly Bean 是 API v16
【解决方案2】:

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

【讨论】:

  • 很高兴,如果您在其他论坛上发布了这个问题,也请在那里分享这个解决方案......
【解决方案3】:

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

像这样:

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

【讨论】:

【解决方案4】:

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

【讨论】:

  • 很抱歉“不保留活动”选项未启用。
【解决方案5】:

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

 android:fillAfter="true"

【讨论】:

  • 感谢您的回答,但这没有帮助,我认为 overridePendingTransition 的动画不需要 fillAfter。
  • fillBefore 那么呢?如果在绘制静态(非动画)视图之后绘制动画的第一帧,则可能会发生闪烁 - 例如,它可能发生在transition_left_to_right,因为目标视图首先绘制在屏幕上,然后开始动画开始后立即移出屏幕
  • FillBefore 也帮不上忙。
猜你喜欢
  • 2013-04-26
  • 2011-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多