Android Animated Vector Drawable: change rotation degree at runtime
Asked Answered
A

1

7

I'm trying to make an Android vector animation, which contains a rotation on a vectorial group. Basically, if the degree transition would be constant, I would use those resources:

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

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

And my ObjectAnimator:

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

However, as you can see above, the final degree of rotation is set in the ObjectAnimator resource, as 360° for instance.

For my animation, how can I change programmatically this final value? The rotation degree need to be computed with some other data, so I don't know the target value before the animation starts.

Thank you for your help!

Atronna answered 29/8, 2016 at 7:19 Comment(1)
How do you made this work?Thury
F
1

I think the solution is to create an object of ObjectAnimator in the class like this

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

for more information check this out

Frisky answered 10/3, 2022 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.