关闭

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

3334人阅读 评论(0) 收藏 举报
分类:

给在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


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




0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:171980次
    • 积分:1790
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:27篇
    • 译文:0篇
    • 评论:37条
    文章分类
    最新评论