background image not repeating in android layout

前端 未结 4 1180
庸人自扰
庸人自扰 2020-12-08 04:54

i\'ve used the following code to repeat the image in the background but its not working can any one help?

Layout.xml



        
相关标签:
4条回答
  • 2020-12-08 05:26

    Create a copy of grass_bg.xml for each time you use it (ie grass_bg_2.xml). This worked for me to assure the tileMode setting was not lost when the same background is used repeatedly.

    0 讨论(0)
  • 2020-12-08 05:30

    Bitmaps (and their states) get reused a lot, and I've found it's easy to lose the tileMode if a BitmapDrawable is used in more than one place. The following code fixes the problem for me:

     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);
               }
          }
     }
    
    0 讨论(0)
  • 2020-12-08 05:37
        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...*/ }
    
    0 讨论(0)
  • 2020-12-08 05:42

    I faced the same issue but decided to investigate bit deeper. The cause was that I noticed one of my drawable works all the time, while the other is always broken. The trick is one image was made out of the another with just minior changes to colors and alpha. Drawables' XML is identical beside reference to the PNG. So I took pnginfo to see what's there.

    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
    

    the diagstripe_yellow.png works, while diagstripe_dark.png does not, and if I replace references to it with reference to diagstripe_yellow.png, then it works (at least on 2.2.1 I got here) So the major differences are:

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

    First try was to to disable interlacing, with no luck, even when header looks same:

    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
    

    If anyone bothers to dig deeper here are the files: http://webnetmobile.com/files/ or use base64 tool to decode files from the quotes below:

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

    Speak up if you got any further notes.

    0 讨论(0)
提交回复
热议问题