动画选择器会导致视图初始化崩溃

时间:2019-07-12 11:24:03

标签: android-studio kotlin

我的目标是创建一个自定义开关,其中包括选中状态和未选中状态之间的动画过渡。

我很容易地达到了默认状态,并且在这两种状态下我的开关都像预期的那样,并且在其状态改变时它也改变了外观。

当我附加动画选择器而不是常规选择器时,就会出现问题。

我遇到这样的错误:

  

android.view.InflateException:二进制XML文件行#10:二进制XML   文件行#10:错误夸大类android.widget.Switch

     

原因:android.content.res.Resources $ NotFoundException:文件   的res / drawable / switch_track_animation_unchecked_checked.xml来自   可绘制资源ID#0x7f060072           在android.content.res.Resources.loadDrawableForCookie(Resources.java:2640)

     

原因:android.content.res.Resources $ NotFoundException:文件   可绘制资源中的res / drawable / switch_track_animated_selector.xml   ID#0x7f060070

我可以确保可以正确访问那些可绘制对象等。

我经历了无效的缓存,然后重新启动,清理构建,重新构建等,但是它不起作用。

查看示例

<Switch
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:thumb="@drawable/switch_thumb_selector"
            android:track="@drawable/switch_track_animated_selector"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:checked="true"
            android:id="@+id/switch2"/>

switch_track_animated_selector

<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/checked"
          android:drawable="@drawable/switch_bckg_on"
          android:state_checked="true"/>

    <item android:id="@+id/unchecked"
          android:drawable="@drawable/switch_bckg_off"
          android:state_checked="false"/>

    <transition
            android:fromId="@+id/unchecked"
            android:toId="@+id/checked"
            android:drawable="@drawable/switch_track_animation_unchecked_checked"/>

    <transition
            android:fromId="@+id/checked"
            android:toId="@+id/unchecked"
            android:drawable="@drawable/switch_track_animation_checked_unchecked"/>
</animated-selector>

switch_bckg_on.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00B05A"/>
    <stroke android:width="1dp" android:color="#00B05A" />
    <corners android:radius="180dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

switch_bckg_off.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#F1F1F1"/>
    <stroke android:width="1dp" android:color="#E0E0E0" />
    <corners android:radius="180dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

switch_track_animation_checked_unchecked.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 xmlns:aapt="http://schemas.android.com/aapt"
                 android:drawable="@drawable/switch_bckg_on">
    <target android:name="android:drawable">
        <aapt:attr name="android:animation">
            <objectAnimator
                    android:duration="@android:integer/config_shortAnimTime"
                    android:interpolator="@android:interpolator/accelerate_decelerate"
                    android:propertyName="strokeColor"
                    android:valueFrom="#A0A0A0"
                    android:valueTo="#1E9618"
                    android:valueType="intType" />
        </aapt:attr>
    </target>
</animated-vector>

switch_track_animation_unchecked_checked.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 xmlns:aapt="http://schemas.android.com/aapt"
                 android:drawable="@drawable/switch_bckg_off">
    <target android:name="android:drawable">
        <aapt:attr name="android:animation">
            <objectAnimator
                    android:duration="@android:integer/config_shortAnimTime"
                    android:interpolator="@android:interpolator/accelerate_decelerate"
                    android:propertyName="strokeColor"
                    android:valueFrom="#A0A0A0"
                    android:valueTo="#1E9618"
                    android:valueType="intType"/>
        </aapt:attr>
    </target>
</animated-vector>

我也检查了上面的方法,使用矢量可绘制而不是形状,结果也一样。 向量的例子

ic_thumb_vector_off

