Tiled drawable sometimes stretches

本文提供了解决Android ListView中瓷砖背景图偶发拉伸为整个列表项的问题的方法,包括一个实用的修复函数和可能的原因分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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.



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


 
 
 
Thanks, it helped, at least for now. –   jki  Apr 16 '12 at 18:53
 
This solution works. Thank you :) –   dineth  Jun 14 '12 at 0:35
1  
How do I apply it to a drawable defined in xml? I use this drawable within a <selector > –   Kirill Kulakov  Jan 31 '13 at 13:56
 
This solution works for me too, but view background was not set strangely so I have to add view.setBackgroundResource(R.drawable.bg_striped_img); before view.getBackground(); . Thank you :) –  letroll  Mar 11 '13 at 12:26 
 
Thanks,it works for me –   penghaitao  Aug 30 '13 at 2:29
add comment

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.


http://stackoverflow.com/questions/4336286/tiled-drawable-sometimes-stretches/5852198#5852198

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值