2011-09-14 21 views
8

これは私のカスタムセレクタ(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> 

両方で、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の背景が変わって、何が推測されたのだろうか?すべてがうまいです、それはそうであるように繰り返されます。問題がどこにあるのか分からず、ハイライトしている間に自分の背景が繰り返されないのはなぜですか。何かご意見は?

+0

ここで同じ問題があります。ランダムであるように見えます。ビットマップが正しく繰り返されることがあります。時にはそれが伸びている場合もあります。 – iseeall

+0

ここで同じ問題が発生します。私をナットにしている。見つけたら解決策を投稿してください。ありがとう! – dineth

+0

これを参照してください:http://stackoverflow.com/a/7615120/1037294 –

答えて

3

これはバグです、それはICSに固定し、この回答を参照してください。ここでhttps://stackoverflow.com/a/7615120/1037294

は、回避策です:https://stackoverflow.com/a/9500334/1037294

注意、回避策はドロウアブルの他のタイプのため、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); 
       } 
      } 
     } 
    } 
} 
+0

ありがとうございました。最後にそれを固定しました。 –

+0

xmlで定義されたDrawableにどのように適用しますか? –

関連する問題