Tiled drawable sometimes stretches

前端 未结 8 629
余生分开走
余生分开走 2020-11-29 20:59

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



        
8条回答
  •  温柔的废话
    2020-11-29 21:35

    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

提交回复
热议问题