如何通过对象动画制作动画

时间:2018-05-05 10:13:00

标签: android android-vectordrawable objectanimator

我正在尝试根据选择或不选择按钮状态,在ObjectAnimator的帮助下缩放矢量drawable。我试图这样做,

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_map_colored"
        android:state_selected="true" >
        <set>
            <objectAnimator
                android:duration="150"
                android:propertyName="scaleX"
                android:valueTo="2"
                android:valueType="floatType" />
            <objectAnimator
                android:duration="5000"
                android:propertyName="scaleY"
                android:valueTo="2"
                android:valueType="floatType"/>
        </set>
    </item>
    <item android:drawable="@drawable/ic_map" android:state_selected="false" >
        <set>
            <objectAnimator
                android:duration="150"
                android:propertyName="scaleX"
                android:valueTo="1"
                android:valueType="floatType"/>
            <objectAnimator
                android:duration="5000"
                android:propertyName="scaleY"
                android:valueTo="1"
                android:valueType="floatType"/>
        </set>
    </item>
</selector>

我只是想在选择图像时增加比例并在取消选择图像视图时减小比例。但是上面的代码不起作用,怎么做呢?

1 个答案:

答案 0 :(得分:2)

试试这段代码..

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <set>
        <objectAnimator
            android:duration="150"
            android:propertyName="scaleX"
            android:valueTo="2"
            android:valueType="floatType" />
        <objectAnimator
            android:duration="5000"
            android:propertyName="scaleY"
            android:valueTo="2"
            android:valueType="floatType"/>
    </set>
    <set>
        <objectAnimator
            android:duration="150"
            android:propertyName="scaleX"
            android:valueTo="1"
            android:valueType="floatType"/>
        <objectAnimator
            android:duration="5000"
            android:propertyName="scaleY"
            android:valueTo="1"
            android:valueType="floatType"/>
    </set>
</set>

然后调用如下代码..

AnimatorSet set = (AnimatorSet)  AnimatorInflater.loadAnimator(getActivity(), R.animator.sample);
set.setTarget(fab); // set the view you want to animate
set.start();