android - java.lang.IllegalStateException : Underflow in restore - more restores than saves 错误

标签 android ripple rippledrawable

我正在为我的项目使用 rippleeffect 库。但是在 Android Nougat 和 Marshmallow 中,应用程序由于这个库而崩溃:

编译'com.github.traex.rippleeffect:library:1.3'

错误信息是:

FATAL EXCEPTION: main Process: com.test.testapp, PID: 17713 java.lang.IllegalStateException: Underflow in restore - more restores than saves at android.graphics.Canvas.native_restore(Native Method) at android.graphics.Canvas.restore(Canvas.java:522) at com.andexert.library.RippleView.draw(RippleView.java:170) ........................ ........................

就以下链接而言,这是一个已知问题。 https://github.com/traex/RippleEffect/issues/76而且我还尝试了很多来自 stackoverflow 的解决方案,但到目前为止运气不错!!!

如何解决这个问题?

最佳答案

我遇到了同样的问题,但没有找到好的解决方案。但是如果你

  • targetSdkVersion 降级到 22 你可以运行它:这意味着它不会崩溃!但我真的推荐。
  • 尝试使用 compile this dependency 编译它 ->'com.github.emanzanoaxa:RippleEffect:52ea2a0ab6'
  • 在每个 restore() 之前调用 canvas.save(); 是您链接中的另一个建议,因为您可以尝试
  • 您也可以尝试将该库添加到您的项目中并使用它

https://codeload.github.com/traex/RippleEffect/zip/master (从您提供的链接中可以找到人们尝试使用的解决方案)


或者我建议您自己创建它们,根本不需要库!

Ripple 触摸效果在 Android 5.0(API 级别 21)中引入,动画由新的 RippleDrawable 类实现。

常规按钮的涟漪效果在 API 21 中默认工作,对于其他可触摸 View ,可以通过指定实现:

android:background="?attr/selectItemBackground"

对于 View 中包含的涟漪或:

android:background="?attr/selectItemBackgroundBorderless"

对于超出 View 边界的涟漪。

您可以使用以下代码实现相同的效果:

int[] attrs = new int[]{R.attr.selectItemBackground};
TypedArray typedArray = getActivity().obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
myView.setBackgroundResource(backgroundResource);

如果您想将波纹效果自定义到 View 中, 您需要在 drawable 目录中创建一个新的 XML 文件。

例子:

示例 1:无限涟漪

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ffff0000" />

示例 2:带有 mask 和背景颜色的波纹

<ripple android:color="#7777666"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/mask"
        android:drawable="#ffff00" />
    <item android:drawable="@android:color/white"/>
</ripple>

示例 3:在可绘制资源上波纹

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#ff0000ff">
    <item android:drawable="@drawable/my_drawable" />
</ripple>

如何使用: 要将 ripple xml 文件附加到任何 View ,请将其设置为背景,如下所示: 假设您的 ripple 文件名为 my_ripple.xml。在示例 1、2 或 3 中

<View 
    android:id="@+id/myViewId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/my_ripple" />

关于android - java.lang.IllegalStateException : Underflow in restore - more restores than saves 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41390367/

相关文章:

android - 改变涟漪效应的颜色

java - Android 对背景项 onclick 的波纹效果

Android imagebutton 波纹效果

android - WebView.draw(canvas) 有时会在某些设备上的 Android Lollipop 5.0+ 中崩溃 - 致命信号 11 (SIGSEGV)

java - 在 Android 的 MapsActivity 中单击标记时显示的按钮

java - XMPP 与 Android Studio 上的 Smack 4.1 连接

node.js - PhoneJS w Ripple模拟器无法连接到本地 Node 服务器

android - 更改首选项(设置)后,显示设置的文本不会更新

Android Lollipop - 通知中的涟漪效应

android - 在 recyclerview 中触摸没有涟漪效应