动画矢量绘图不适用于 API 15

Animated Vector Drawable not working with API 15

我一直在尝试 运行 在具有 API 级别 15 的设备上绘制动画矢量。

下面是我的动画矢量图"animated_feedback.xml":

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 android:drawable="@drawable/feedback">

    <target
    android:animation="@animator/background_circle_animator"
    android:name="BG_White_Circle"/>

</animated-vector>

可绘制对象 "feedback.xml" 包含以下内容:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="180dp"
        android:height="180dp"
        android:viewportWidth="180"
        android:viewportHeight="180">
    <group
        android:name="BG_White_Circle_Group">
        <path
            android:name="BG_White_Circle"
            android:fillColor="#fff"
            android:pathData="M 90 0 C 139.705627485 0 180 40.2943725152 180 90 C 180 139.705627485 139.705627485 180 90 180 C 40.2943725152 180 0 139.705627485 0 90 C 0 40.2943725152 40.2943725152 0 90 0 Z" />
        <path
            android:name="Ring"
            android:strokeColor="#0094c1"
            android:strokeWidth="5.00000003634922"
            android:strokeMiterLimit="10"
            android:pathData="M 90 9 C 134.735064736 9 171 45.2649352637 171 90 C 171 134.735064736 134.735064736 171 90 171 C 45.2649352637 171 9 134.735064736 9 90 C 9 45.2649352637 45.2649352637 9 90 9 Z" />
    </group>
</vector>

动画师之一 "background_circle_animator.xml" 如下:

<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:ordering="sequentially"
     android:fillAfter="true">

    <objectAnimator
        android:propertyName="fillAlpha"
        android:valueType="floatType"
        android:valueFrom="0f"
        android:valueTo="1f"
        android:duration="200"/>

    <objectAnimator
        android:duration="700"/>

    <objectAnimator
        android:propertyName="fillAlpha"
        android:valueType="floatType"
        android:valueFrom="1f"
        android:valueTo="0f"
        android:duration="200"/>

</set>

在我的 activity_main.xml 中,我有应该播放此动画的 ImageView:

<ImageView
    android:id="@+id/feedback_ui"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="invisible"/>

最后在我的 MainActivity.java 中有以下代码:

ImageView feedbackUI = (ImageView) findViewById(R.id.feedback_ui);
feedbackUI.setImageResource(R.drawable.animated_feedback);

Drawable animation = feedbackUI.getDrawable();
if (animation instanceof Animatable) {
    feedbackUI.setVisibility(View.VISIBLE);
    ((Animatable) animation).start();
}

以下是我的gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "karim.com.testinganimation"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0'
}

当 运行 执行此代码时,您应该会看到一个白色圆圈,上面有一个蓝色圆环,它应该淡入然后淡出。

我 运行 此代码在 API > 21 设备上 运行 完美。

当我在 API 15 的设备上 运行 时,我在动画开始后立即发生以下崩溃:

D/PropertyValuesHolder(19300): Can't find native method using JNI, use reflectionjava.lang.NoSuchMethodError: no method with name='set' signature='(F)V' in class Landroid/support/graphics/drawable/VectorDrawableCompat$VFullPath;
E/PropertyValuesHolder(19300): Couldn't find setter/getter for property null with value type float
E/PropertyValuesHolder(19300): Couldn't find no-arg method for property null: java.lang.NoSuchMethodException: get []
D/AndroidRuntime(19300): Shutting down VM
W/dalvikvm(19300): threadid=1: thread exiting with uncaught exception (group=0x40a9e1f8)
E/AndroidRuntime(19300): FATAL EXCEPTION: main
E/AndroidRuntime(19300): java.lang.NullPointerException
E/AndroidRuntime(19300):    at android.animation.PropertyValuesHolder.setupSetterAndGetter(PropertyValuesHolder.java:513)
E/AndroidRuntime(19300):    at android.animation.ObjectAnimator.initAnimation(ObjectAnimator.java:392)
E/AndroidRuntime(19300):    at android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:544)
E/AndroidRuntime(19300):    at android.animation.ValueAnimator.start(ValueAnimator.java:934)
E/AndroidRuntime(19300):    at android.animation.ValueAnimator.start(ValueAnimator.java:957)
E/AndroidRuntime(19300):    at android.animation.ObjectAnimator.start(ObjectAnimator.java:370)
E/AndroidRuntime(19300):    at android.animation.AnimatorSet$DependencyListener.startIfReady(AnimatorSet.java:705)
E/AndroidRuntime(19300):    at android.animation.AnimatorSet$DependencyListener.onAnimationEnd(AnimatorSet.java:659)
E/AndroidRuntime(19300):    at android.animation.ValueAnimator.endAnimation(ValueAnimator.java:1040)
E/AndroidRuntime(19300):    at android.animation.ValueAnimator.access0(ValueAnimator.java:49)
E/AndroidRuntime(19300):    at android.animation.ValueAnimator$AnimationHandler.handleMessage(ValueAnimator.java:675)
E/AndroidRuntime(19300):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(19300):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(19300):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(19300):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(19300):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(19300):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(19300):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(19300):    at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager(  164):   Force finishing activity karim.com.testinganimation/.MainActivity

我该如何解决这个问题,因为它对 运行 在 API 级别 15 的设备上非常重要。

我没有 API 15 设备或模拟器来测试它,但在我看来它对你的中间 objectAnimator 不满意:

 <objectAnimator
    android:duration="700"/>

您的部分错误消息,例如“找不到 setter/getter for 属性 null”,表明它正在尝试设置动画师,但没有关于动画师应该做什么的详细信息动画,它正在崩溃。

假设这个 objectAnimator 的目的只是让动画矢量在淡出之前暂停,我建议尝试这样的事情:

<objectAnimator
    android:propertyName="fillAlpha"
    android:valueType="floatType"
    android:valueFrom="1f"
    android:valueTo="1f"
    android:duration="700"/>