设置ImageView的ScaleType到"中心"不工作,我希望它工作、中心、ImageView、ScaleType

2023-09-05 01:13:06 作者:噗通i

我有我的的ImageView 以下布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants">
    <ImageView
        android:id="@+id/imageview_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:scaleType="fitCenter" />
    (...)
</LinearLayout>

这些 48dp 等于36px,48像素和72px在 LDPI MDPI 键,分别为华电国际。所有这一切将在本的ImageView 中使用的图像基本上都是图标和我什么,我想我的应用程序(免费许可)在网络上找到。但不幸的是,它不来与图像大于48像素,并使用不同的图像是不可能的。所以,我需要一个新的解决方案...

Those 48dp equal to 36px, 48px and 72px in ldpi, mdpi and hdpi respectively. All the images that will be used in this ImageView are basically icons and I've found on the web what I want for my app (free license). But unfortunately, it doesn't come with images bigger than 48px and using different images is out of the question. So I need a new solution...

现在我已经在 LDPI MDPI 文件夹36px和48像素的图像,分别。但我在使用华电国际麻烦。我想对华电国际运行设备是他们使用 MDPI 图像(较大的一个可用)和使用它,因为它是,不结垢。基本上,的ImageView 华电国际为72px,所以我想在48像素的图像是72px内,在中心,无需缩放。对于这一点,我只是试图改变 scaleType 的ImageView 以上只是中心,但图像获取反正缩放。

For now I have 36px and 48px images in the ldpi and mdpi folders, respectively. But I'm having trouble with hdpi. What I want for devices running on hdpi is for them to use the mdpi image (the bigger one available) and use it as it is, without scaling. Basically, the ImageView for hdpi is 72px, so I want the 48px image to be inside the 72px, in the center, without scaling. For that, I simply tried to change scaleType in the ImageView above to just center, but the image gets scaled anyway.

在这里是我的最后一个问题,什么是正确的方法来解决上述问题呢?怎样才可以有 LDPI MDPI 被使用在各自的密度图像,但在运行设备华电国际拿起可用最大图像(也就是 MDPI 的)和prevent任何缩放,刚装修的图像中的中心的的ImageView

My ultimate question in here is, what's the proper way to fix the problem described above? How can I have ldpi and mdpi images being used in their respective densities, but have devices running hdpi pick up the biggest image available (which is the mdpi ones) and prevent any scaling, just fitting the image in the center of the ImageView?

编辑: 我回答这个问题,我自己,但它可能是答案别人来这里寻找。但我提供什么是真正发生的一些见解和潜在的问题。请看看,如果你能提供一个解决方法/修复。我敢说你:)

I answered this question myself, but it might to be the answer others coming here are looking for. But I do provide some insight on what's really happening and the underlying problem. Please take a look and provide a workaround/fix if you can. I dare you :)

推荐答案

这个问题原来是一个非问题,我道歉,任何人创立的,希望能接受的答案将是他们正在寻找的东西,当它获得总是那样。

This question turns out to be a non-question and I apologize to anyone that founds it, hoping the accepted answer will be what they are looking for when it won't be.

要清楚, scaleType = centerInside 工作正常。也就是说,如果你有一个形象比的ImageView 本身,比形象将不被缩放到的ImageView的边界,它会留在中心,缩放的。

To make it clear, scaleType=centerInside is working as expected. That is, if you have an image smaller than the ImageView itself, than that image won't be scaled to the bounds of the ImageView, it will remain in the center and unscaled.

但是,对于上述工作作为预期,绘制必须放在 nodpi 文件夹。据我所知,这并不总是可以接受的。所以,当绘制必须放在密度的文件夹,而不是 nodpi 文件夹之一, scaleType 属性将只能在特定的情况下。

But for the above to work as expected, the drawable must be placed in the nodpi folder. I understand that this is not always acceptable. So, when that drawable must be placed into one of the density folders instead of the nodpi folder, the scaleType attribute will only work in specific situations.

当将工作:

您运行的是具有 Xdpi 密度和设备/仿真器应用程序 有一个在 Xdpi 密度文件夹中的绘制(这里 X 表示, M ^ h 甚至 XH )。 您正在运行的应用程序的设备/仿真器, 例如,与 HDPI 密度,但没有在一个可拉伸 华电国际文件夹,系统采用主从替代绘制 nodpi 哪个文件夹,将挑选的文件夹(它并不总是知道 从)。 You are running the app on a device/emulator with Xdpi density and there is a drawable in the Xdpi density folder (here X means, l, m, h or even xh). You are running the app device/emulator, for instance, with hdpi density, but there isn't a drawable in the hdpi folder and the system picks the alternative drawable from the nodpi folder (it's not always known which folder it will pick from).

当它是行不通的:

您正在运行的应用程序的设备/仿真器,比如,用华电国际 密度大,但没有在华电国际文件夹中的绘制和 系统采用主从任何其它密度的替代提拉 文件夹(而不是 nodpi 文件夹中),可绘制将被缩小到 的ImageView 边界和 scaleType 属性不会做 任何东西。 You are running the app device/emulator, for instance, with hdpi density, but there isn't a drawable in the hdpi folder and the system picks the alternative drawable from any other of the density folders (not the nodpi folder), the drawable will be scaled to the ImageView bounds and the scaleType attribute will not do anything.

总之,没有正确回答我的问题,这真的取决于你正在努力实现的目标。答案的我的问题,不过,我只是需要做两件事:1)将的ImageView scaleType centerInside 和b)重复从 MDPI 文件夹到华电国际所有可绘文件夹(如上面解释的, scaleType = centerInside 将使其工作)。

In conclusion, there's no "right" answer to my question, it really depends on what you are trying to achieve. The answer my question though, I just need to do 2 things: a) Set the ImageView scaleType to centerInside and b) Duplicate all drawables from the mdpi folder into the hdpi folder (as explained above, the scaleType=centerInside will make it work).

当然,复制可绘制不是最优的,但我找不到任何其他的解决办法,到目前为止,没有人可以既......所以,在目前而言,我会因为接受了纪念这一个。

Of course, duplicating drawables is not optimal, but I can't find any other solution and so far, no one else could either... So, in the time being, I'll mark this one as accepted.

什么是最佳的答案/解决方案呢? 在我看来,如果该设备/仿真器是华电国际运行,并没有一个匹配绘制的华电国际文件夹,它应该是挑选的 MDPI 文件夹中的绘制无需缩放,使 scaleType 属性,以做的事情。或者,也许强制系统转到 nodpi 文件夹,如果没有找到匹配的绘制在各自的文件夹中的密度,可能的解决方案了。

What would be the optimal answer/solution then? In my opinion, if the device/emulator is running in hdpi and there isn't a matching drawable in the hdpi folder it should be pick the drawable from the mdpi folder without scaling it, allowing the scaleType attribute to do it's thing. Or maybe force the system to go to the nodpi folder if it doesn't find a matching drawable in the respective density folder, that could a solution too.

因此​​,如果任何人都不能提供一个解决方法/解决这个问题,那将是真正的正确答案。如果它曾经说到,我会改变接受的状态。

So, if anyone can ever provide a workaround/fix to this issue, that would be the real correct answer. If it ever comes to that, I'll change the accepted status.