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:30

There is a lot of noise about this topic online, with various (and numerous) suggested solutions.

  • If you're still at a loss, my suggestion is to keep all tiled bitmap resources to square, base-2 dimensions.

ie: 16px by 16px for an xhdpi tile asset.

I hoped that the Android platform would "over-tile" to fill a space if the bitmap did not tessellate perfectly - and then trim the waste. However trialling a 10px*10px tiled bitmap across mdpi, hdpi and xhdpi (and v2.3 to v4.0)'inconsistently' showed this stretching.

The base-2 dimension allows for whole and even division as you progress through the various resolutions and as each device tries to paint the tiles each time the view is created.

In Android development, we contest with the ranging hardware and the vendors dipping their fingers into the platform - sometimes this sort of trivial black magic just works.

This appears to have resolved the issue for me at least. Worth a shot.

查看更多
Luminary・发光体
3楼-- · 2019-01-07 06:37

I also got bitten by this problem. Very hard to diagnose, even harder to find similar reports and usable solutions.

"Tapas" on the freenode #android-dev irc channel came with the following utility method:

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);
        }
    }
}

Apply it to all Views that have a tiled background set (e.g. findViewById them).

Also, I have the impression this bug started acting up after setting "anyDensity=true" in AndroidManifest.xml

查看更多
Animai°情兽
4楼-- · 2019-01-07 06:38

Still suffered from this problem on older devices running Lollipop, occurs on orientation changes. Setting the tile mode programmatically didn't work but this answer did: https://stackoverflow.com/a/13480444/658727

查看更多
We Are One
5楼-- · 2019-01-07 06:41

This sounds like a bug, although I've never seen it myself. If you have a simple APK that reproduces the issue, please send it to me (romainguy /at/ android.com) or file a bug here.

查看更多
虎瘦雄心在
6楼-- · 2019-01-07 06:43

I've just had the exact same issue except with CLAMP TileMode. I have a bitmap that I want to then just stretch away at the bottom and have it set up as an XML defined BitmapDrawable and in the Graphical Preview window all looks fine, no matter what size I make the ViewImage it draws my bitmap at the top and then repeats the last pixels to fill to the end.

Launching the app on various SDK builds on the emulator and on my own phone all then produced a straight 'fill' type distortion which is completely useless.

The solution turned out to simply be to re-apply the TileMode every time I changed the size of the ImageView within my code:

((BitmapDrawable)ascender.getDrawable()).setTileModeY(TileMode.CLAMP);

Now it's all drawing fine. So yes, this looks like a bug to me.

查看更多
唯我独甜
7楼-- · 2019-01-07 06:43

As I didn't see the link here, this was confirmed to be a bug in Android. It was fixed in ICS. See XML drawable Bitmap tileMode bug? for more details.

查看更多
登录 后发表回答