为什么 ImageView 中可绘制对象的宽度/高度错误?

Why are the Width / Height of the drawable in ImageView wrong?(为什么 ImageView 中可绘制对象的宽度/高度错误?)

本文介绍了为什么 ImageView 中可绘制对象的宽度/高度错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该是一个简单的.

当我拉 image.getDrawable().getIntrinsicWidth() 时,我得到一个大于源图像宽度的值.它的 X 坐标返回 2880 而不是 1920,它太大了 1.5 倍?

When I pull image.getDrawable().getIntrinsicWidth() I get a value larger than the source image width. It's X coordinate is returning 2880 instead of 1920 which is 1.5 times too big?

我想知道具有中心"scaleType 的 ImageView 是否会影响它,但是根据 文档:

I wondered wether the ImageView having a scaleType of "center" effected it but, according to the documentation:

"将图像在视图中居中,但不执行缩放."

"Center the image in the view, but perform no scaling."

这里是来源:

<ImageView  
    android:id="@+id/backgroundImage"   
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:src="@drawable/background"
    android:scaleType="center"/>

推荐答案

你说drawable来自你的/res文件夹.它在哪个文件夹中?

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

  • /res/drawable

/res/drawable-mdpi

/res/drawable-hdpi

等等.

您正在测试的设备的密度是多少?它是一般密度为 240dpi 的 Nexus S 吗?因为如果您的源可绘制文件位于 drawable-mdpi 文件夹中并且您在 240dpi 设备上进行测试,那么 Android 系统将自动将可绘制文件放大 1.5 以便物理尺寸与 160dpi 的基线设备密度一致.

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.

当您调用 getIntrinsicWidth() 时,返回的是在 Android 缩放可绘制对象之后可绘制对象想要的大小.你会注意到 2880 = 1920 * 1.5

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

如果您将可绘制对象放置在 /res/drawable 中,Android 系统会将这些可绘制对象视为用于 mdpi 设备,因此会在您的 Nexus S 中进行升级.如果这是用于 hdpi 屏幕并且您这样做了不希望它升级然后尝试将它放在 drawable-hdpi

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

这篇关于为什么 ImageView 中可绘制对象的宽度/高度错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么 ImageView 中可绘制对象的宽度/高度错误?

基础教程推荐