1
\$\begingroup\$

I am new to animations in android. I was wondering, is there a way to keep a button moving around the screen, bouncing off the walls, until it gets clicked? I was thinking to do something like this:

    while(!clicked){
    //Run code to move button
        }
  button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
    clicked = true;
        }
    );

And to make it random, do something like this:

Random r = new Random();
btnCount.animate().xBy(r.nextInt(10)+1).yBy(r.nextInt(10)+1);

But that doesn't work, and nothing happens. What is the best way to do this animation?

Thanks,

Ruchir

\$\endgroup\$

1 Answer 1

-1
\$\begingroup\$

Try this

        TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 500);
        anim.setDuration(300l);
        anim.setFillAfter(true);
        button.startAnimation(anim);
\$\endgroup\$
2
  • \$\begingroup\$ Please add comments or describe what the code is doing and why you're doing it. Blocks of code are generally not that helpful without explanation. \$\endgroup\$ Commented Dec 9, 2016 at 16:28
  • \$\begingroup\$ But this one is helpful and is self explanatory \$\endgroup\$ Commented Dec 9, 2016 at 16:29

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.