java.lang.IllegalStateException:恢复下溢 - 恢复多于保存

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

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

compile '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) ........................ ........................

至于以下 link 这是一个已知问题。 https://github.com/traex/RippleEffect/issues/76 我也尝试了很多来自 Whosebug 的解决方案,但到目前为止运气不错!!!

如何解决这个问题?

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

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

https://codeload.github.com/traex/RippleEffect/zip/master(来自您提供的 link 人们已经尝试使用的解决方案)


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

Ripple触摸效果在Android5.0(API21级)引入,动画由新的RippleDrawableclass实现。

常规,普通按钮的波纹效果 works 默认情况下在 API 21 中,对于其他可触摸视图,可以通过 指定 :

android:background="?attr/selectItemBackground"

对于视图中包含的涟漪或:

android:background="?attr/selectItemBackgroundBorderless"

对于超出视图范围的涟漪。

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

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

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

示例:

示例 1:无界波纹

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

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

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

使用方法: 要将您的波纹 xml 文件附加到 任何视图 ,请将其设置为背景,如下所示: 假设您的波纹文件名为 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" />

他们修复了错误 :P RippleEffect

再次检查您的代码。你可能在某处有一个额外的 .restore() 方法。

在我的例子中,我在 OnScroll 中模糊图像(我滚动视图并循环模糊)。我初始化 canvas 所以:

val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
// OnScroll:
    scroll.draw(canvas)
    blurImage(bitmap)

导致异常。所以,我将 val canvas = Canvas(bitmap) 移动到一个循环中:

val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
// OnScroll:
    val canvas = Canvas(bitmap)
    scroll.draw(canvas)
    blurImage(bitmap)

后来我删除了循环外的模糊,因此 canvas 不再在循环中创建。