Use same animation at different times, in different elements

484 views Asked by At

I have several TextView I want to animate. I want to use the same animation but that start at different times for each TextView. I searched but could not find how. I tried setStartOffset, but it seems that I'm not using as directed. Someone could help me? This is my code:

    TranslateAnimation animation = new TranslateAnimation(
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
    Animation.ABSOLUTE, -1500.0f, Animation.ABSOLUTE, 0.0f);
    animation.setDuration(3000);
    tvNumero1.startAnimation(animation);

    //this fails:
    animation.setStartOffset(300);
    tvNumero2.startAnimation(animation);
1

There are 1 answers

1
Toguard On BEST ANSWER

I created different animations for the element, alternatively, you could use an animaiton from an xml resource. Here's the code:

//First Animation
TranslateAnimation animation = new TranslateAnimation(
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
    Animation.ABSOLUTE, -1500.0f, Animation.ABSOLUTE, 0.0f);
animation.setDuration(3000);
tvNumero1.startAnimation(animation);

//Second Animation
TranslateAnimation animation2 = new TranslateAnimation(
    Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, 0.0f,
    Animation.ABSOLUTE, -1500.0f, Animation.ABSOLUTE, 0.0f);
animation2.setDuration(3000);
animation2.setStartOffset(300);
tvNumero2.startAnimation(animation2);

Alternatively, you can define the animation in an XML file:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromYDelta="-1500"
    android:toYDelta="0" >

</translate>

Here is the code for XML:

Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.text_move);
tvNumero1.startAnimation(animation);

Animation animation2 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.text_move);
animation2.setStartOffset(300);
tvNumero2.startAnimation(animation2);

Previous code it seems it's waiting for the offset and then starts the whole animation, I changed it to 3 seconds, and it takes those 3 seconds to start.