StateListDrawable和平铺位图

8
这是我的自定义选择器(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>

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>

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>

位图也完全相同。高亮只是稍微浅一点,没有其他区别。两个位图都是PNG文件。

现在我设置

convertView.setBackgroundResource(R.drawable.list_item_background);

问题出在这里。我的common_cell_background不会重复,它会被拉伸。但是当我触摸列表中的单元格时,背景会更改为common_cell_background_highlight,猜猜发生了什么?一切都很好,它像应该一样重复了。我不知道问题出在哪里,为什么我的背景不会像高亮色一样重复。有什么想法吗?


同样的问题在这里。看起来是随机的:有时位图会正确重复,有时它会被拉伸。 - iseeall
同样的问题在这里。让我感到疯狂,请如果您找到解决方案,请发帖分享。谢谢! - dineth
请查看此链接:https://dev59.com/6msz5IYBdhLWcg3w5ME0#7615120 - a.ch.
@a.ch. 请将您的评论作为答案发布,我会将其标记为正确。 - kamil zych
1个回答

3
这是一个错误,已在ICS中修复,详见此答案:https://dev59.com/6msz5IYBdhLWcg3w5ME0#7615120 这里有一个解决方法:https://dev59.com/Fm855IYBdhLWcg3wcz_y#9500334 请注意,此解决方法仅适用于BitmapDrawable,对于其他类型的drawable,如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);
                }
            }
        }
    }
}

你如何将它应用到在xml中定义的Drawable? - Kirill Kulakov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接