StateListDrawable和瓷砖位图

StateListDrawable和瓷砖位图

本文介绍了StateListDrawable和瓷砖位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

这是我的自定义选择器(StateListDrawable)

This is my custom selector (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。低于code:

Both, common_cell_background and common_cell_background_highlight are 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>

位图也完全相同。亮点是只是一点点轻,没有其他区别。这两个位图PNG文件。

Bitmaps are also exactly the same. Highlight is just a little bit lighter and there is no other differences. Both bitmaps are PNG files.

现在我设置

convertView.setBackgroundResource(R.drawable.list_item_background);

和这里的问题。我common_cell_background不重复,它是捉襟见肘。但是,当我谈谈我的名单背景变为细胞common_cell_background_highlight和猜测什么什么,令人惊奇?一切都很好,它的重复像它应该的。我不知道哪里出了问题,为什么我的背景不重复,而亮点呢。有什么想法?

and here is the problem. My common_cell_background doesn't repeat, it's stretched. But what is suprising when I touch on cell of my list background changes to common_cell_background_highlight and guess what? 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?

推荐答案

这是错误,它是固定在ICS,看到这样的回答:的

This is the bug, it was fixed in ICS, see this answer: http://stackoverflow.com/a/7615120/1037294

下面是一种变通方法:

Here is a workaround: http://stackoverflow.com/a/9500334/1037294

请注意,该解决方法仅适用于 BitmapDrawable ,其他类型的可绘像 StateListDrawable 你会需要做额外的工作。这里是我用什么:

Note, that the workaround is only applicable for BitmapDrawable, for other types of drawables like StateListDrawable you'll need to do extra work. 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);
                }
            }
        }
    }
}

这篇关于StateListDrawable和瓷砖位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:46