【问题标题】:java.lang.IllegalStateException: Underflow in restore - more restores than savesjava.lang.IllegalStateException:恢复中的下溢 - 恢复多于保存
【发布时间】:2017-05-14 09:48:00
【问题描述】:

我在我的项目中使用rippleeffect library。但是在Android Nougat and Marshmallow 中,App 会因为这个库而崩溃:

compile 'com.github.traex.rippleeffect:library:1.3'

错误信息是:

致命异常:主要 进程:com.test.testapp,PID:17713 java.lang.IllegalStateException:恢复中的下溢 - 恢复多于保存 在 android.graphics.Canvas.native_restore(本机方法) 在 android.graphics.Canvas.restore(Canvas.java:522) 在 com.andexert.library.RippleView.draw(RippleView.java:170) ……………………………………………………………………………… ......................

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

有什么办法可以解决这个问题?

【问题讨论】:

  • 我认为您可以通过将 -targetSdkVersion 降级到 22 来修复此错误,但我不建议使用它!你不需要库来使用涟漪效应
  • 这个问题出现在 Android API Level 23 (Marshmallow) 中。我将库版本更新为最新版本。他们已经解决了这个错误。

标签: android ripple rippledrawable


【解决方案1】:

他们修复了这个错误:P RippleEffect

【讨论】:

  • 非常好@Nuke
  • 它还在来。
【解决方案2】:

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

  • targetSdkVersion 降级到22 你可以运行它:意味着它不会崩溃!但我真的建议这样做。
  • 尝试用编译这个依赖来编译它->'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 中默认起作用,对于其他可触摸视图,可以通过指定来实现:

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

如果你想自定义涟漪效果到一个视图中, 您需要在可绘制目录中创建一个新的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" />

【讨论】:

  • 'com.github.emanzanoaxa:RippleEffect:52ea2a0ab6' 依赖对我有用。
  • 我有 2 个 restore() 用于 1 个 save()。添加第二次保存就可以了。非常感谢您的提示。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    在我的情况下,我在 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)
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 2013-02-04
      • 1970-01-01
      相关资源
      最近更新 更多