I'm trying to visualize a rotating object on an Android 9 device by programmatically rotating a vector drawable, created from a .svg file using the New->Vector Asset method. The rotation of the object should follow the changes of some external property, instead of a predefined animation.
When a vector image is drawn on an ImageView as a VectorDrawable, the image produced has smooth edges as it should, but when the image is rotated programmatically using a RotateDrawable, the edges become jagged, as if the image was treated as a bitmap, and not redrawn as vector graphics.
The image below illustrates this problem:
According to the VectorDrawable documentation, a bitmap cache is created for each vector asset when it is first loaded, but is there a possibility to force the re-rendering when the image is rotated?
Below is some sample code used to create the effect.
Drawable class:
public class MyDrawable extends Drawable implements Drawable.Callback, Runnable {
@Override
public void draw(Canvas canvas)
{
VectorDrawable vectorDrawable = (VectorDrawable)mainActivity.getResources().getDrawable(R.drawable.test_drawable, mainActivity.getTheme());
vectorDrawable.setBounds(canvas.getClipBounds());
vectorDrawable.draw(canvas); // This looks OK
RotateDrawable rotator = new RotateDrawable();
rotator.setBounds(canvas.getClipBounds());
rotator.setLevel(1000);
rotator.setDrawable(vectorDrawable.mutate());
rotator.draw(canvas); // Jagged edges
}
}
Used in an ImageView in a Fragment:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
final ImageView testView = (ImageView)getView().findViewById(R.id.test_view);
testView.setImageDrawable(new MyDrawable());
}