简体   繁体   English

StateListDrawable和平铺的位图

[英]StateListDrawable and tiled bitmap

This is my custom selector (StateListDrawable) 这是我的自定义选择器(StateListDrawable)

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/common_cell_background" />
    <item
        android:state_pressed="true"
        android:drawable="@drawable/common_cell_background_highlight" />
    <item
        android:state_focused="true"
        android:drawable="@drawable/common_cell_background_highlight" />
    <item
        android:state_selected="true"
        android:drawable="@drawable/common_cell_background_highlight" />
</selector>

Both, common_cell_background and common_cell_background_highlight are XML. common_cell_background和common_cell_background_highlight都是XML。 Code below: 代码如下:

common_cell_background.xml common_cell_background.xml

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

common_cell_background_highlight.xml common_cell_background_highlight.xml

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

Bitmaps are also exactly the same. 位图也完全相同。 Highlight is just a little bit lighter and there is no other differences. 高光只是更轻一点,没有其他差异。 Both bitmaps are PNG files. 两个位图都是PNG文件。

Now I set 现在我开始

convertView.setBackgroundResource(R.drawable.list_item_background);

and here is the problem. 这是问题所在。 My common_cell_background doesn't repeat, it's stretched. 我的common_cell_background不再重复,它被拉伸了。 But what is suprising when I touch on cell of my list background changes to common_cell_background_highlight and guess what? 但是,当我触摸列表背景的单元格时,令人惊讶的是更改为common_cell_background_highlight并猜出什么吗? Everything is fine, it's repeated like it should be. 一切都很好,可以重复进行。 I have no idea where is the problem, why my background doesn't repeat while highlight does. 我不知道问题出在哪里,为什么突出显示时背景不重复。 Any thoughts? 有什么想法吗?

This is the bug, it was fixed in ICS, see this answer: https://stackoverflow.com/a/7615120/1037294 这是错误,已在ICS中修复,请参见以下答案: https : //stackoverflow.com/a/7615120/1037294

Here is a workaround: https://stackoverflow.com/a/9500334/1037294 这是一种解决方法: https : //stackoverflow.com/a/9500334/1037294

Note, that the workaround is only applicable for BitmapDrawable , for other types of drawables like StateListDrawable you'll need to do extra work. 请注意,该解决方法仅适用于BitmapDrawable ,对于其他类型的可绘制对象(如StateListDrawable您需要做一些额外的工作。 Here is what I use: 这是我使用的:

public static void fixBackgrndTileMode(View view, TileMode tileModeX, TileMode tileModeY) {
    if (view != null) {
        Drawable bg = view.getBackground();

        if (bg instanceof BitmapDrawable) {
            BitmapDrawable bmp = (BitmapDrawable) bg;
            bmp.mutate(); // make sure that we aren't sharing state anymore
            bmp.setTileModeXY(tileModeX, tileModeY);
        }
        else if (bg instanceof StateListDrawable) {
            StateListDrawable stateDrwbl = (StateListDrawable) bg;
            stateDrwbl.mutate(); // make sure that we aren't sharing state anymore

            ConstantState constantState = stateDrwbl.getConstantState();
            if (constantState instanceof DrawableContainerState) {
                DrawableContainerState drwblContainerState = (DrawableContainerState)constantState;
                final Drawable[] drawables = drwblContainerState.getChildren();
                for (Drawable drwbl : drawables) {
                    if (drwbl instanceof BitmapDrawable)
                        ((BitmapDrawable)drwbl).setTileModeXY(tileModeX, tileModeY);
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM