0

I am currently doing a beginners android studio/java course. I have made a Dice rolling app and a Magic Eight Ball app that both use an RNG and arrays to overlay new images on a button press.

After showing to friends, they have said it would be good to have some kind of animation, now I am not going to model and animate 3D dice etc, simply because I can't.

I did have the idea that pressing the button that fires the RNG could be used to fire it a set amount of times, lets say 10 as an example, and the resulting cycling through dice numbers/8ball images would give a pseudo animated appearance.

My question is, how do I make a single button press, fire the RNG/Array more than once?

I have tried to search an answer for this on here and google, but I do not think I am asking the right questions, as all the answers I can find are on how to stop multiple actions taking place, rather than trying to force them automatically

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button AskButton;
        AskButton = findViewById(R.id.AskButton);

        final ImageView Ball;
        Ball = findViewById(R.id.Ball);

        final int[] ballArray = {R.drawable.ball1,
                           R.drawable.ball2,
                           R.drawable.ball3,
                           R.drawable.ball4,
                           R.drawable.ball5};

        AskButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Magic Eight Ball", "Button Press");

                Random RNG = new Random();

                int number = RNG.nextInt(5);

                Log.d("Magic Eight Ball", "Number is:" + number );

                Ball.setImageResource(ballArray [number]);

            }
        });
    }
}

Currently, one button press changes the image once, I want the one button press to change the image 10 times in quick succession before stopping.

Ashish
  • 6,791
  • 3
  • 26
  • 48
  • 1
    You essentially want to call same method few times with a delay. Look for similar questions, for example: https://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android – Pawel Sep 22 '19 at 12:55

1 Answers1

0

Try something like this:

    private fun animateNextFrame(delay: Long) {
        ball.postDelayed({
            ball.setImageResource(ballArray[rng.nextInt(5)]);
            if (delay < 1000) {
                animateNextFrame(delay * 2)
            }
        }, delay)
    }

Then in your onClick() just invoke the runnable callback (immediately) for the first time, and provide an initial delay.

Manipulate the values (initial delay, threshold and multiplier) to make your animation feel more natural (for example - my approach rotates values quicker at the beginning, and then slower at the end).

Remember to clear your callbacks (ball.removeCallbacks(null))when your activity gets stopped / destroyed.

Mateusz Herych
  • 1,595
  • 12
  • 20
  • Heh, I got so used to Kotlin being a standard nowadays that I didn't realize until now that your project is Java. Apologies for this, and I hope it's still helpful. :) – Mateusz Herych Sep 22 '19 at 13:15
  • Thanks for the answer, I will have a play around and see if I can get something similar to this working with Java. Thanks! – Carl Flegg Sep 22 '19 at 13:34
  • That will be a productive excercise after all. :) API is the same, so all you have to do is convert it to the Java syntax. Btw, there was a naming mistake in my answer, I updated it. – Mateusz Herych Sep 22 '19 at 13:40