android – java.lang.IllegalStateException:恢复时下溢 – 恢复多于保存

我正在为我的项目使用rippleeffect库.但是在Android Nougat和Marshmallow中,由于这个库App崩溃了:

编译’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和我也尝试过*的很多解决方案,但到目前为止运气确实有利!

可以做些什么来解决这个问题?

解决方法:

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

>将targetSdkVersion降级到22你可以运行它:意味着它不会崩溃!但我真的不建议这样做.
>尝试使用编译此依赖项来编译它 – >’com.github.emanzanoaxa:RippleEffect:52ea2a0ab6′
>调用canvas.save();在每次恢复之前()是你的链接的另一个建议,你可以试试
>您还可以尝试在项目中添加该库并使用它

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

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

在Android 5.0(API级别21)中引入了波纹触摸效果,并且动画由新的RippleDrawable类实现.

一般情况下,常规按钮的涟漪效果在API 21中默认工作,对于其他可触摸视图,可以通过指定:

机器人: “ATTR / selectItemBackground” 背景=

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

机器人: “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>

如何使用:
要将ripple xml文件附加到任何视图,请将其设置为背景,如下所示:
假设您的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 – Ripple在ImageView下面绘制


下一篇:Android 的点击事件你知道有几种?