Displaying dynamic images based on an Int value (for showing player's score on main menu) – JAVA/XML/Android/Eclipse -
disclaimer: i’m extremely new java/xml, please bear me. i’m using replica island source if there’s easier/newer way of doing this, please let me know.
objective: display player’s score on main menu screen using .png numbers instead of text. why? because .png numbers more flashy/stylized standard text.
currently: i’m capturing , displaying player’s score via int variable that’s saved , loaded in sharedpreferences. i’m displaying text via following:
current xml:
<textview android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:paddingtop="10dp" android:textsize="24sp" android:textcolor= "#ff0000" android:background="#99000000" />
current mainmenuactivity class:
int mhighscorecoin = prefs.getint(preferenceconstants.preference_coins_collected, 0); textview text = (textview) findviewbyid(r.id.text); text.settext("current high score: " + mhighscorecoin);
this works fine, don’t want basic text on main menu. during actual gameplay, i’m using following method convert int score, array calls .png numbers (0-9) display them in game:
public int inttodigitarray(int value, int[] digits) { int charactercount = 1; if (value >= 1000) {charactercount = 4;} else if (value >= 100) {charactercount = 3;} else if (value >= 10) {charactercount = 2;} int remainingvalue = value; int count = 0; { int index = remainingvalue != 0 ? remainingvalue % 10 : 0; remainingvalue /= 10; digits[charactercount - 1 - count] = index; count++; } while (remainingvalue > 0 && count < digits.length); if (count < digits.length) {digits[count] = -1;} return charactercount; }
question: how can display high score in xml/mainmenuactivity class, using .png version of numbers instead of text i’m using?
the start of feeble attempt:
… mdrawscore = findviewbyid(r.id.drawscore); … if(mdrawscore !=null){ hudsystem hud = new hudsystem(); int[] mcoindigitsformenu; mcoindigitsformenu = new int[4]; hud.inttodigitarray(mhighscorecoin, mcoindigitsformenu); }
note: question may unanswerable information provided. again, i’m extremely new java , in on head. example, need pulling ton more stuff “hudsystem” finish creating array of images?
any details or general thoughts appreciated.
wwwhhhhelp, after hours , hours of reading (thanks stack overflow!) , trial , error, i've answered own question.
is cleanest, effective approach? highly unlikely (again, have no idea wtf i'm doing, lol). regardless, works, :). tweaked further here's general concept.
xml
<linearlayout android:layout_width="fill_parent" android:layout_height="70dp" android:orientation="horizontal" android:gravity="bottom|center_horizontal" android:id="@+id/mainsectionmenu" > <imageview android:id="@+id/drawscore" android:layout_width="70dp" android:layout_height="70dp" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:paddingtop="5dp" android:layout_marginright="-9dp" android:clickable="false" /> <imageview android:id="@+id/drawscore2" android:layout_width="70dp" android:layout_height="70dp" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:paddingtop="5dp" android:layout_marginleft="-9dp" android:layout_marginright="-9dp" android:clickable="false" /> <imageview android:id="@+id/drawscore3" android:layout_width="70dp" android:layout_height="70dp" android:orientation="horizontal" android:layout_gravity="center_horizontal" android:paddingtop="5dp" android:layout_marginleft="-9dp" android:clickable="false" /> </linearlayout>
mainmenuactivity
after pulling players mhighscorecoin sharedpreferences (oncreate , onresume), run following:
if(mhighscorecoin >99){ int hundsdigit = mhighscorecoin/100; imageview imageviewhunds = (imageview)findviewbyid(r.id.drawscore); int hunds = hundsdigit; string.valueof(hunds); int residhunds = getresources().getidentifier("ui_" + string.valueof(hunds), "drawable", getpackagename()); imageviewhunds.setimagedrawable(getresources().getdrawable(residhunds)); int tensdigit = ((mhighscorecoin / 10)%10); imageview imageviewtens = (imageview)findviewbyid(r.id.drawscore2); int tens = tensdigit; string.valueof(tens); int residtens = getresources().getidentifier("ui_" + string.valueof(tens), "drawable", getpackagename()); imageviewtens.setimagedrawable(getresources().getdrawable(residtens)); int onesdigit = (mhighscorecoin % 10); imageview imageviewones = (imageview)findviewbyid(r.id.drawscore3); int ones = onesdigit; string.valueof(ones); int residones = getresources().getidentifier("ui_" + string.valueof(ones), "drawable", getpackagename()); imageviewones.setimagedrawable(getresources().getdrawable(residones)); } else if(mhighscorecoin > 9) { int tensdigit = mhighscorecoin/10; imageview imageviewtens = (imageview)findviewbyid(r.id.drawscore); int tens = tensdigit; string.valueof(tens); int residtens = getresources().getidentifier("ui_" + string.valueof(tens), "drawable", getpackagename()); imageviewtens.setimagedrawable(getresources().getdrawable(residtens)); int onesdigit = (mhighscorecoin % 10); imageview imageviewones = (imageview)findviewbyid(r.id.drawscore2); int ones = onesdigit; string.valueof(ones); int residones = getresources().getidentifier("ui_" + string.valueof(ones), "drawable", getpackagename()); imageviewones.setimagedrawable(getresources().getdrawable(residones)); } else { imageview imageviewones = (imageview)findviewbyid(r.id.drawscore2); int ones = mhighscorecoin; string.valueof(ones); int residones = getresources().getidentifier("ui_" + string.valueof(ones), "drawable", getpackagename()); imageviewones.setimagedrawable(getresources().getdrawable(residones)); }
Comments
Post a Comment