StateListDrawable和平铺位图

kam*_*ych 8 xml android bitmap repeat statelist

这是我的自定义选择器(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>
Run Code Online (Sandbox Code Playgroud)

common_cell_background和common_cell_background_highlight都是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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

位图也完全相同.亮点只是稍​​微轻一点,没有其他差异.两个位图都是PNG文件.

现在我开始了

convertView.setBackgroundResource(R.drawable.list_item_background);
Run Code Online (Sandbox Code Playgroud)

这是问题所在.我的common_cell_background没有重复,它已经拉长了.但是,当我触摸列表背景的单元格时,令人惊讶的是变为common_cell_background_highlight并猜测是什么?一切都很好,它应该像它应该重复.我不知道问题出在哪里,为什么我的背景不会重复突出显示.有什么想法吗?

a.c*_*ch. 3

这是错误,已在 ICS 中修复,请参阅此答案:/sf/answers/533058431/

这是一种解决方法:/sf/answers/665023411/

请注意,该解决方法仅适用于BitmapDrawable,对于其他类型的可绘制对象,例如StateListDrawable您需要做额外的工作。这是我使用的:

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);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)