Question

I have an application where i spawn a new enemy every 4 seconds. the enemy goes from right to left using anTranslateAnimation. This animation takes 7 seconds total.

The problem that I'm facing now is that when the animation is running, every time that i spawn a new enemy, the animation stops for the old one and starts animating the new enemy.

Is there any way to animate two different objects using the same TranslateAnimation?

Just in case, here is my animation

        translate= new TranslateAnimation(
            Animation.ABSOLUTE, (float) 1.0,
            Animation.ABSOLUTE, (float) -4.0,
            Animation.ABSOLUTE,0,
            Animation.ABSOLUTE,0);

    translate.setDuration(10000);
    translate.setFillAfter(true);
    //newIV is the enemy's ImageView
    newIV.startAnimation(translate);
Was it helpful?

Solution

You can apply animation like this

make an animation xml in anim folder

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
     <translate
        android:fromXDelta="100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="500" 
        />
   </set>

Then code in class

final Animation RightToLeft = AnimationUtils.loadAnimation(context,
                R.anim.right_to_left);


        ((ImageView)findViewById(R.id.yourImage))
                .startAnimation(RightToLeft);

Hope it will help.

OTHER TIPS

You can set a same animation to Different views in same position.try to use animationLisener

    translate.setAnimationListener(new TranslateAnimation.AnimationListener() {                             @Override
        public void onAnimationStart(Animation animation) { }
            @Override
        public void onAnimationRepeat(Animation animation) { }
        @Override
        public void onAnimationEnd(Animation animation) 
        {
         animation.setFillAfter(false);
         RelativeLayout.LayoutParams params =(RelativeLayout.LayoutParams)mLogo.getLayoutParams();
         //Set your firest view layout or hide the previous newIV
        //This ll avoid filckering
         animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
         animation.setDuration(1);
         ImageView mLogo = new ImageView(activity);// Second enemy as u said     

         animation = new TranslateAnimation(
        Animation.ABSOLUTE, (float) 1.0,
        Animation.ABSOLUTE, (float) -4.0,
        Animation.ABSOLUTE,0,
        Animation.ABSOLUTE,0);      
         mLogo.startAnimation(animation);mLogo.setLayoutParams(params);
         mLogo.clearAnimation();
         mLogo.startAnimation(animation);
            }
});

This ll animate When first enemy going to end create a new instance of animation and use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top