Setting ImageView ScaleType to quot;centerquot; is not working as I expect it(将 ImageView ScaleType 设置为“center;没有按我的预期工作)
问题描述
我的 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
分别等于 ldpi
、mdpi
和 hdpi
中的 36px、48px 和 72px.ImageView
中将使用的所有图像基本上都是图标,我在网上找到了我想要的应用程序(免费许可).但不幸的是,它没有提供大于 48px 的图像,并且使用不同的图像是不可能的.所以我需要一个新的解决方案...
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 和 48px 的图像.但是我遇到了 hdpi
的问题.对于在 hdpi
上运行的设备,我想要的是让他们使用 mdpi
图像(可用的更大的图像)并按原样使用它,而无需缩放.基本上,hdpi
的 ImageView
是 72px,所以我希望 48px 的图像位于 72px 的中间,没有缩放.为此,我只是尝试将上面 ImageView
中的 scaleType
更改为 center
,但无论如何图像都会被缩放.
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
图像以各自的密度使用,但让运行 hdpi
的设备拾取可用的最大图像(即mdpi
的)并防止任何缩放,只是将图像放在 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.
但要使上述内容按预期工作,drawable 必须放在 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
密度文件夹中有一个drawable(这里X
表示,l
、m
、h
甚至xh
). - 您正在运行应用设备/模拟器,例如,具有
hdpi
密度,但在hdpi
文件夹,系统从nodpi
文件夹(并不总是知道它会选择哪个文件夹来自).
- You are running the app on a device/emulator with
Xdpi
density and there is a drawable in theXdpi
density folder (hereX
means,l
,m
,h
or evenxh
). - You are running the app device/emulator,
for instance, with
hdpi
density, but there isn't a drawable in thehdpi
folder and the system picks the alternative drawable from thenodpi
folder (it's not always known which folder it will pick from).
当它不起作用时:
- 您正在运行应用设备/模拟器,例如,使用
hdpi
密度,但hdpi
文件夹中没有可绘制对象,并且系统从任何其他密度中选择替代可绘制对象文件夹(不是nodpi
文件夹),drawable 将缩放到ImageView
边界和scaleType
属性不会做任何东西.
- You are running the app device/emulator, for instance, with
hdpi
density, but there isn't a drawable in thehdpi
folder and the system picks the alternative drawable from any other of the density folders (not thenodpi
folder), the drawable will be scaled to theImageView
bounds and thescaleType
attribute will not do anything.
总之,我的问题没有正确"答案,这实际上取决于您要达到的目标.回答 my 问题虽然,我只需要做两件事:a) 将 ImageView
scaleType
设置为 centerInside
b) 将 mdpi
文件夹中的所有可绘制对象复制到 hdpi
文件夹中(如上所述,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).
当然,复制drawables不是最佳的,但我找不到任何其他解决方案,到目前为止,也没有其他人可以......所以,我暂时将这个标记为已接受.
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.
那么最佳答案/解决方案是什么?
在我看来,如果设备/模拟器在 hdpi
中运行并且 hdpi
文件夹中没有匹配的可绘制对象,则应该从 中选择可绘制对象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.
这篇关于将 ImageView ScaleType 设置为“center";没有按我的预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 ImageView ScaleType 设置为“center";没有按我的预期工作
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01