Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VectorDrawable: How to position it on canvas?

For my custom view:

MyCustomView extends View 

I made a VectorDrawable

mMyVectorDrawable = VectorDrawableCompat.create(getContext().getResources(), R.drawable.ic_some_image, null);

I have set its bounds

mMyVectorDrawable.setBounds(0, 0, mMyVectorDrawable.getIntrinsicWidth(), mMyVectorDrawable.getIntrinsicHeight());

And I draw it on the Canvas

mMyVectorDrawable.draw(canvas);

I see the image at position 0,0

but how do I position it? How do I position the Rect, I thought the first two params of setBounds would be X and Y coordinates of where to start drawing but this just affects the size.

How can I position my vector drawable on a canvas?

like image 247
Ersen Osman Avatar asked Jun 03 '16 16:06

Ersen Osman


People also ask

What is a vector in XML?

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability.

What is vector asset in android Studio?

Android Studio includes a tool called Vector Asset Studio that helps you add material icons and import Scalable Vector Graphic (SVG) and Adobe Photoshop Document (PSD) files into your project as vector drawable resources.


1 Answers

You can translate your canvas before drawing the drawable onto it.

mMyVectorDrawable.setBounds(0, 0, width, height);
canvas.translate(dx, dy);
mMyVectorDrawable.draw(canvas);

The values dx and dy specify the offset from the coordinates (0, 0) where the next drawable will be drawn.

If you want, you can also undo the translation afterwards:

canvas.translate(-dx, -dy);
like image 199
Danilo Bargen Avatar answered Sep 18 '22 02:09

Danilo Bargen