0

I've got an animation to perform which consists of some arrow heads aligned horizontally where the alpha values of the arrows will change to achieve an animation effect (i.e. first arrow has alpha 1.0, then the second will get a value 1.0 etc.).

So if I have a function like this:

void highlightFirstArrow()
{
    mArrow1.setAlpha(1.0f);
    mArrow2.setAlpha(0.75f);
    mArrow3.setAlpha(0.50f);
    mArrow4.setAlpha(0.20f);
}

Then I'd want to start, repeat numerous times, then stop a function such as this:

void animateArrows()
    {
    highlightFirstArray();
    pause;
    highlightSecondArray();
    pause;
    etc.
    }

Obviously this would lock up the GUI thread if it were performed in a for look for example. What are the options for achieving the desired animiation:

- run a for loop in a separate thread 
- don't use a loop, instead constantly execute the functions individually via a timer
- use built in specific android animation mechanisms. If so which is most appropriate? Would AnimatorSet() be good for this scenario, or something else
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378
  • When you want to do something repeatedly on the Android UI thread, the correct solution is almost always [`Handler#postDelayed(...)`](http://developer.android.com/reference/android/os/Handler.html#postDelayed%28java.lang.Runnable,%20long%29). You should be able to find lots of posts about it. – Kevin Krumwiede Jul 21 '15 at 21:28
  • Consider using `ImageView.setAlpha(int)` for better performance, if your arrows are ImageViews. – BladeCoder Jul 21 '15 at 23:37
  • setAlpha(int) is now deprecated – Gruntcakes Jul 23 '15 at 15:58

1 Answers1

0

You definitely shouldn't use any loops or timers. There're lots of built in classes which could help to animate your views. For instance you can use ValueAnimator:

ValueAnimator.ofFloat(1f, 0.2f).setDuration(1000).addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override public void onAnimationUpdate(ValueAnimator animation) {
        float value = (float) animation.getAnimatedValue();
        arrow1.setAlpha(value);
        arrow2.setAlpha(value);
    }
});
eleven
  • 6,779
  • 2
  • 32
  • 52