Question

I have created an array of coordinates. The centre of an image should move through those coordinates. I have used TranslateAnimation to achieve it. But during the animation the image is moving within the last two coordinates. Below is my code:

private void CreateAnimationAndRun() {
        // move to the different coordinates one by one
        for(int k=0; k<xpoints.length; k++) {
            if(k==0) {
            x1 = xstart;
                y1 = ystart;
            } else {
            x1 = xpoints[k-1];
            y1 = ypoints[k-1];
            }
            x2 = xpoints[k];
            y2 = ypoints[k];
            TranslateAnimation translateAnimation = new TranslateAnimation(x1, x2, y1, y2);
            translateAnimation.setDuration(1000);
            translateAnimation.setAnimationListener(anim_listener);
            imv1.startAnimation(translateAnimation);
        }
    }

The above function has been called on a button click.

Was it helpful?

Solution

I've only had a few hours to play Android animation myself, so I hope this isn't misleading.

I think the problem is that your loop is overwriting the animation with every iteration, such that when you run all you see is the very last animation (between the last two points).

Instead, the animations need to be generated with different start times. From what I understand so far, the best way to do this is to create an animationSet, and use your loop to load it up with animations that differ in starting time via setStartOffset(). Then on your click you can just start the animationSet. Alternately the click could create them in the loop and use setStartTime() to get them to play at appropriate points.

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