【问题标题】:Snackbar and other animations stopped working on some Android devicesSnackbar 和其他动画在某些 Android 设备上停止运行
【发布时间】:2016-05-14 02:40:36
【问题描述】:

我有一个非常奇怪的问题,我无法弄清楚。直到最近我才成为问题,但我似乎无法恢复以防止它发生。另一个奇怪的事情是它可以在某些设备上运行,而在其他设备上则不行。

问题是动画。特别是小吃店。小吃吧应该上下动画,但事实并非如此。它只是显示然后隐藏。查看下面的视频以查看问题。

Video of issue

这是用于为小吃栏设置动画的 Android 代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        ViewCompat.setTranslationY(mView, mView.getHeight());
        ViewCompat.animate(mView)
                .translationY(0f)
                .setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR)
                .setDuration(ANIMATION_DURATION)
                .setListener(new ViewPropertyAnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(View view) {
                        mView.animateChildrenIn(ANIMATION_DURATION - ANIMATION_FADE_DURATION,
                                ANIMATION_FADE_DURATION);
                    }

                    @Override
                    public void onAnimationEnd(View view) {
                        onViewShown();
                    }
                }).start();
    }

它使用 ViewCompat 作为 v4 库。我有其他动画在另一个活动中工作正常。此外,问题不仅仅是一项活动,而是所有活动。这让我觉得它的系统范围很广。但它们都使用不同的内部主题,但都扩展了 Theme.AppCompat.NoActionBar。

这是我的主要布局

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:elevation="4dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextAppearance="@style/ToolbarTitle"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways|snap"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabTextAppearance="@style/TabText"
        app:tabMinWidth="@dimen/tab_minwidth"
        app:tabMode="fixed"
        app:tabGravity="fill"
        app:layout_scrollFlags="enterAlways"/>


</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

<fr.castorflex.android.circularprogressbar.CircularProgressBar
    android:id="@+id/base_progressSpinner"
    android:layout_gravity="center"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:indeterminate="true"
    android:visibility="invisible"
    app:cpb_color="@color/spinner"
    app:cpb_rotation_speed="1.0"
    app:cpb_sweep_speed="1.0"
    app:cpb_stroke_width="4dp"
    app:cpb_min_sweep_angle="10"
    app:cpb_max_sweep_angle="300"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_upload"
    android:visibility="gone"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:layout_anchor="@id/content_frame"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="0dp"
    android:src="@drawable/app_fab_upload"
    android:layout_margin="@dimen/big_padding"
    android:clickable="true"
    app:backgroundTint="@color/fab_social"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_muzei"
    android:visibility="gone"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:layout_anchor="@id/content_frame"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="0dp"
    android:src="@drawable/app_fab_muzei"
    android:layout_margin="@dimen/big_padding"
    android:clickable="true"
    app:backgroundTint="@color/fab_social"/>

 </android.support.design.widget.CoordinatorLayout>

适用的设备

  • Nexus 9(棉花糖)
  • Nexus 4 (KitKat)
  • 银河 S7(棉花糖)

无法使用的设备

  • Droid Turbo 2(棉花糖)
  • Galaxy S7 (Marshmallow) *我的设备可以工作,我的测试人员不行
  • Nexus 6p (Android N)

其他动画问题与开关有关。我有 2 个布局相同,一个在切换时会卡顿,另一个只是在没有动画的情况下切换。

我还为我的 AppBarLayout 设置了一个 LayoutTransition 来动画我的 TabLayout 的隐藏/显示,并且它在所有设备上都可以正常工作

【问题讨论】:

  • 视频链接坏了
  • 在三星 J5 pro(牛轧糖)上也不起作用。
  • 所有的 Moto 阵容都有同样的问题。

标签: android android-support-library android-appcompat


【解决方案1】:

我找到了发生这种情况的原因,但还没有找到解决方法。

/**
 * Returns true if we should animate the Snackbar view in/out.
 */
private boolean shouldAnimate() {
    return !mAccessibilityManager.isEnabled();
}

这是由 Snackbar 类调用的,在工作设备上为 false,在不工作的设备上为 true。有人知道吗?

