Search code examples
androidtranslate-animation

Can we use Android TranslateAnimation for 2 different location?


TranslateAnimation is use when you want to move image in different possintion like

  1. left to right
  2. up to down
  3. one XYscalse position to other XYscale position

Syntax

TranslateAnimation animation = new TranslateAnimation(StartinXscale,StartingYscale,EndXscale,EndYscale);

Methods

animation.setDuration(millisecond);//move speed.
animation.setRepeatCount(int value);//how many time you want to move it from starting to ending position.
animation.setRepeatMode(int value);//mode like goto destination and return back to main position. 
imageView.startAnimation(animation);//Start animation on imageView

Can we use above code twice time in one activity?


MyCode

private void animationAction() {
        float StartX = 500.0f;
        float StartY = -300.0f;
        float EndX = -300.0f;
        float EndY = 500.0f;
        int i = 0;
        for (i = 0; i <2; i++) {
                TranslateAnimation animation = new TranslateAnimation(StartX,StartX + EndX, StartY, StartY + EndY);
                animation.setDuration(3000);
                animation.setRepeatCount(5);
                animation.setRepeatMode(2);
                animation.setFillAfter(true);
                img_animation.startAnimation(animation);
                EndX = 300.0f;

        }

In MyCode result was only execute one time



Solution

  • // Animation
        private Animation animMove;
        private Animation animFadein;
    
    //Method to preform 2 animation in sequence  
    
    private void doAnimation() {
            // load the animation
            animMove = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
            animFadein = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
            // start move up animation on brnetwork text
            imageView.startAnimation(animMove);
            // set animation listener
            animMove.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
    
                    Resources res = getResources();
                    Drawable drawable = res.getDrawable(R.drawable.background);
                    relMain.setBackgroundDrawable(drawable);
    
                    llInput.startAnimation(animFadein);
                    llInput.setVisibility(View.VISIBLE);
                    tvSignUp.startAnimation(animFadein);
                    tvVideo.startAnimation(animFadein);
                    tvSignUp.setVisibility(View.VISIBLE);
                    tvVideo.setVisibility(View.VISIBLE);
                }
            });
    
            // set animation listener
            animFadein.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
                }
            });
    
        }
    
    after MOVE animation i have to perform FEDIN animation
    in your case you can replace FEDIN by your Next Animation.