在android布局中不重复的背景图像

suj*_*ith 23 android

我已经使用以下代码在后台重复图像,但它无法正常工作可以帮助吗?

Layout.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"
    >
Run Code Online (Sandbox Code Playgroud)

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"/>
Run Code Online (Sandbox Code Playgroud)

它显示出相同的小图像.它不重复......

Ale*_*umb 36

位图(及其状态)被重用了很多,我发现如果在多个地方使用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);
           }
      }
 }
Run Code Online (Sandbox Code Playgroud)

  • 似乎这个bug在Honeycomb中得到修复.这意味着仅在Gingerbread及更早版本中需要解决方法. (2认同)

Old*_*664 9

grass_bg.xml每次使用时创建一份副本(即grass_bg_2.xml).这对我tileMode有用,以确保在重复使用相同背景时设置不会丢失.


Mar*_*ski 6

我遇到了同样的问题,但决定深入研究.原因是我一直注意到我的一个可绘制的作品,而另一个总是被打破.诀窍是一个图像是由另一个图像制成的,只需对颜色和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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

Channels (Samples/Pixel):
Pixel depth (Pixel Depth):
Colour Type (Photometric Interpretation):
Interlacing:
Run Code Online (Sandbox Code Playgroud)

首先尝试禁用隔行扫描,没有运气,即使标题看起来相同:

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
Run Code Online (Sandbox Code Playgroud)

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

diagstripe_yellow.png:

iVBORw0KGgoAAAANSUhEUgAAABIAAAAeCAYAAAAhDE4sAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAL
EwAACxMBAJqcGAAAAAd0SU1FB9wCEg8JKbHU3pgAAAAdaVRYdENvbW1lbnQAAAAAAENyZWF0ZWQg
d2l0aCBHSU1QZC5lBwAAAE5JREFUSMdj7OnpqWdgYGCQft3S8FS0poFcNhM1DHkqWtPAuLxc4D+l
hjAwMDAwWwa2MIx6bdRro14b9dqo10a9Nuo1Gnstj4GBQYgSAwG9j8m8FwE2EgAAAABJRU5ErkJg
gg==
Run Code Online (Sandbox Code Playgroud)

diagstripe_dark.png:

iVBORw0KGgoAAAANSUhEUgAAABIAAAAeCAIAAAHZaentAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAL
EwAACxMBAJqcGAAAAAd0SU1FB9wCDww0GV3Ql5EAAAAdaVRYdENvbW1lbnQAAAAAAENyZWF0ZWQg
d2l0aCBHSU1QZC5lBwAAAGVJREFUOMvtkjsSgCAMRFfvfwOiV30WMCBqKFJIQ8XO/tgiAo6UAOUH
2ABJp5mqWri98B3ZXBmoogx0F4GX3w3LrQnZHju61Cfb6j15RqebG/23On/tHMiRkwheyxq5Rs4Z
aRZIXsBYcInPMeOmAAAAAElFTkSuQmCC
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

如果你有任何进一步的说明,请说出来.