0

I am looking for some advice what is the best way animate 4 images in the same time. I want to do fade_in/fade_out animation. I think I will need 4 threads for every animation separately. I read something about AsyncTask but I don't think it is good solution for me. Can anyone give me advice how can I achieve it?

Matwosk
  • 459
  • 1
  • 9
  • 25
  • Have you tried to get a reference to the views and call.animate? – weston Apr 17 '14 at 21:37
  • 1
    If your apply all animations one by one in your code, a kind of `image1.startAnimation(..); image2.startAnimation(...); image3.startAnimation(...)` they all will be animated simultaneously unless you set different delay times for them. – Alex Salauyou Apr 17 '14 at 22:06
  • 1
    Animations in Android are async by their nature, they all are executed in UI thread, performing step-by-step transformations of a view on UI redraw loops. After calling `view.startAnimation()` app doesn't wait until animation ends--it moves further immediately. – Alex Salauyou Apr 17 '14 at 22:23
  • @Salauyou Actually I want to set some delay between start different animations. Is there some way how I can achieve it? – Matwosk Apr 17 '14 at 22:32
  • 1
    @Matwosk use `setStartOffset()` for `Animation` objects http://developer.android.com/reference/android/view/animation/Animation.html#setStartOffset%28long%29 – Alex Salauyou Apr 17 '14 at 22:36
  • 1
    It stratring working. Thanks a lot! U can send your comments like answer I will accept it like answer for this question. – Matwosk Apr 17 '14 at 22:54
  • @Matwosk never mind :) – Alex Salauyou Apr 17 '14 at 22:59
  • @Salauyou I have last problem. My animation is infinite. I want to every animation start after 500ms. If I use setStartOffset - it is used in every repeat so finally time between animations is longer and longer in every repeat. If I use setStartTime() - no matter what value I set, it starts in the same time as other animations. What I want to achieve is. Animation1 starts 1ms. Animation2 starts 500ms. Animation3 starts 1000ms etc... and duration for every animation is 1000ms. – Matwosk Apr 17 '14 at 23:44
  • it happens because you use one mutable animation object for all views, so when you change it, all further animations are affected. Create another instances of initial animation object using `.clone()` method and set appropriate offset in each. Or use a `Timer` to start animations in different time points. – Alex Salauyou Apr 18 '14 at 02:46

1 Answers1

1

What you want to use is an AnimatorSet or its Builder: http://developer.android.com/reference/android/animation/AnimatorSet.html http://developer.android.com/reference/android/animation/AnimatorSet.Builder.html

You can add several ObjectAnimators to the set and then animate them at the same time. https://stackoverflow.com/a/13506513/379245

Community
  • 1
  • 1
BVB
  • 5,380
  • 8
  • 41
  • 62
  • Thanks, this is probably what I was looking for. – Matwosk Apr 17 '14 at 21:51
  • `AnimatorSet` object has different purpose--it allows to combine _animations_ (like scale, rotation, movement, other sets) into one set, not _views_. – Alex Salauyou Apr 17 '14 at 22:10
  • just call `startAnimation()` one by one for all 4 images. See explanation in comments below your question. – Alex Salauyou Apr 17 '14 at 22:30
  • You can use `ObjectAnimator`s along with `AnimatorSet` in order to get the set to animate individual views. – BVB Apr 17 '14 at 22:42
  • I should mention that @Salauyou 's method would work as well. The reason I prefer this one is it is a bit awkward to read several animations starting "one after the other" but actually firing "at the same time". – BVB Apr 17 '14 at 22:47
  • @BVB btw, method you mentioned still needs to attach separate animation to each view – Alex Salauyou Apr 17 '14 at 22:56
  • I guess I must be confused about what exactly the question is asking for. I assumed that there were individual views that needed to be animated at the same time. Would it not be possible to attach the same animation to each view? But yes, a separate ObjectAnimator would be required for each view. In the example I linked the animations are different because one animation animates a "x" property, while the other animates a "y" property. – BVB Apr 17 '14 at 22:57
  • @BVB `AnimatorSet` and `ObjectAnimator` are just more recent and more flexible substitute for `Animation` class... but... okay – Alex Salauyou Apr 17 '14 at 23:05