Android动画矢量图形:如何在运行时更改旋转角度

7

我想制作一个包含矢量组旋转的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>

我的 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>

我的ObjectAnimator:

<objectAnimator
     android:duration="500" 
     android:propertyName="rotation" 
     android:valueFrom="0" 
     android:valueTo="360" />

然而,正如您在上面所看到的,最终旋转度数是在ObjectAnimator资源中设置的,例如360°。

对于我的动画,我如何在程序中更改此最终值?旋转度数需要使用其他数据进行计算,因此在动画开始之前我不知道目标值。

谢谢您的帮助!


你是如何让这个工作的? - Konstantin Konopko
1个回答

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接