Android objectAnimator设置propertyValuesHolder

时间:2017-01-09 03:24:33

标签: android android-animation android-vectordrawable

当我将代码行android:propertyName="scaleX"更改为android:propertyName="alpha"时,动画无效!

代码

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

动画/ 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>

动画/ 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 个答案:

答案 0 :(得分:0)

如果你想在你的android上添加淡入淡出动画,你可以使用这个xml代码。

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

此代码使alpha值在2000ms内从0变为1.0。

不要忘记在res / anim文件夹中添加它并将其添加到Activity.java

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

这应该解决它。

快乐编码。