2011-09-14 10 views
8

To jest mój zwyczaj selektor (StateListDrawable)StateListDrawable i kafelki bitmapy

<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> 

Zarówno common_cell_background i common_cell_background_highlight są XML. Kod poniżej:

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> 

bitmapy są również takie same. Podświetlenie jest tylko trochę lżejsze i nie ma innych różnic. Obie mapy bitowe to pliki PNG.

Teraz mogę ustawić

convertView.setBackgroundResource(R.drawable.list_item_background); 

i tu jest problem. Mój common_cell_background nie powtórzy się, jest rozciągnięty. Ale co jest zaskakujące, kiedy dotykam komórki z mojej listy zmienia się na common_cell_background_highlight i zgadnij co? Wszystko jest w porządku, powtarza się tak, jak powinno być. Nie mam pojęcia, gdzie jest problem, dlaczego moje tło nie powtarza się, gdy robi się podświetlenie. jakieś pomysły?

+0

ten sam problem tutaj. Wydaje się, że jest losowy: czasami bitmapa jest powtarzana poprawnie, czasami jest rozciągnięta na ten sam problem tutaj: – iseeall

+0

. doprowadza mnie do szału. proszę opublikuj rozwiązanie, jeśli je znajdziesz. Dziękuję! – dineth

+0

zobacz: http://stackoverflow.com/a/7615120/1037294 –

Odpowiedz

3

to jest bug, to została ustalona w ICS, zobacz tę odpowiedź: https://stackoverflow.com/a/7615120/1037294

o to obejście: https://stackoverflow.com/a/9500334/1037294

Uwaga, że ​​obejście ma zastosowanie tylko dla BitmapDrawable, dla innych typów kanału alfa jak StateListDrawable musisz wykonać dodatkową pracę. Oto, czego używam:

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

Dziękuję. W końcu to naprawiłem. –

+0

Jak zastosować go do Drawable zdefiniowany w xml? –