skip to Main Content

I am trying to create a gradient animation with AnimatedVectorDrawable in AndroidStudio

But at the Start I get this error :

Unable to start activity ComponentInfo{com.palrahome/com.ex.MainActivity}: java.lang.IllegalStateException: Target with the name "point_one" cannot be found in the VectorDrawable to be animated.

my Code


loading_anim.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">
    <aapt:attr name="android:drawable">
        <vector
            android:width="156dp"
            android:height="2dp"
            android:viewportWidth="156"
            android:viewportHeight="2">
            <group android:name="loading">
                <path
                    android:name="path"
                    android:pathData="M0,0H156V2H0Z">
                    <aapt:attr name="android:fillColor">
                        <gradient
                            android:name="gradiant"
                            android:endX="156"
                            android:endY="2"
                            android:startX="0"
                            android:startY="2"
                            android:type="linear">
                            <item
                                android:color="#00229ED9"
                                android:offset="0" />
                            <item
                                android:name="point_one"
                                android:color="#FF229ED9"
                                android:offset="0.4" />
                            <item
                                android:name="point_two"
                                android:color="#FF229ED9"
                                android:offset="0.6" />
                            <item
                                android:color="#00229ED9"
                                android:offset="1" />
                        </gradient>
                    </aapt:attr>
                </path>
            </group>
        </vector>
    </aapt:attr>
    <target android:name="point_one">
        <aapt:attr name="android:animation">
            <objectAnimator
                android:duration="300"
                android:interpolator="@android:anim/decelerate_interpolator"
                android:propertyName="offset"
                android:valueFrom="0.4"
                android:valueTo="0.2"
                android:repeatMode="reverse"
                android:repeatCount="infinite"
                android:valueType="floatType" />
        </aapt:attr>
    </target>
    <target android:name="point_two">
        <aapt:attr name="android:animation">
            <objectAnimator
                android:duration="300"
                android:interpolator="@android:anim/decelerate_interpolator"
                android:propertyName="offset"
                android:valueFrom="0.6"
                android:valueTo="0.8"
                android:repeatMode="reverse"
                android:repeatCount="infinite"
                android:valueType="floatType" />
        </aapt:attr>
    </target>
</animated-vector>

and just start like this in mainActivity.java :

AnimatedVectorDrawable drawable = (AnimatedVectorDrawable) bind.loading.getDrawable();
    drawable.start();

What is the problem with this code and how can I fix it?

2

Answers


  1. Chosen as BEST ANSWER

    I finally realized that it is not possible to target name "point_one" , so

    the animation with android:fillAlpha: looks good

    <?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">
        <aapt:attr name="android:drawable">
            <vector
                android:width="156dp"
                android:height="5dp"
                android:viewportWidth="156"
                android:viewportHeight="5">
                <group android:name="loading">
                    <path
                        android:name="path"
                        android:fillAlpha="0.0"
                        android:pathData="M0,0H156V2H0Z">
                        <aapt:attr name="android:fillColor">
                            <gradient
                                android:name="gradiant"
                                android:endX="156"
                                android:endY="5"
                                android:startX="0"
                                android:startY="5"
                                android:type="linear">
                                <item
                                    android:color="#00229ED9"
                                    android:offset="0" />
                                <item
                                    android:color="#FF229ED9"
                                    android:offset="0.4" />
                                <item
                                    android:color="#FF229ED9"
                                    android:offset="0.6" />
                                <item
                                    android:color="#00229ED9"
                                    android:offset="1" />
                            </gradient>
                        </aapt:attr>
                    </path>
                </group>
            </vector>
        </aapt:attr>
        <target android:name="path">
            <aapt:attr name="android:animation">
                <objectAnimator
                    android:duration="800"
                    android:interpolator="@android:anim/decelerate_interpolator"
                    android:propertyName="fillAlpha"
                    android:repeatCount="infinite"
                    android:repeatMode="reverse"
                    android:valueFrom="0.5"
                    android:valueTo="1.0"
                    android:valueType="floatType" />
            </aapt:attr>
        </target>
    </animated-vector>
    

  2. You should change your vector file to point_ond.
    Can not support capital letters in res directory.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search