我已经使用以下代码在后台重复图像,但它不起作用可以帮助吗?
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"
>
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"/>
它显示出相同的小图像。它没有重复......
答案 0 :(得分: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);
}
}
}
答案 1 :(得分:9)
每次使用时都会创建grass_bg.xml
的副本(即grass_bg_2.xml
)。这对我有用,可以确保在重复使用相同的背景时tileMode
设置不会丢失。
答案 2 :(得分: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" />
如果你有任何进一步的说明,请说出来。
答案 3 :(得分: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...*/ }