求助:VectorDrawable的viewportWidth和viewportHeight单位是什么?
Hey there, I totally get why this feels unclear since the docs don't explicitly spell out the unit for these attributes—let's break this down plainly:
First, a quick recap of the drawable's intrinsic size (
android:width/android:height):These define the actual physical size the drawable will occupy on screen, and they support all standard Android measurement units (dp, sp, px, etc.). As you noted, using dp is the best practice here to keep things density-independent.
Now for
android:viewportWidthandandroid:viewportHeight:
These are abstract, unit-less values—they don't correspond to dp, px, or any physical screen unit at all. Think of the viewport as a virtual canvas that your vector paths are drawn onto. Every coordinate in your vector'spathDatais defined relative to this viewport's dimensions.For example: If you set
viewportWidth="24"andviewportHeight="24", every point and line in your vector path uses this 24x24 virtual space as its reference. When the drawable is rendered on screen, this entire virtual canvas is scaled up or down to fit the intrinsic size you set withandroid:width/android:height(say, ifandroid:width="48dp", the 24-unit viewport will be scaled to fill 48dp of screen space).The core idea here is that the viewport's values are just a coordinate system reference—they don't represent any real-world screen measurement on their own.
内容的提问来源于stack exchange,提问作者Anil