<vector android:height="33dp" android:viewportHeight="300"
    android:viewportWidth="300" android:width="33dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:name="background_off" android:fillColor="#FFFFFF" android:fillType="nonZero"
        android:pathData="M149.926,149.926m-149.176,0a149.176,149.176 0,1 1,298.352 0a149.176,149.176 0,1 1,-298.352 0"
        android:strokeAlpha="0.1" android:strokeColor="#000000" android:strokeWidth="0.5"/>
    <path android:name="icon_off" android:fillColor="#9B9B9B" android:fillType="evenOdd"
        android:pathData="M166.88,146.104C166.13,145.354 166.13,144.228 166.88,143.478L195.019,115.339C195.77,114.588 196.145,113.463 196.145,112.712C196.145,111.962 195.77,110.836 195.019,110.086L189.767,104.833C189.016,104.083 187.891,103.708 187.14,103.708C186.015,103.708 185.264,104.083 184.514,104.833L156.375,132.972C155.624,133.723 154.499,133.723 153.748,132.972L125.609,104.833C124.859,104.083 123.733,103.708 122.983,103.708C122.233,103.708 121.107,104.083 120.357,104.833L115.104,110.086C114.354,110.836 113.979,111.962 113.979,112.712C113.979,113.463 114.354,114.588 115.104,115.339L143.243,143.478C143.994,144.228 143.994,145.354 143.243,146.104L115.104,174.243C114.354,174.993 113.979,176.119 113.979,176.869C113.979,177.62 114.354,178.745 115.104,179.496L120.357,184.748C121.107,185.499 122.233,185.874 122.983,185.874C123.733,185.874 124.859,185.499 125.609,184.748L153.748,156.609C154.499,155.859 155.624,155.859 156.375,156.609L184.514,184.748C185.264,185.499 186.39,185.874 187.14,185.874C187.891,185.874 189.016,185.499 189.767,184.748L195.019,179.496C195.77,178.745 196.145,177.62 196.145,176.869C196.145,176.119 195.77,174.993 195.019,174.243L166.88,146.104Z"
        android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>

ic_thumb_vector_on

<vector android:height="33dp" android:viewportHeight="302"
    android:viewportWidth="302" android:width="33dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:name="background_on" android:fillColor="#FFFFFF" android:fillType="nonZero"
        android:pathData="M151,151m-150.25,0a150.25,150.25 0,1 1,300.5 0a150.25,150.25 0,1 1,-300.5 0"
        android:strokeAlpha="0.1" android:strokeColor="#000000" android:strokeWidth="0.5"/>
    <path android:name="icon_on" android:fillColor="#00B05A" android:fillType="evenOdd"
        android:pathData="M105.523,153.198C104.806,152.5 104.448,151.453 104.448,150.754C104.448,150.056 104.806,149.009 105.523,148.31L110.536,143.422C111.968,142.026 114.117,142.026 115.549,143.422L115.907,143.772L135.602,164.371C136.318,165.069 137.393,165.069 138.109,164.371L186.093,115.841L186.451,115.841C187.883,114.444 190.032,114.444 191.464,115.841L196.477,120.728C197.91,122.125 197.91,124.22 196.477,125.616L139.183,183.573C138.467,184.272 137.751,184.621 136.676,184.621C135.602,184.621 134.886,184.272 134.17,183.573L106.239,154.246L105.523,153.198Z"
        android:strokeColor="#00000000" android:strokeWidth="1"/>
</vector>

也许我/我在gradle文件中缺少了一些东西?

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "...customswitch"
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

我希望此视图能够加载,并且不会在视图init上引发错误。

2 个答案:

答案 0 :(得分:0)

您的问题是switch_track_animation_unchecked_checked.xmlswitch_track_animation_checked_unchecked.xml是动画VectorDrawables的动画,并且这些动画使静态VectorDrawable处于动画状态。我想您是在为ShapeDrawable而不是VectorDrawable设置动画,这就是导致异常的原因。

您需要将switch_bckg_on.xmlswitch_bckg_off.xml替换为<vector>...</vector>的实现,以在<animated-vector>...</animated-vector>块内对其进行动画处理。

或者,您可以用StateListAnimator代替AnimatedVectorDrawables,尽管我不确定这样做是否能够真正实现您想要的目标。

答案 1 :(得分:0)

似乎您的 switch_track_animated_selector 没有一些没有任何 id 的默认状态。您可以尝试添加此状态。这在类似情况下帮助了我