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?