wnafee / vector-compat

A support library for VectorDrawable and AnimatedVectorDrawable classes introduced in Lollipop
1.23k stars 161 forks source link

Fails when attempting to animate vector path attributes #16

Open twyatt opened 9 years ago

twyatt commented 9 years ago

First and foremost, thanks for all your hard work on this library!

On Android API 14 devices, animating a vector group works beautifully but when attempting to animate vector path attributes (e.g. fillAlpha on path circlePath of circle.xml below) an exception is thrown:

07-15 23:51:44.013  17042-17042/com.example.vector_compat_test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vector_compat_test/com.example.vector_compat_test.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/circle_fade.xml from drawable resource ID #0x7f02003c
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/circle_fade.xml from drawable resource ID #0x7f02003c
            at android.content.res.Resources.loadDrawable(Resources.java:1918)
            at android.content.res.Resources.getDrawable(Resources.java:659)
            at com.wnafee.vector.compat.ResourcesCompat.getDrawable(ResourcesCompat.java:34)
            at com.example.vector_compat_test.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag animated-vector
            at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:877)
            at android.graphics.drawable.Drawable.createFromXml(Drawable.java:818)
            at android.content.res.Resources.loadDrawable(Resources.java:1915)
            at android.content.res.Resources.getDrawable(Resources.java:659)
            at com.wnafee.vector.compat.ResourcesCompat.getDrawable(ResourcesCompat.java:34)
            at com.example.vector_compat_test.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

drawable/circle.xml

<?xml version="1.0" encoding="utf-8"?>
<vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:width="150dp"
    android:height="150dp"
    android:viewportWidth="100"
    android:viewportHeight="100"

    app:vc_viewportWidth="100"
    app:vc_viewportHeight="100">

    <group
        android:name="circleGroup"
        android:rotation="0"
        android:pivotX="50"
        android:pivotY="50">

        <!--
        Circle path data generated with:
        CX: 50.5, CY: 50.5, R: 28
        http://complexdan.com/svg-circleellipse-to-path-converter/
        -->
        <path
            android:name="circlePath"
            android:fillColor="@android:color/white"
            android:fillAlpha="1.0"
            android:pathData="@string/path_circle"

            app:vc_fillColor="@android:color/white"
            app:vc_fillAlpha="1.0"
            app:vc_pathData="@string/path_circle"/>

    </group>

</vector>

drawable/circle_fade.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/circle">

    <target
        android:name="circlePath"
        android:animation="@animator/fade"/>

</animated-vector>

animator/fade.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="fillAlpha"
    android:duration="@android:integer/config_longAnimTime"
    android:valueFrom="1.0"
    android:valueTo="0.0"
    android:repeatCount="1"
    android:repeatMode="reverse"/>

MainActivity.java

...
import com.wnafee.vector.compat.ResourcesCompat;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Drawable drawable = ResourcesCompat.getDrawable(this, R.drawable.circle_fade);

        ImageView circleView = (ImageView) findViewById(R.id.circle);
        circleView.setImageDrawable(drawable);

        circleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawable instanceof Animatable) {
                    ((Animatable) drawable).start();
                } else {
                    Log.d(TAG, "!(drawable instanceof Animatable)");
                }
            }
        });
    }

}

The same project works when running on Android API 21+ devices.

twyatt commented 9 years ago

Project used for testing: https://github.com/twyatt/vector-compat-test

ghost commented 9 years ago

I have the same problem.

timrijckaert commented 8 years ago

Same problem here. Any solutions?

olegosipenko commented 8 years ago

Confirm, it's falling when trying to animate path attributes. Though the Support Vectors from Support Library successfully animate these attributes, but have no ways to animate paths, this library successfully animates paths, but no path attributes

mshenawy22 commented 4 years ago

same problem , any solutions?

twyatt commented 4 years ago

same problem , any solutions?

@mshenawy22 I don't believe this project is maintained anymore. Does the official AnimatedVectorDrawableCompat provide what you need?