Search code examples
androidandroid-layoutandroid-activityandroid-animationtranslate-animation

Want image to be shown on center for few seconds while going from (YDelta) to (toYDelta) in Translate Animation


I am creating Login page with animation like FB login . And i want my Logo image to be shown on center for few seconds while animated from Ydelta to Ydelta. Code :

 animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);

        animation.setAnimationListener(new Animation.AnimationListener() {


            @Override
            public void onAnimationStart(Animation arg0) {
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
            }

            @Override
            public void onAnimationEnd(Animation arg0) {



                        loginBox.setVisibility(View.VISIBLE);
                        Animation animFade = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade);
                        loginBox.startAnimation(animFade);

            }
        });

        ImageView imgLogo = (ImageView) findViewById(R.id.imageView );
        imgLogo.startAnimation(animation);

    }

translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fillAfter="true">
    <translate
        android:fromYDelta="30%p"
        android:toYDelta="0%p"
        android:duration="1000" />
</set>

Any type of Help , Appreciated !! Thanks


Solution

  • You could use two animations:

    translate1.xml:

    <translate
        android:fromYDelta="0%"
        android:toYDelta="50%"
        android:duration="1000"
         />
    

    translate2.xml:

    <translate
        android:fromYDelta="50%"
        android:toYDelta="100%"
        android:duration="1000"
        android:startOffset="2000" />
    

    With android:startOffset to delay the animation.


     moveToCenterAnim= AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate1);
    
     moveToCenter.setAnimationListener(new Animation.AnimationListener() {
    
    
            @Override
            public void onAnimationStart(Animation arg0) {
            }
    
            @Override
            public void onAnimationRepeat(Animation arg0) {
            }
    
            @Override
            public void onAnimationEnd(Animation arg0) {
    
                ...
    
                ImageView imgLogo = (ImageView) findViewById(R.id.imageView );       
                Animation moveOnAnim= AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate2);
                imgLogo.startAnimation(moveOnAnim);
    
            }
        });
    
        ImageView imgLogo = (ImageView) findViewById(R.id.imageView );
        imgLogo.startAnimation(moveToCenterAnim);
    
    }