Displaying dynamic images based on an Int value (for showing player's score on main menu)

36 views
Skip to first unread message

JF

unread,
Jun 1, 2014, 10:59:15 AM6/1/14
to replica-island-...@googlegroups.com
I just posted this question on StackOverflow, but it may help here as well. If i get an answer at SO, i'll post it here for reference.

Disclaimer: I’m extremely new to Java/Xml, so please bear with me. I’m using Replica Island source so if there’s an easier/newer way of doing this, please let me know.

Objective: Display a player’s score on the main menu screen using .PNG numbers instead of text. Why? Because my .PNG numbers are much more flashy/stylized than standard text.

Currently: I’m capturing and displaying the player’s score via an int variable that’s saved to and loaded from in SharedPreferences. I’m then displaying it TEXT via the 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, but I don’t want basic text on my main menu. During actual gameplay, I’m using the following method to convert my int score, into an array that calls my .PNG numbers (0-9) to 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;

do {

                  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 I display the high score in my XML/MainmenuActivity class, using the .PNG version of the numbers instead of the text I’m currently using?

The start of my 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: This question may be unanswerable with the information provided. Again, I’m extremely new to Java and in over my head. For example, do I need to be pulling a ton more stuff from “HudSystem” to finish creating the array of images?

Any details or general thoughts would be much appreciated.

Thanks!

JF

unread,
Jun 1, 2014, 4:25:24 PM6/1/14
to replica-island-...@googlegroups.com
Wwwhhhhelp, after hours and hours of reading (thanks Stack Overflow!) and trial and error, I've answered my own question. 

Is it the cleanest, most effective approach?  HIGHLY unlikely (again, I have almost no idea wtf i'm doing, lol). Regardless, it works, :).

**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 the players mHighScoreCoin from SharedPreferences (onCreate AND onResume), I run the 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));
        }

Bart Hirst

unread,
Jun 2, 2014, 8:29:51 AM6/2/14
to ReplicaIsland Coding Community
Looks good to me - DIY is always the best way :)

        }

--
You received this message because you are subscribed to the Google Groups "ReplicaIsland Coding Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to replica-island-coding...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages