XML drawable Bitmap tileMode bug?
Asked Answered
C

2

17

I'm having issues where my drawable resource uses the tileMode repeat. In some cases the image is just stretched and sometimes it is properly repeated.

The following are the xml files I use to create the button states:

Image drawable used for tile repeated

Image drawable used for tile repeated

^^^^^^^^^^^^^

btn_menu_item.xml

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:constantSize="true" android:visible="true" android:variablePadding="true">
    <!-- selected -->
    <item 
        android:state_selected="true" 
        android:drawable="@drawable/menu_item_selected"/>

    <!-- focused -->
    <item 
        android:state_focused="true" 
        android:drawable="@drawable/menu_item_pressed"/>

    <!-- pressed -->
    <item 
        android:state_pressed="true" 
        android:drawable="@drawable/menu_item_pressed"/>

    <!-- normal -->
    <item 
        android:state_pressed="false" 
        android:state_focused="false" 
        android:drawable="@drawable/menu_item_normal"/>

</selector>

menu_item_normal.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <gradient 
                android:startColor="#757575" 
                android:endColor="#929292" 
                android:angle="90"/>
        </shape>    
    </item>

    <item>
        <bitmap 
            android:src="@drawable/menu_lines_texture"  
            android:tileMode="repeat"
            android:dither="true"/>
    </item>
</layer-list>

menu_item_pressed.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <gradient 
                android:startColor="#dd4e00" 
                android:endColor="#c64600" 
                android:angle="90"/>
        </shape>
    </item>

    <item>
        <bitmap 
            android:src="@drawable/menu_lines_texture"  
            android:tileMode="repeat"
            android:dither="true"/>
    </item>
</layer-list>

Please see the images below of what exactly I'm talking about.

Normal state image properly repeated Pressed state image not repeated but stretched

Carrington answered 28/9, 2011 at 16:9 Comment(0)
E
22

This is a known bug, partially fixed in Android 3.0 and completely fixed in ICS.

Execrate answered 30/9, 2011 at 19:25 Comment(6)
Thanks for your reply on this! Is there a workaround for this?Carrington
I am still finding this problem in 3.2 on the Xoom. Setting it in code fixes it.Zurheide
Like I said, completely fixed in ICS :)Execrate
Can you point what the problem was so we could think about non-code workaround? Or is this utopia?Eyla
How can you set the tiling mode from code in case of a statelist drawable?Vigilant
I tried to horizontally tile a 50 (wide) x 3000 (high) pixel image and Android reported it can't make a texture that big. Then I fired up photoshop, manually tiled it into a 1000x3000 image and there's no problem to show it...Sheets
T
2

We were having a similar problem writing for 3.2 on Sony's Google TV device. We noticed some very similar background-striping stemming from using android:tileMode="repeat" on a bitmap as a background image.

In this case, the fix was to turn off hardware acceleration on the view that contained the bitmap like so (from a function in our Activity):

View tile_holder = this.findViewById(R.id.tile_holder);
tile_holder.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Tades answered 20/11, 2012 at 19:15 Comment(1)
I can vouch for this. Problem still exists on older phones running Lollipop, setting the tile mode programmatically didn't work but this did (via the containing layout file).Slumberous

© 2022 - 2024 — McMap. All rights reserved.