1

I would like to use the Android animation framework to calculate movement over time. However, the TranslateAnimations I create always seem to indicate that they have ended in every call I make to getTransformation, and the Transformation appears incorrect. In the code below, I expect the following Log output after running void1() and void2():

Test 1 initially: 100.0, 1000.0 (true)

Test 1 after 2.5 seconds: 150.0, 1500.0 (true)

Test 2 initially: 100.0, 1000.0 (true)

Test 2 after 2.5 seconds: 150.0, 1500.0 (true)

Instead, I get this output:

Test 1 initially: 12.0, 34.0 (true)

Test 1 after 2.5 seconds: 12.0, 34.0 (true)

Test 2 initially: 0.0, 0.0 (true)

Test 2 after 2.5 seconds: 0.0, 0.0 (true)

void test1() {
        Animation anim = new TranslateAnimation(100, 1000, 200, 2000);
        anim.setFillAfter(true);
        anim.setFillBefore(true);
        anim.setDuration(5000);
        anim.startNow();

        Transformation t = new Transformation();
        boolean hasended = anim.getTransformation(AnimationUtils.currentAnimationTimeMillis(), t);
        float newpts[] = {12, 34};
        t.getMatrix().mapPoints(newpts);
        Log.v("AnimationTest", "Test 1 initially: " + newpts[0] + ", " + newpts[1] + " (" + hasended + ")");

        long target = System.currentTimeMillis() + 2500;
        while (System.currentTimeMillis() < target)
            System.gc();

        hasended = anim.getTransformation(AnimationUtils.currentAnimationTimeMillis(), t);
        t.getMatrix().mapPoints(newpts);
        Log.v("AnimationTest", "Test 1 after 2.5 seconds: " + newpts[0] + ", " + newpts[1] + " (" + hasended + ")");
    }

    void test2() {
        Animation anim = new TranslateAnimation(100, 1000, 200, 2000);
        anim.setFillAfter(true);
        anim.setFillBefore(true);
        anim.setDuration(5000);
        anim.startNow();

        //Based on http://stackoverflow.com/questions/4112599/android-how-to-stop-animation-cancel-does-not-work
        Transformation t = new Transformation();
        boolean hasended = anim.getTransformation(AnimationUtils.currentAnimationTimeMillis(), t);
        Matrix transformationMatrix = t.getMatrix();
        float[] matrixValues = new float[9];
        transformationMatrix.getValues(matrixValues);
        float transX = matrixValues[Matrix.MTRANS_X];
        float transY = matrixValues[Matrix.MTRANS_Y];
        Log.v("AnimationTest", "Test 2 initially: " + transX + ", " + transY + " (" + hasended + ")");

        long target = System.currentTimeMillis() + 2500;
        while (System.currentTimeMillis() < target)
            System.gc();

        hasended = anim.getTransformation(AnimationUtils.currentAnimationTimeMillis(), t);
        transformationMatrix = t.getMatrix();
        transformationMatrix.getValues(matrixValues);
        transX = matrixValues[Matrix.MTRANS_X];
        transY = matrixValues[Matrix.MTRANS_Y];
        Log.v("AnimationTest", "Test 2 after 2.5 seconds: " + transX + ", " + transY + " (" + hasended + ")");
    }

How can I get the results I'm expecting?

Ben
  • 1,272
  • 13
  • 28
  • In test 1, you set 'newpts[] = {12, 34}'. How do you expect it to output it any differently, since you don't change the values before the log statements? – Andreass Apr 11 '11 at 08:17
  • Matrix.mapPoints(newpts) has side effects (writes to newpts). – Ben Apr 12 '11 at 05:34

2 Answers2

1

There are two problems with the code above -- the first is that initialize() must be called before the TranslateAnimation will return valid results for getTransformation(). The second is that getTransformation() returns whether the Animation is still running, not whether it has stopped. After making these two changes, the code works as desired.

Ben
  • 1,272
  • 13
  • 28
0

Couldn't you simply implement AnimationListener ? Within onAnimationEnd() you could re-initialize them or whatever you need to do between testing?

While-E
  • 1,527
  • 2
  • 20
  • 37
  • I could, but I'm not sure how that would help the problem (which is solved in a different answer) -- the problem was that Animation.getTransformation would always indicate that the animation had already ended, even when it should still have been going. The problem was that Animation.initialize must be called before an animation can be used, and I misinterpreted the return value of getTransformation. anim doesn't need to be reinitialized because it is a different object in test1 and test2. – Ben Apr 14 '11 at 04:32
  • @Ben My apologies for the misinterpretation, glad you solved it though. – While-E Apr 14 '11 at 18:13