Android动画矢量Drawable:在运行时更改旋转度

Pie*_*lon 5 animation android vector rotation

我正在尝试制作Android矢量动画,其中包含矢量组的旋转.基本上,如果学位转换是不变的,我会使用这些资源:

我的VectorDrawable:

<vector
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:width="64dp" 
    android:height="64dp" 
    android:viewportHeight="600" 
    android:viewportWidth="600">
    <group
        android:name="myRotationGroup" 
        android:pivotX="300.0" 
        android:pivotY="300.0" 
        android:rotation="0">
        <path
            android:name="myName" 
            android:fillColor="#000000" 
            android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z"/>
    </group>
</vector>
Run Code Online (Sandbox Code Playgroud)

我的AnimatedVectorDrawable:

<animated-vector 
   xmlns:android="http://schemas.android.com/apk/res/android" 
   android:drawable="@drawable/my_vector_drawable" >
     <target
         android:name="myRotationGroup" 
         android:animation="@animator/my_rotation" />
 </animated-vector>
Run Code Online (Sandbox Code Playgroud)

而我的ObjectAnimator:

<objectAnimator
     android:duration="500" 
     android:propertyName="rotation" 
     android:valueFrom="0" 
     android:valueTo="360" />
Run Code Online (Sandbox Code Playgroud)

但是,如上所示,最终旋转度在ObjectAnimator资源中设置,例如360°.

对于我的动画,如何以编程方式更改此最终值?需要使用其他一些数据计算旋转度,因此在动画开始之前我不知道目标值.

谢谢您的帮助!

Kan*_*mar 1

我认为解决方案是在类中创建一个 ObjectAnimator 对象,如下所示

ObjectAnimator myRotation = ObjectAnimator.ofFloat(target,"rotation",360f);
myRotation.setDuration(500);
myRotation.start();    // this will start the animation

// here target is the object of the view on which you want to apply this animation 
// and you can change this 360f to any number at which degree you want to rotate
Run Code Online (Sandbox Code Playgroud)

欲了解更多信息,请查看此