如何将动画与我自己的对象一起使用?

时间:2011-04-11 02:35:54

标签: android animation

我想使用Android动画框架来计算一段时间内的移动。但是,我创建的TranslateAnimations似乎总是表明它们在我为getTransformation进行的每次调用中都已结束,并且转换显示不正确。在下面的代码中,我希望在运行void1()和void2()之后输出以下Log:

  

最初测试1:100.0,1000.0(真)

     

2.5秒后测试1:150.0,1500.0(真)

     

最初测试2:100.0,1000.0(真)

     

2.5秒后测试2:150.0,1500.0(真)

相反,我得到了这个输出:

  

最初测试1:12.0,34.0(真)

     

2.5秒后测试1:12.0,34.0(真)

     

最初测试2:0.0,0.0(真)

     

2.5秒后测试2:0.0,0.0(真)

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 + ")");
    }

我如何得到我期待的结果?

2 个答案:

答案 0 :(得分:1)

上面的代码有两个问题 - 第一个是必须在TranslateAnimation返回getTransformation()的有效结果之前调用initialize()。第二个是getTransformation()返回动画是否仍在运行,而不是它是否已经停止。完成这两项更改后,代码将按预期工作。

答案 1 :(得分:0)

你不能简单implement AnimationListener吗?在onAnimationEnd()范围内,您可以重新初始化它们或测试之间需要做什么吗?