java - Cycle random images in Android -


i'm beginner android / java development. started learning programming , easiest way me modify open source projects understand principles involved.

anyways, want simple modification bubble shoot game, 5-10 different background images cycled randomly:

  • when every new level started (but remain same when level restarted)
  • or easier: background changing every single time when level restarting or starting

i defined string array of background drawables:

<?xml version="1.0" encoding="utf-8"?> <resources>     <string-array name="random_background"></string-array>         <item name="background_01">@drawable/background01</item>         <item name="background_02">@drawable/background02</item>         <item name="background_03">@drawable/background03</item>         <item name="background_04">@drawable/background04</item>         <item name="background_05">@drawable/background05</item>         <item name="background_06">@drawable/background06</item>         <item name="background_07">@drawable/background07</item>         <item name="background_08">@drawable/background08</item>         <item name="background_09">@drawable/background09</item>         <item name="background_10">@drawable/background10</item>  </resources> 

the original code define background on gameview.java in src folder:

  mbackgroundorig =     bitmapfactory.decoderesource(res, r.drawable.background, options); 

please me create easy way idea.

also have few questions:

  • the xml file must in res/values correct ? can use file name ? example: random.xml
  • i got error xml file: error: 'type' attribute required item need setup type , format each item ? if yes, type , format please ?
  • can avoid exceeding video memory limit ? mean load current background, , after each use flush out before new random image displayed.

notes: bubble shoot source code: https://code.google.com/p/bubble-shoot/

background pictures file names: background01.jpg, background02.jpg, etc.

thank much,

paul

since string array indexed numbers it's easy you're describing , use whatever images like.

assuming you're careful type out names in drawables folder.

so array (which doesn't need named items) like

<string-array name="myimages">     <item>foo</item>     <item>bar</item>     <item>image7</item>     <item>king_kong</item> </string-array> 

and forth. drawables need contain images foo.jpg, bar.jpg, etc.

now in code pull random number between 0 , length of array , drawable this:

int randomnum = math.floor(math.random() * getresources().getstringarray(r.array.myimages).length); field field = r.drawable.class.getfield(getresources().getstringarray(r.array.myimages)[randomnum]); int imgid = field.getint(null);  myimg.setimgresource(imgid); 

i'm writing memory, may have typos, general idea.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -