background image not repeating in android layout

前端 未结 4 1209
庸人自扰
庸人自扰 2020-12-08 04:54

i\'ve used the following code to repeat the image in the background but its not working can any one help?

Layout.xml



        
4条回答
  •  青春惊慌失措
    2020-12-08 05:30

    Bitmaps (and their states) get reused a lot, and I've found it's easy to lose the tileMode if a BitmapDrawable is used in more than one place. The following code fixes the problem for me:

     public static void fixBackgroundRepeat(View view) {
          Drawable bg = view.getBackground();
          if(bg != null) {
               if(bg instanceof BitmapDrawable) {
                    BitmapDrawable bmp = (BitmapDrawable) bg;
                    bmp.mutate(); // make sure that we aren't sharing state anymore
                    bmp.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
               }
          }
     }
    

提交回复
热议问题