【问题标题】:background image not repeating in android layout背景图像在android布局中不重复
【发布时间】:2011-05-03 21:53:12
【问题描述】:

我已使用以下代码在后台重复图像,但它不工作任何人都可以帮忙吗?

布局.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/grass_bg"
    >

drawable 中的grass_bg.xml 看起来像这样

<?xml version="1.0" encoding="utf-8"?>
    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/grass_small"
        android:tileMode="repeat"/>

它显示相同的小图像。它没有重复......

【问题讨论】:

标签: android


【解决方案1】:

位图(及其状态)经常被重复使用,我发现如果在多个地方使用 BitmapDrawable,很容易丢失 tileMode。以下代码为我解决了这个问题:

 public static void fixBackgroundRepeat(View view) {
      Drawable bg = view.getBackground();
      if(bg != null) {
           if(bg instanceof BitmapDrawable) {
                BitmapDrawable bmp = (BitmapDrawable) bg;
                bmp.mutate(); // make sure that we aren't sharing state anymore
                bmp.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
           }
      }
 }

【讨论】:

  • 这个错误似乎在 Honeycomb 中已修复。这意味着只有在 Gingerbread 和更早版本中才需要解决方法。
  • 不幸的是,我仍然可以在 Jelly Bean 上看到这个问题。但非常非常罕见。我不知道如何真正触发它。我只知道复杂的嵌套布局似乎对它有影响。无论如何,+1 因为这个解决方案在这里有效。非常感谢!
  • @Janusz 我刚刚在 Honeycomb (3.2 Galaxy Tab) 上遇到了这个问题,所以我想说它只在 Honeycomb 和更早版本中需要
【解决方案2】:

为您每次使用它创建一个grass_bg.xml 的副本(即grass_bg_2.xml)。这对我来说可以确保在重复使用相同背景时不会丢失 tileMode 设置。

【讨论】:

  • 嘿,我遇到过类似的奇怪问题,需要重命名可绘制对象才能解决。知道为什么会这样吗?
  • 它只发生在我的 Kindle 上
【解决方案3】:

我遇到了同样的问题,但决定进行更深入的调查。原因是我一直注意到我的一个可绘制作品,而另一个总是坏掉。诀窍是一个图像是由另一个图像制成的,只是对颜色和 alpha 进行了微小的更改。除了引用 PNG 之外,Drawables 的 XML 是相同的。所以我拿了 pnginfo 来看看那里有什么。

diagstripe_dark.png:

Image Width: 18 Image Length: 30
Bitdepth (Bits/Sample): 8
Channels (Samples/Pixel): 3
Pixel depth (Pixel Depth): 24
Colour Type (Photometric Interpretation): RGB
Image filter: Single row per byte filter
Interlacing: Adam7 interlacing
Compression Scheme: Deflate method 8, 32k window
Resolution: 2835, 2835 (pixels per meter)
FillOrder: msb-to-lsb
Byte Order: Network (Big Endian)
Number of text strings: 0 of 0

diagstripe_yellow.png:

Image Width: 18 Image Length: 30
Bitdepth (Bits/Sample): 8
Channels (Samples/Pixel): 4
Pixel depth (Pixel Depth): 32
Colour Type (Photometric Interpretation): RGB with alpha channel
Image filter: Single row per byte filter
Interlacing: No interlacing
Compression Scheme: Deflate method 8, 32k window
Resolution: 2835, 2835 (pixels per meter)
FillOrder: msb-to-lsb
Byte Order: Network (Big Endian)
Number of text strings: 0 of 0

diagstripe_yellow.png 有效,而 diagstripe_dark.png 无效,如果我用 diagstripe_yellow.png 替换对它的引用,那么它可以工作(至少在 2.2.1 我到这里)所以主要区别是:

Channels (Samples/Pixel):
Pixel depth (Pixel Depth):
Colour Type (Photometric Interpretation):
Interlacing:

第一次尝试是禁用隔行扫描,但没有运气,即使标题看起来相同:

diagstripe_dark-2.png:

Image Width: 18 Image Length: 30
Bitdepth (Bits/Sample): 8
Channels (Samples/Pixel): 4
Pixel depth (Pixel Depth): 32
Colour Type (Photometric Interpretation): RGB with alpha channel
Image filter: Single row per byte filter
Interlacing: No interlacing
Compression Scheme: Deflate method 8, 32k window
Resolution: 0, 0 (unit unknown)
FillOrder: msb-to-lsb
Byte Order: Network (Big Endian)
Number of text strings: 0 of 0

如果有人想深入挖掘这里的文件:http://webnetmobile.com/files/ 或使用 base64 工具从下面的引号中解码文件:

diagstripe_yellow.png:

iVBORw0KGgoAAAANSUhEUgAAABIAAAAeCAYAAAAhDE4sAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAL
EwAACxMBAJqcGAAAAAd0SU1FB9wCEg8JKbHU3pgAAAAdaVRYdENvbW1lbnQAAAAAAENyZWF0ZWQg
d2l0aCBHSU1QZC5lBwAAAE5JREFUSMdj7OnpqWdgYGCQft3S8FS0poFcNhM1DHkqWtPAuLxc4D+l
hjAwMDAwWwa2MIx6bdRro14b9dqo10a9Nuo1Gnstj4GBQYgSAwG9j8m8FwE2EgAAAABJRU5ErkJg
gg==

diagstripe_dark.png:

iVBORw0KGgoAAAANSUhEUgAAABIAAAAeCAIAAAHZaentAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAL
EwAACxMBAJqcGAAAAAd0SU1FB9wCDww0GV3Ql5EAAAAdaVRYdENvbW1lbnQAAAAAAENyZWF0ZWQg
d2l0aCBHSU1QZC5lBwAAAGVJREFUOMvtkjsSgCAMRFfvfwOiV30WMCBqKFJIQ8XO/tgiAo6UAOUH
2ABJp5mqWri98B3ZXBmoogx0F4GX3w3LrQnZHju61Cfb6j15RqebG/23On/tHMiRkwheyxq5Rs4Z
aRZIXsBYcInPMeOmAAAAAElFTkSuQmCC

stripes.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:antialias="false"
    android:filter="false"
    android:src="@drawable/diagstripe_yellow"
    android:tileMode="repeat" />

如果您有任何进一步的说明,请说出来。

【讨论】:

    【解决方案4】:
        try{ BitmapDrawable background = (BitmapDrawable) myView.getBackground();
        background.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); }
        catch(Exception e) { /*Do nothing; background is not BitmapDrawable; can be a color or null...*/ }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-05
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多