问题 如何为Android准备曲线翻译动画?


android中有4种类型的动画 - 旋转,alpha,缩放和翻译。 我想准备弯曲的翻译动画。

可能吗。?


4925
2017-12-02 09:48


起源



答案:


您使用的是什么Android版本?从API级别11开始,您可以使用自定义 动画师 这可以轻松实现您的曲线转换。

如果您使用下面的版本,那么只有使用翻译动画和设置动画侦听器手动连接多个线性翻译的可能性

编辑:

例:

View view;
animator = ValueAnimator.ofFloat(0, 1); // values from 0 to 1
animator.setDuration(5000); // 5 seconds duration from 0 to 1
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float value = ((Float) (animation.getAnimatedValue()))
                    .floatValue();
        // Set translation of your view here. Position can be calculated
        // out of value. This code should move the view in a half circle.
        view.setTranslationX((float)(200.0 * Math.sin(value*Math.PI)));
        view.setTranslationY((float)(200.0 * Math.cos(value*Math.PI)));
    }
});

我希望它有效。只需从我的某个应用中复制并粘贴(并缩短和更改)代码。


9
2017-12-02 11:21



请提供一些在android 3.0中准备曲线转换的示例代码。 - user884126
你有。我希望它有效 - js-
@ js-先生,我可以用9岁以上的android库实现一个低于11的api级别的弯曲动画吗? - Rat-a-tat-a-tat Ratatouille


答案:


您使用的是什么Android版本?从API级别11开始,您可以使用自定义 动画师 这可以轻松实现您的曲线转换。

如果您使用下面的版本,那么只有使用翻译动画和设置动画侦听器手动连接多个线性翻译的可能性

编辑:

例:

View view;
animator = ValueAnimator.ofFloat(0, 1); // values from 0 to 1
animator.setDuration(5000); // 5 seconds duration from 0 to 1
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float value = ((Float) (animation.getAnimatedValue()))
                    .floatValue();
        // Set translation of your view here. Position can be calculated
        // out of value. This code should move the view in a half circle.
        view.setTranslationX((float)(200.0 * Math.sin(value*Math.PI)));
        view.setTranslationY((float)(200.0 * Math.cos(value*Math.PI)));
    }
});

我希望它有效。只需从我的某个应用中复制并粘贴(并缩短和更改)代码。


9
2017-12-02 11:21



请提供一些在android 3.0中准备曲线转换的示例代码。 - user884126
你有。我希望它有效 - js-
@ js-先生,我可以用9岁以上的android库实现一个低于11的api级别的弯曲动画吗? - Rat-a-tat-a-tat Ratatouille


以下是我使用的动画师:

目的:沿路径“路径”移动视图“视图”

Android v21 +:

// Animates view changing x, y along path co-ordinates
ValueAnimator pathAnimator = ObjectAnimator.ofFloat(view, "x", "y", path)

Android v11 +:

// Animates a float value from 0 to 1 
ValueAnimator pathAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);

// This listener onAnimationUpdate will be called during every step in the animation
// Gets called every millisecond in my observation  
pathAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

float[] point = new float[2];

@Override
    public void onAnimationUpdate(ValueAnimator animation) {
        // Gets the animated float fraction
        float val = animation.getAnimatedFraction();

        // Gets the point at the fractional path length  
        PathMeasure pathMeasure = new PathMeasure(path, true);
        pathMeasure.getPosTan(pathMeasure.getLength() * val, point, null);

        // Sets view location to the above point
        view.setX(point[0]);
        view.setY(point[1]);
    }
});

如同: Android,沿路径移动位图?


3
2018-05-15 08:29



花更多的时间来解释你的答案如何运作是有帮助的,因此提问者可以更容易地遵循你的代码。 - SuperBiasedMan
@SuperBiasedMan感谢您的反馈!在评论中添加了解释。 - Dileep P G
我必须做: pathMeasure.getLength()/2 因为视图一直回到原来的位置。 - Rick
视图可能会继续回到原始位置,因为您将true传递给PathMeasure(path,forceClose)。这告诉它关闭路径,从最后一个点到原始位置添加一条线。 - definera


请考虑以下Web链接。它是C中的游戏。您需要隔离projectile()函数并尝试理解其中定义的变量。一旦你尝试在自己的代码中实现它。

http://www.daniweb.com/software-development/c/code/216266


-1
2018-02-10 06:17



虽然这在理论上可以回答这个问题,但最好是(meta.stackexchange.com/q/8259)您可以编辑答案以包含解决方案的基本部分,并提供链接以供参考。 - Peter O.