Android objectAnimator set propertyValuesHolder

866 Views Asked by At

When I changed code line android:propertyName="scaleX" to android:propertyName="alpha", animation is not working!

Code

AnimatedVectorDrawableCompat mAnimatedVectorDrawable = AnimatedVectorDrawableCompat.create(
                        getApplication(), R.drawable.v_frame_animation
                );
                image.setImageDrawable(mAnimatedVectorDrawable);
                if (mAnimatedVectorDrawable != null) {
                    mAnimatedVectorDrawable.start();
                }

animator/v_frame_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:drawable="@drawable/gears_copy">
        <target
            android:name="vaaa"
            android:animation="@animator/heart_frame_animator" />
    </animated-vector>

animator/heart_frame_animator.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="5000"
            android:repeatCount="infinite"
            android:valueType="floatType">
            <propertyValuesHolder
                android:propertyName="scaleX"
                android:valueType="floatType">
                <keyframe
                    android:fraction="0"
                    android:interpolator="@android:anim/accelerate_interpolator"
                    android:value="0" />
                <keyframe
                    android:fraction=".5"
                    android:interpolator="@android:anim/accelerate_interpolator"
                    android:value="1" />
                <keyframe
                    android:fraction="1"
                    android:interpolator="@android:anim/accelerate_interpolator"
                    android:value="0" />
            </propertyValuesHolder>
        </objectAnimator>
1

There are 1 best solutions below

0
On

If you want to add fade animation to your android, you can use this xml code.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0"
android:toAlpha="1.0"
android:duration= "2000"
/>
</set>

This code makes the alpha value go from 0 to 1.0 in 2000ms.

Don't forget to add this in your res/anim folder and add this to your Activity.java

try {
        Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.splash);
        splash.startAnimation(myFadeInAnimation);//splash is an ImageView
    }catch (NullPointerException e){
        e.printStackTrace();
    }

This should solve it.

Happy Coding.