Why are the Width / Height of the drawable in ImageView wrong?

前端 未结 4 1872
失恋的感觉
失恋的感觉 2021-01-12 00:41

Should be a simple one.

When I pull image.getDrawable().getIntrinsicWidth() I get a value larger than the source image width. It\'s X coordinate is retu

4条回答
  •  失恋的感觉
    2021-01-12 01:28

    You said the drawable is from your /res folder. Which folder is it in?

    • /res/drawable

    • /res/drawable-mdpi

    • /res/drawable-hdpi

      etc..

    And what is the density of the device you are testing on? Is it a Nexus S with general density of 240dpi? Because if your source drawable is located in the drawable-mdpi folder and you are testing on a 240dpi device, then the Android system will automatically scale the drawable up by a factor of 1.5 so that the physical size will be consistent with the baseline device density at 160dpi.

    When you call getIntrinsicWidth() what is returned is the size the drawable wants to be after Android scales the drawable. You'll notice that 2880 = 1920 * 1.5

    If you placed the drawable in /res/drawable the Android system treats those drawables as meant for mdpi devices, thus the upscaling in your Nexus S. If this was meant for hdpi screens and you do not want this to upscale then try placing it in drawable-hdpi

提交回复
热议问题