1
\$\begingroup\$

I have a game in which the player has to do something within a defined time.

E.g.: the player has 10 seconds to complete a level.

My current code animates an imageview (it's only a horizontal line) when the game starts:

progressAnimation = new TranslateAnimation(0, -Settings.screenWidth, 0, 0);
progressAnimation.setDuration(level * 10000);
progressAnimation.setFillAfter(true);
progressAnimation.setInterpolator(new LinearInterpolator());

progressView = new ProgressView(context);
progressView.startAnimation(progressAnimation);

Simultaneously I start a handler:

handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        fireResultActivity(-1); // -1 means, time's up.
    }
}, level * 10000);

If the user can finish the game within 10 seconds an activity comes up and congratulates, if they can't another, "fail" activity comes up.

Is it a good practice to use animation to indicate the progress and start a handler simultaneously?

Could you recommend other (better) solution?

\$\endgroup\$
2
  • \$\begingroup\$ How can I have people answer this question? \$\endgroup\$ Commented Mar 18, 2016 at 20:09
  • 2
    \$\begingroup\$ You've got 1 answer already, but check codereview.stackexchange.com/help/no-one-answers \$\endgroup\$ Commented Mar 22, 2016 at 9:19

1 Answer 1

2
\$\begingroup\$

First of All you should be using setAnimationListener on animation object. so no need of Handler

progressAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
       fireResultActivity(-1); // call your method here
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

As it has built-in listener for Animation, there is no need to create new thread using handler

\$\endgroup\$
4
  • \$\begingroup\$ Hi there, welcome to Code Review! Could you explain in your answer why setAnimationListener is better than using a handler? \$\endgroup\$ Commented Mar 22, 2016 at 8:17
  • \$\begingroup\$ As its has in-build listener for Animation no need to create new thread using handler \$\endgroup\$ Commented Mar 22, 2016 at 9:16
  • \$\begingroup\$ if you would write your explanation in your answer then we can delete the comments \$\endgroup\$ Commented Mar 22, 2016 at 9:17
  • \$\begingroup\$ @Pimgd update answer \$\endgroup\$ Commented Mar 22, 2016 at 9:41

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.