因此,在我在系统设置中禁用 lastpass 后,可访问性快餐栏现在可以正常显示动画。这太疯狂了。 Nova 发射器具有相同的效果。我猜任何启用的可访问性服务都会导致快餐栏动画不起作用。

【讨论】:

  • 我可以确认这也发生在我身上。启用 JoaoApps 和 Lastpass 的加入以实现可访问性和禁用两者都允许小吃栏动画。
  • 事实证明这显然是有意的行为,see here。至少可以说令人沮丧,特别是因为这意味着 Droid Turbo 用户必须禁用 Moto Voice 才能看到 Snackbar 动画。然而,我注意到的一件事是,谷歌收件箱应用程序中的 Snackbars 仍然是动画的——至少它是可能的,现在只是想弄清楚......
  • 有人找到解决方案了吗?
【解决方案2】:

正如 Bignadad 所提到的,问题在于任何可访问性功能,包括密码管理器等功能,都会禁用快餐栏动画。谷歌,截至本次编辑,has fixed this for AndroidX but not the Design support library

因为 Snackbar 的基类 BaseTransientBottomBar 使用包私有的 final 方法来处理动画,所以如果要修复它,您有两种选择:从头开始制作自己的小吃吧,或者使用更 hacky 的反射解决方案:

Kotlin 示例:

// Only force when necessary, and don't animate when TalkBack or similar services are enabled
val shouldForceAnimate = !accessibilityManager.isEnabled && accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_SPOKEN).isEmpty()

Snackbar.make(coordinatorLayout, text, duration).apply {
    if (shouldForceAnimate) {
        try {
            val accManagerField = BaseTransientBottomBar::class.java.getDeclaredField("mAccessibilityManager")
            accManagerField.isAccessible = true
            val accManager = accManagerField.get(this)
            AccessibilityManager::class.java.getDeclaredField("mIsEnabled").apply {
                isAccessible = true
                setBoolean(accManager, false)
            }
            accManagerField.set(this, accManager)
        } catch (e: Exception) {
            Log.d("Snackbar", "Reflection error: $e")
        }
    }
}.show()

Java 示例:

// Only force when necessary, and don't animate when TalkBack or similar services are enabled
boolean shouldForceAnimate = !accessibilityManager.isEnabled() && accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_SPOKEN).size == 0;

Snackbar snackbar = Snackbar.make(coordinatorLayout, text, duration);
if(shouldForceAnimate){
    try {
        Field accManagerField = BaseTransientBottomBar.class.getDeclaredField("mAccessibilityManager");
        accManagerField.setAccessible(true);
        AccessibilityManager accManager = (AccessibilityManager) accManagerField.get(snackbar);
        Field isEnabledField = AccessibilityManager.class.getDeclaredField("mIsEnabled");
        isEnabledField.setAccessible(true);
        isEnabledField.setBoolean(accManager, false);
        accManagerField.set(snackbar, accManager);
    } catch (Exception e) {
        Log.d("Snackbar", "Reflection error: " + e.toString());
    }
}
snackbar.show();

我喜欢这里的第三种选择,但我不知道有一种选择,至少在 AndroidX 通过适当的修复退出测试版之前是这样。

【讨论】:

  • 运行上面的代码后,您可以在所有其他 Snackbar 实例中直接调用 .show() ,而无需使用此反射。如果你已经运行了上面的反射,我建议你创建一个包装类来处理,然后调用 .show()
  • 我的应用程序是基于分析文本语音的辅助功能服务,所以这个动画不起作用真的很令人沮丧。这似乎是唯一的选择,谢谢。
  • “前进一步,后退两步” - Google
  • 除非您不介意破坏视障人士的体验,否则请勿执行此操作:一旦显示快餐栏,TalkBack 将停止工作,从而使应用对他们毫无用处。
【解决方案3】:

自 Material Components for Android 1.0.0-alpha3 以来已修复此问题,this commit

使用它而不是设计库(如果您使用的是AndroidX,这是要走的路):

implementation "com.google.android.material:material:$material_components_version"

【讨论】:

  • 只有在使用的无障碍服务不使用 FEEDBACK_SPOKEN 的情况下才会出现这种情况,但总比没有好!
猜你喜欢
  • 1970-01-01
  • 2023-03-04
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多