getintrinsicwidth()/getIntrinsicHeight()大于或者小于图片实际像素大小

给在XML里给ImageView设置一张图片: 

android:src="@drawable/aaa"

然后在java代码中:

int intrinsicWidth = mImageView.getDrawable().getIntrinsicWidth();
int intrinsicHeight = mImageView.getDrawable().getIntrinsicHeight();
打印出来的结果发现大于图片的实际宽高;
度娘搜索无果,google一下,在stackoverflow【点我跳转】上找到了答案:原来我的测试设备是1080p,对应的图片资源文件应该是drawable-xxhdpi,但是这张图片位于drawable-xhdpi 目录下:
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


就不翻译了,很老的常识问题,很简单,记录一下,免得忘记~




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值