简体   繁体   中英

Best way to store drawable list

I'm trying to create a little game. Now, I'm writting a kind of "init" file. Depending the level, I want to change the background (drawable). I have a class level and a method setBackgroung.

My setup file is build like that
-- 01 -- // level one
[BKG]:2130837520 // id of the drawable
-- 02 -- // level two
[BKG]:2130837525 // id of the drawable

I created a function to draw the background depending the level. It works, but I'm not sure that my solution is very nice. And I have for each level almost 20 different drawable

Thank you for your help.

If I were using default Android stuff I'd go with a LevelListDrawable . Just set up your drawable in an xml file like this:

<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="0" android:drawable="@drawable/level_1" />
    <item android:maxLevel="1" android:drawable="@drawable/level_2" />
    <item android:maxLevel="2" android:drawable="@drawable/level_3" />
    <item android:maxLevel="3" android:drawable="@drawable/level_4" />
</level-list>

Then set that as your background. As the player progresses just call

backgroundDrawable.setLevel(level);

and it will switch to the drawable associated with that level. Then, as you add levels, you'll just need to add a line in the xml file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM