Reputation: 71
I have a vector drawable that I want to rotate depending on some input in my app.
I have a vector_drawable.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="100dp"
android:height="100dp"
android:viewportWidth="100"
android:viewportHeight="100">
<group
android:name="rotation_group"
android:pivotX="50"
android:pivotY="50"
android:rotation="180" >
<path
android:pathData="..."
android:fillColor="#ffffff"/>
<path
android:pathData="..."
android:fillColor="#ffffff"/>
</group>
<path
android:pathData="..."
android:fillColor="#242424"/>
</vector>
In my code, I create a ValueAnimator
ValueAnimator animator = ObjectAnimator.ofInt(myDrawable, "rotate", 0, 360);
animator.setDuration(500);
animator.addUpdateListener(animatorUpdateListener);
animator.start();
With animatorUpdateListener being defined as follows:
animatorUpdateListener = new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int angle = (int) valueAnimator.getAnimatedValue("rotate");
myAngle = angle;
myView.invalidate();
}
};
In my onDraw method I then call
canvase.save(Canvas.MATRIX_SAVE_FLAG);
canvas.rotate(myAngle);
myDrawable.draw(canvas);
canvas.restore();
How would I be able, instead of rotating the canvas, to change the android:rotation
parameter inside the rotation_group
of my vector_drawable.xml?
Thanks so much!
Upvotes: 3
Views: 2033
Reputation: 3931
You can't. There's no way to alter the XML source definition on the fly. AnimatedVectorDrawable
can effectively alter the android:rotation
parameter, but only to predefined values it seems, not programmatically.
An alternative solution is to rotate the Canvas
when drawing it, as you currently do.
Or maybe wrap your VectorDrawable
in a RotateDrawable
and draw that instead.
Or setRotation
on the whole view.
Upvotes: 1