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
\$\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\$ – Vaillancourt Dec 9 '16 at 16:28
  • \$\begingroup\$ But this one is helpful and is self explanatory \$\endgroup\$ – lxknvlk Dec 9 '16 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.