2010-11-02 65 views
21

我已經使用下面的代碼在後臺重複圖像,但它不工作可以幫助任何人?在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" 
    > 

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"/> 

它顯示了相同的小圖像。它不重複...

+0

它適合我用您提供的代碼,是在另一個佈局的佈局或什麼? – SteD 2010-11-02 13:06:02

+0

https://maxalley.wordpress.com/2012/09/27/android-repeating-background-image/ – boctulus 2015-04-02 16:56:34

回答

9

每次使用時間(即grass_bg_2.xml)用於創建的grass_bg.xml副本。這對我來說確保tileMode設置在重複使用相同背景時不會丟失。

+1

嘿,我經歷過類似的奇怪問題,需要重命名drawable才能解決。任何想法爲什麼這是這種情況? – 2011-11-13 02:00:46

+0

它只在Kindle上發生 – 2012-11-28 12:14:25

35

位圖(及其狀態)得到重用很多,我發現如果在多個位置使用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); 
      } 
     } 
} 
+2

看來這個bug在Honeycomb中得到修復。這意味着只有薑餅和更早的時候才需要解決方法。 – Janusz 2012-05-02 07:41:24

+0

不幸的是,我仍然可以看到果凍豆的問題。但非常非常罕見。我不知道如何實際觸發它。我只知道複雜的嵌套佈局似乎對它有影響。無論如何,+1,因爲這裏的解決方案工作。非常感謝你! – tiguchi 2012-11-28 19:42:16

+0

@Janusz我剛剛在Honeycomb(3.2 Galaxy Tab)上遇到了這個問題,所以我只能說它只在Honeycomb和更早版本中需要 – 2013-05-15 07:54:54

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 

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" /> 

講,如果你有任何進一步的說明。

0
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...*/ }