1

我有一个视图,我需要用加速度进行缩放,我的意思是,当比例为 时MIN_SCALE,速度必须很慢,但当比例接近 时MAX_SALE,速度必须更快。现在我的速度总是一样的。

视图将使用许多帧来进行移动:

numberOfFrames = Math.round((float)ANIMATION_TIME/GameLoopThread.FRAME_PERIOD);
frameCount = 0;

我用该帧数计算 scaleVelocity :

scaleVelocity = (MAX_SCALE-MIN_SCALE)/numberOfFrames;

每次游戏循环迭代,我都会使用以下代码更新视图的比例:

if (frameCount<numberOfFrames) {
    currentScale = currentScale + scaleVelocity;
    frameCount++;
}

当帧数达到numberOfFrames动画必须结束。

如何向此代码添加加速?请记住,加速度必须尊重视图需要MAX_SCALE从 frameCount 变量到达最后一帧。

4

1 回答 1

1

来自 android 文档的屏幕截图

定义你的插值器

INTERPOLATOR = new AccelerateInterpolator();  

在计算 scaleVelocity 时,获取当前插值

float interpolatedValue = INTERPOLATOR.getInterpolation(frameCount / numberOfFrames);

getInterpolation() 返回一个介于 0(动画开始)和 1(动画结束)之间的值

scaleVelocity = (MAX_SCALE-MIN_SCALE)/numberOfFrames * interpolatedValue;  // use min,max func if needed.

加速插值器的数学方程是 f(x) = x²,如果你想要更大的变化,那么创建你的自定义插值器。

动画的工作测试方法。

 private void testAnim() {
    int numberOfFrames = 100;//Math.round((float)ANIMATION_TIME/GameLoopThread.FRAME_PERIOD);
    float frameCount = 0;
    float MAX_SCALE = 4;
    float MIN_SCALE = 0.1f;
    float scaleVelocity;
    float currentScale ;
    Interpolator INTERPOLATOR = new AccelerateInterpolator();

    do {
        float interpolatedValue = INTERPOLATOR.getInterpolation(frameCount / numberOfFrames);
        scaleVelocity = (MAX_SCALE - MIN_SCALE) * interpolatedValue;

        currentScale = Math.max(MIN_SCALE, scaleVelocity);
        ++frameCount;
        Log.d("STACK", "testAnim: currentScale = " + currentScale);
        // apply scale to view.
    } while (frameCount < numberOfFrames);

    // finally set final value of animation.
    currentScale = MAX_SCALE;
    // apply scale to view.

}
于 2019-09-27T19:43:03.250 回答