android - 使用 libgdx 创建交互式动画

标签 android animation libgdx interactive gesture-recognition

我正在寻找一种将平移手势与动画完成百分比联系起来的方法。让我告诉你我的意思。

enter image description here

该图像代表我想要执行的动画,即移动图像 Actor 或 Sprite 。动画通过平移手势执行。当用户滑动 200 像素时,动画已 100% 完成并处于第 6 阶段。如果用户仅滑动 100px,则完成 50%,处于第 3 阶段。如果用户不执行平移手势,动画将保持在 0% 并处于第 1 阶段。我正在寻找有关如何开始构建此类模型的提示。我相信这就是所谓的互动。您有什么建议吗?

最佳答案

您可以使用 GestureDetector 来处理平移输入。 gestureListener.pan 方法可以更新动画位置参数。

private int screenPixelsToAnimationPositionRatio = 0.01f; //will need to tweak this and probably adjust it at runtime based on screen dimensions and resolution
private float animationPosition = 0; //from 0 to 1, fraction of animation complete

public void create () {

    //...

    GestureAdapter gestureAdapter = new GestureAdapter {
        @Override
        public boolean pan (float x, float y, float deltaX, float deltaY) {
            animationPosition += deltaX * screenPixelsToAnimationPositionRatio;
            animationPosition = MathUtils.clamp(animationPosition, 0, 1);
            return true;
        }
    };

    GestureDetector gestureDetector = new GestureDetector(gestureAdapter);
    Gdx.input.setInputProcessor(gestureDetector); //or put the detector in an InputMultiplexer with your other input listeners.
}

然后您将创建一个方法,可以根据 animationPosition 的当前值更新对象的位置和旋转。您需要找出确定所需运动的方程。例如,看起来有点像上面所示的内容:

private void updateAnimation (){
    x = animationPosition * 30;
    float y = 0, rotation = 0;
    if (animationPosition >= 0.25f) { 
        float jumpPosition = Math.min(1, (animationPosition - 0.25f) / 0.5f);
        y = 30 * (1 - Interpolation.pow2In.apply(Math.abs(2 * jumpPosition - 1)));
        rotation = 180 * jumpPosition;
    }

    mySprite.setPosition(x, y);
    mySprite.setRotation(rotation);
}

然后在 render 中的某个位置调用此更新方法。

关于android - 使用 libgdx 创建交互式动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34954975/

相关文章:

java - Android/Java 中的反斜杠

Android:动画菜单图标

java.lang.NoClassDefFoundError : org. apache.batik.dom.svg.SVGDOMImplementation

java - 即时速度变化 LibGDX

java - LibGDX HTTP Post 请求在 Node.js 服务器上接收 400 消息

android - 如何在 android 中使用 Volley 在 Json 对象中发布 Json 数组

android - 在后台捕获相机帧 (Android)

java - Android - 重新定义从支持库中删除的类 (AsyncTaskCompat)

javascript - 如何在受 javascript 滚动和高度范围限制的滚动时按比例减小元素高度?

html - Chrome 中的渲染错误