What is the impact of viewportwidth/height on Android drawables

AndroidSvgAndroid Vectordrawable

Android Problem Overview


I have an vector drawable I imported from an SVG asset. Sometimes, I have to adjust the size. Usually I update width and height. What I can't work out is how viewportwidth and height also impact the svg. It seems changing these dimensions can push the svg out of view within the visible area.

What do these units of measurement represent ? What is it's relationship with width and height respectively ? The documentation from Google is, (as usual), woefully inadequate. Could someone please elaborate ?

Android Solutions


Solution 1 - Android

The viewportWidth and viewPortHeight define the area of the document that the content of the VectorDrawable is drawn within. They are equivalent to the width and height fields of an SVG viewBox. Research how an SVG viewBox works if you need further explanation.

So imagine your shape is a rectangle that is 100 wide and 100 height. Your viewportWidth and viewPortHeightshould normally both be set to 100. So that Android knows the dimensions of the underlying shapes.

The width and height attributes tell Android what the default ("intrinsic") rendering size of the VectorDrawable should be. You can think of these like the width and height of a PNG or GIF (or SVG for that matter).

So the contents of your VectorDrawable could be defined over an area of 100x100. But if your width and height are 24x24, the contents will be scaled down from 100x100 to 24x24.

So that's why fiddling with the viewportWidth and viewPortHeight messes with the VectorDrawable. So for instance, if you change them to 50x50, you would end up with one corner of the shape scaled down to 24x24 - instead of the whole shape.

Solution 2 - Android

Let's assume that the apple inside the canvas is your main vector drawable.Then h and w which are the height and width of the canvas respectively. So finally that h and w will be your drawable's viewportHeight and viewportWidth respectively

enter image description here

Solution 3 - Android

Viewportwidth/Viewportheight are the dimensions of the canvas for the SVG paths and the width/height are the actual intrinsic dimensions of the entire drawable.

Solution 4 - Android

Simply viewportWidth is like width of Frame and width like width of the picture inside that frame.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionangryITguyView Question on Stackoverflow
Solution 1 - AndroidPaul LeBeauView Answer on Stackoverflow
Solution 2 - AndroidGk Mohammad EmonView Answer on Stackoverflow
Solution 3 - Androidpdegand59View Answer on Stackoverflow
Solution 4 - AndroidAli AkramView Answer on Stackoverflow