You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:VectorDrawable的viewportWidth和viewportHeight单位是什么?

Understanding VectorDrawable's viewportWidth/viewportHeight Units

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:viewportWidth and android: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's pathData is defined relative to this viewport's dimensions.

    For example: If you set viewportWidth="24" and viewportHeight="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 with android:width/android:height (say, if android: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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:33:55