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\$

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

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.