Animated Vector Drawable not working with API 15

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

I've been trying to run an Animated Vector Drawable on a device with API level 15.

The following is my animated vector "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> 

The drawable "feedback.xml" contains the following:

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

And one of the animators "background_circle_animator.xml" is the following:

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

In my activity_main.xml I have the ImageView that should play this animation:

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

Finally in my MainActivity.java I have the following code:

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

The following is my 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' } 

When running this code you should see a white circle with a blue ring on top of it and it should fade in and then fade out.

I ran this code on API > 21 device and it ran perfectly.

When I run it on a device with API 15 I get the following crash as soon as I the animation starts:

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.access$900(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 

How can I fix this issue as it is very critical to run on devices with API level 15.

回答1:

I don't have an API 15 device or emulator to test this, but it seems to me it's not happy with your middle objectAnimator:

 <objectAnimator     android:duration="700"/> 

Parts of your error message such as 'Couldn't find setter/getter for property null' suggest it's trying to set up an animator, but as there are no details of what the animator should be animating, it's crashing.

Assuming the purpose of this objectAnimator is just to cause the animated vector to pause before fading out, I would suggest trying something like this:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!