VectorDrawableCompat and Canvas rotate, retrievable fade 90/270 degrees

I am trying to use vector drawings to draw on canvas. Everything is fine and dandy, until I rotate the canvas object 90 or 270 degrees. Closer I get to 90 or 270 degrees, a more blurry image shown on the canvas appears. Finally, at 90 or 270 degrees, the vector that can be drawn on the canvas completely disappears. Is there any fix or workaround for this? Or should I approach drawing to canvas using svg with another library? Thanks!

Here is the code:

public class CanvasView extends View { private static final String TAG = "CanvasView"; private VectorDrawableCompat vectorDrawableCompat; private int angle; public CanvasView(Context context) { super(context); init(); } public CanvasView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CanvasView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init(){ vectorDrawableCompat = VectorDrawableCompat.create(getResources(), R.drawable.ic_android_black_24dp, null); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); vectorDrawableCompat.setBounds((getWidth()/2) - 50, (getHeight()/2) - 50, (getWidth()/2) + 50, (getHeight()/2) + 50); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.save(); canvas.rotate(angle, getWidth()/2, getHeight()/2); vectorDrawableCompat.draw(canvas); canvas.restore(); } public void setAngle(int angle){ Log.i(TAG, "setAngle: " + angle); this.angle = angle; invalidate(); } } 

Here's the project: https://github.com/danskiess/VectorTest

+5
source share
1 answer

This is fixed in the Android infrastructure. https://code.google.com/p/android/issues/detail?id=192413

One possible workaround for this rotation case is to simply draw a VectorDrawable in Bitmap and then rotate the bitmap.

+1
source

Source: https://habr.com/ru/post/1244966/


All Articles