平铺可绘制有时会拉伸
我有一个 ListView,其项目具有平铺背景。为了实现这一点,我使用以下可绘制 xml:
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/tile"
android:tileMode="repeat" />
通常,这是可行的。然而,有时,src 可绘制对象不会平铺,而是拉伸以填充整个列表项。 (我有几个像这样的不同图块,我将它们混合在一个 ListView 中。如果有拉伸而不是平铺,那么它永远不会同时出现在所有图块中,这是值得的。)
我还尝试添加 < code>android:dither="true" 到该 xml,因为我在某处读到,如果没有它,可能会出现错误。那并没有改变任何事情。
有人遇到过同样的问题吗?你是怎么修好的?
I have a ListView whose items have a tiled background. To accomplish this, I use the following drawable xml:
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/tile"
android:tileMode="repeat" />
Usually, this works. Sometimes, however, the src drawable isn't tiled, but stretched to fill the entire list item. (I've got several different tiles like this, and I use them mixed in one ListView. If there is stretching instead of tiling, it's never been in all of them at once, for what that's worth.)
I also tried to add android:dither="true"
to that xml, since I read somewhere that without it there might be bugs. That didn't change anything.
Has anyone had the same problem? How did you fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
网上有很多关于这个话题的讨论,以及各种(和大量)建议的解决方案。
资源为正方形、以 2 为底的维度。
即:xhdpi 图块资源为 16px x 16px。
我希望 Android 平台能够在位图没有完美镶嵌的情况下“过度平铺”来填充空间,然后修剪浪费。然而,在 mdpi、hdpi 和 xhdpi(以及 v2.3 到 v4.0)上尝试 10px*10px 平铺位图“不一致”地显示了这种拉伸。
当您在各种分辨率中前进以及每次创建视图时每个设备都尝试绘制图块时,以 2 为底的维度允许进行整体甚至均匀划分。
在 Android 开发中,我们与范围广泛的硬件和涉足平台的供应商进行竞争 - 有时这种微不足道的黑魔法确实有效。
这似乎至少为我解决了这个问题。值得一试。
There is a lot of noise about this topic online, with various (and numerous) suggested solutions.
resources to square, base-2 dimensions.
ie: 16px by 16px for an xhdpi tile asset.
I hoped that the Android platform would "over-tile" to fill a space if the bitmap did not tessellate perfectly - and then trim the waste. However trialling a 10px*10px tiled bitmap across mdpi, hdpi and xhdpi (and v2.3 to v4.0)'inconsistently' showed this stretching.
The base-2 dimension allows for whole and even division as you progress through the various resolutions and as each device tries to paint the tiles each time the view is created.
In Android development, we contest with the ranging hardware and the vendors dipping their fingers into the platform - sometimes this sort of trivial black magic just works.
This appears to have resolved the issue for me at least. Worth a shot.
这听起来像是一个错误,尽管我自己从未见过。如果您有一个简单的 APK 可以重现该问题,请将其发送给我 (romainguy /at/ android.com) 或提交错误 在这里。
This sounds like a bug, although I've never seen it myself. If you have a simple APK that reproduces the issue, please send it to me (romainguy /at/ android.com) or file a bug here.
此博客条目讨论该问题
与 Ivo van der Wijk 列出的 Tapas 的此解决方案相结合,它对我有用。
关键是从 XML 中删除平铺设置,然后在运行时将其设置为平铺。如果它们都设置为平铺,它对我不起作用。
编辑:实际上,我撒谎了。即使这样,它有时似乎也无法平铺。
如果有一个可靠的解决方法就太好了。
编辑2:将其设置为其他内容(例如CLAMPED),然后将其设置回到目前为止似乎有效。
This blog entry discusses the issue
combined with this solution from Tapas listed by Ivo van der Wijk, it works for me.
The key was to remove the tiled setting from the XML, then set it to tiled at runtime. It does not work for me if they are both set to tiled.
Edit: actually, I lied. Even with this it seems to sometimes fail to tile.
Would be very nice to have a reliable work-around.
Edit 2: setting it to something else (eg. CLAMPED) then setting it back so far seems to be working.
我也有同样的问题。我缺少的是我们需要在 imageview 中添加 scaletype 来 fitXY 以使 xml 位图正常工作。
tile_bitmap.xml
布局.xml
I was also having the same issue. What I was missing was that we need to add scaletype to fitXY in the imageview for the xml bitmap to work properly.
tile_bitmap.xml
layout.xml
我将图像从
drawable-xhdpi
移动到drawable
文件夹,一切都很好。I moved my image from
drawable-xhdpi
todrawable
folder and everything was fine.我也被这个问题困扰了。很难诊断,更难找到类似的报告和可用的解决方案。
freenode #android-dev irc 频道上的“Tapas”带有以下实用方法:
将其应用于所有具有平铺背景集的视图(例如 findViewById 它们)。
另外,我印象中这个错误是在 AndroidManifest.xml 中设置“anyDensity=true”后开始出现的
I also got bitten by this problem. Very hard to diagnose, even harder to find similar reports and usable solutions.
"Tapas" on the freenode #android-dev irc channel came with the following utility method:
Apply it to all Views that have a tiled background set (e.g. findViewById them).
Also, I have the impression this bug started acting up after setting "anyDensity=true" in AndroidManifest.xml
除了 CLAMP TileMode 之外,我刚刚遇到了完全相同的问题。我有一个位图,我想将其拉伸到底部并将其设置为 XML 定义的 BitmapDrawable,并且在图形预览窗口中,无论我将 ViewImage 设置为什么尺寸,它都会在顶部绘制位图然后重复最后一个像素以填充到末尾。
在模拟器和我自己的手机上的各种 SDK 上启动应用程序都会产生直接的“填充”类型失真,这是完全无用的。
事实证明,解决方案只是每次在代码中更改 ImageView 的大小时重新应用 TileMode:
现在一切都可以正常绘制了。所以是的,这对我来说看起来像是一个错误。
I've just had the exact same issue except with CLAMP TileMode. I have a bitmap that I want to then just stretch away at the bottom and have it set up as an XML defined BitmapDrawable and in the Graphical Preview window all looks fine, no matter what size I make the ViewImage it draws my bitmap at the top and then repeats the last pixels to fill to the end.
Launching the app on various SDK builds on the emulator and on my own phone all then produced a straight 'fill' type distortion which is completely useless.
The solution turned out to simply be to re-apply the TileMode every time I changed the size of the ImageView within my code:
Now it's all drawing fine. So yes, this looks like a bug to me.
由于我在这里没有看到链接,因此确认是Android 中的错误。它已在 ICS 中修复。有关更多详细信息,请参阅 XML 可绘制位图tileMode 错误?。
As I didn't see the link here, this was confirmed to be a bug in Android. It was fixed in ICS. See XML drawable Bitmap tileMode bug? for more details.