Tiled drawable sometimes stretches

2019-01-07 06:08发布

I have a ListView whose items have a tiled background. To accomplish this, I use the following drawable xml:

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/tile"
    android:tileMode="repeat" />

Usually, this works. Sometimes, however, the src drawable isn't tiled, but stretched to fill the entire list item. (I've got several different tiles like this, and I use them mixed in one ListView. If there is stretching instead of tiling, it's never been in all of them at once, for what that's worth.)

I also tried to add android:dither="true" to that xml, since I read somewhere that without it there might be bugs. That didn't change anything.

Has anyone had the same problem? How did you fix it?

9条回答
看我几分像从前
2楼-- · 2019-01-07 06:43

This blog entry discusses the issue

combined with this solution from Tapas listed by Ivo van der Wijk, it works for me.

The key was to remove the tiled setting from the XML, then set it to tiled at runtime. It does not work for me if they are both set to tiled.

Edit: actually, I lied. Even with this it seems to sometimes fail to tile.

Would be very nice to have a reliable work-around.

Edit 2: setting it to something else (eg. CLAMPED) then setting it back so far seems to be working.

查看更多
成全新的幸福
3楼-- · 2019-01-07 06:50

I moved my image from drawable-xhdpi to drawable folder and everything was fine.

查看更多
疯言疯语
4楼-- · 2019-01-07 06:50

I was also having the same issue. What I was missing was that we need to add scaletype to fitXY in the imageview for the xml bitmap to work properly.

tile_bitmap.xml

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/tile"
    android:tileMode="repeat" />

layout.xml

<ImageView
    android:layout_width="match_parent"
    android:src="@drawable/tile_bitmap"
    android:layout_height="match_parent"
    android:scaleType="fitXY"/>
查看更多
登录 后发表回答