JayRod
JayRod

Reputation: 157

How to set up a looping background in android

What I want to do is simple. I want to have a looping background, so that it looks like the ground is moving. Here is how I am trying to do it however, it looks like both images are being dispatched at same time.

 public void moveGround(){
        startingXPosition =1500.0f;
        endingXPosition = -1500.0f;
        startingYPosition =0.0f;
        endingYPosition =0.0f;
        ImageView ground = (ImageView)findViewById(R.id.ground);
        TranslateAnimation groundAnimation = new TranslateAnimation(startingXPosition,endingXPosition,startingYPosition,endingYPosition);
        groundAnimation.setDuration(1500);
        groundAnimation.setRepeatCount(Animation.INFINITE);
        ground.startAnimation(groundAnimation);

        if(ground.getLeft()<=0) {
            float startingXPosition2 = 1500.0f;
            float endingXPosition2 = -1500.0f;
            float startingYPosition2 = 0.0f;
            float endingYPosition2 = 0.0f;
            ImageView ground2 = (ImageView) findViewById(R.id.ground2);
            TranslateAnimation groundAnimation2 = new TranslateAnimation(startingXPosition2, endingXPosition2, startingYPosition2, endingYPosition2);
            groundAnimation2.setDuration(1500);
            groundAnimation2.setRepeatCount(Animation.INFINITE);
            ground2.startAnimation(groundAnimation2);
        }

    }

Upvotes: 2

Views: 105

Answers (1)

Rustam
Rustam

Reputation: 6515

try

yourAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // do what you want
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
              // do what you want
            }
        });

Upvotes: 3

Related Questions