Snackbar和其他动画在一些Android设备上停止工作
我有一个很奇怪的问题,我想不出来。直到最近我才成为一个问题,但我似乎无法回头阻止它。另一件奇怪的事情是,它在某些设备上工作,而在其他设备上则不工作 问题是动画。特别是快餐店。snackbar应该上下设置动画,但它不是。它只是显示然后隐藏。查看下面的视频以查看问题 以下是Android代码,用于在中为snackbar设置动画Snackbar和其他动画在一些Android设备上停止工作,android,android-support-library,android-appcompat,Android,Android Support Library,Android Appcompat,我有一个很奇怪的问题,我想不出来。直到最近我才成为一个问题,但我似乎无法回头阻止它。另一件奇怪的事情是,它在某些设备上工作,而在其他设备上则不工作 问题是动画。特别是快餐店。snackbar应该上下设置动画,但它不是。它只是显示然后隐藏。查看下面的视频以查看问题 以下是Android代码,用于在中为snackbar设置动画 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { View
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(棉花糖)*我的设备工作正常,我的测试人员不工作
- Nexus 6p(安卓N)
我的AppBarLayout中还设置了一个LayoutTransition,用于对我的表格布局的隐藏/显示进行动画处理,它工作正常,所有设备都正常。我找到了发生这种情况的原因,但还没有找到修复方法
/**
* Returns true if we should animate the Snackbar view in/out.
*/
private boolean shouldAnimate() {
return !mAccessibilityManager.isEnabled();
}
这被Snackbar类调用,在工作设备上为false,在不工作的设备上为true。有人知道这件事吗
因此,在我在系统设置中禁用了lastpass之后,snackbar现在将按其应有的方式设置动画。这太疯狂了。新星发射器也有同样的效果。我想,在可访问性中启用的任何服务都会导致snackbar动画无法工作。正如Bignadad提到的,问题是任何可访问性功能,包括密码管理器之类的功能,都会禁用snackbar动画。谷歌,截至本次编辑 因为Snackbar的基类BaseTransientBottomBar使用包私有的final方法处理动画,所以如果要修复它,您有两个选择:从头开始滚动您自己的Snackbar,或者使用一个更粗糙的反射解决方案: 科特林示例:
// 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通过适当的修复退出测试版之前是这样 自从Android 1.0.0-alpha3的Material Components使用后,此问题已得到修复 使用它而不是设计库(如果正在使用,这是一种方法):
我可以证实这也发生在我身上。JoaoApps的加入和Lastpass的可访问性启用和禁用都允许snackbar设置动画。结果表明,这显然是预期行为。至少可以说令人沮丧,特别是因为这意味着Droid Turbo用户必须禁用Moto Voice才能观看Snackbar动画。然而,我注意到的一件事是,谷歌收件箱应用程序中的Snackbar仍然是动画的——至少现在有可能,现在只是想弄清楚……有人找到了解决方案吗?一旦运行上面的代码,就可以在所有其他Snackbar实例中直接调用.show(),而不必使用此反射。如果您已经运行了上面的反射,然后只调用.show(),我建议您创建一个包装类来处理。我的应用程序是基于分析文本语音的可访问性服务的,因此这个动画无法工作,这真是令人沮丧。这似乎是唯一的选择,谢谢。“向前一步,向后两步”-谷歌不要这样做,除非你不介意破坏视力受损者的体验:一旦显示snackbar,对讲将停止工作,使应用程序对他们毫无用处。可以通过一个简单的修复来避免破坏视力受损者的应用程序:仅当accessibilityManager.isTouchExplorationEnabled为
false
时运行代码。视频链接断开非三星J5 pro(牛轧糖)也不起作用。所有Moto系列都有相同的问题。只有在使用的辅助功能服务不使用反馈时才会出现这种情况,但总比没有好!
implementation "com.google.android.material:material:$material_components_version"