It is necessary to apply the same animation to two widgets with a certain delay, but the setStartOffSet (delay) method used by me does not give the desired result. What's wrong?

res/anim: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="750" android:toYDelta="0" android:duration="3000"> </translate> </set> java: public class MainActivity extends AppCompatActivity { ImageView mImage1; ImageView mImage2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mImage1 = (ImageView) findViewById(R.id.image_1); mImage2 = (ImageView) findViewById(R.id.image_2); Animation animation = AnimationUtils.loadAnimation(this, R.anim.trans_animation); mImage1.startAnimation(animation); animation.setStartOffset(1000); // НЕ РАБОТАЕТ ЗАДЕРЖКА?! mImage2.startAnimation(animation); } } 

    2 answers 2

    Try to create separate animation objects and set different parameters for them:

      Animation animation = AnimationUtils.loadAnimation(this, R.anim.trans_animation); mImage1.startAnimation(animation); Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.trans_animation); animation1.setStartOffset(1000); mImage2.startAnimation(animation1); 
    • fire thank you - cosmic_M

    Use Thread.sleep(); instead of animation.setStartOffset(1000); It should work. So:

     mImage1.startAnimation(animation); Thread.sleep(1000); mImage2.startAnimation(animation); 
    • Unfortunately did not work. All the time allotted to the "dream", for some reason, the application uses to animation. - cosmic_M
    • Strange! Have you tried using android:startOffset in the XML itself? - DevOma
    • android: startOffset = "1000" - DevOma
    • then I have to create an .xml file for each View in the anim folder (I'm not lazy, it's just if there is a setStartOffset () method ... and also in my project not 2, but 8 widgets ...) - cosmic_M
    • Putting the main stream to sleep is a bad idea. If you need to put to sleep at 5 + s, the application will issue ANR. Moreover, by lulling the main stream all its operations will be frozen, incl. drawing the interface. Those. Immediately after trying to start the first animation, everything will stop, and then the first animation will continue to be drawn and the second will start immediately. Those. in fact we will see it, then it sees the vehicle - a delay for a second and simultaneous triggering of both animations. - Yuriy SPb