android - 在 Android 中如何使用 ObjectAnimator 沿曲线移动到点 x

标签 android

我有一个 ImageView “石头”,正在将它从当前位置移动到 X、Y 位置。我想让它沿着曲线移动。请让我知道我该怎么做(我已将最小 api 设置为 11)

ObjectAnimator moveX = ObjectAnimator.ofFloat(stone, "x", catPos[0] );
ObjectAnimator moveY = ObjectAnimator.ofFloat(stone, "y", catPos[1] );
AnimatorSet as = new AnimatorSet();
as.playTogether(moveX, moveY);
as.start();

最佳答案

Budius 的回答对我来说似乎非常有用。

这是我使用的动画对象:

用途:沿路径“路径”移动 View “ View ”

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, move bitmap along a path?

关于android - 在 Android 中如何使用 ObjectAnimator 沿曲线移动到点 x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28901744/

相关文章:

java - 如何将base64字符串图像解码为字节数组并将其作为blob类型存储在数据库中?

android - 如何通过在 android 的 Fragment 中调用 finish() 来关闭窗口?

android - 使用异步任务从内容提供者加载图像位图

android - Xamarin 用户界面 : Programmatically VS XAML

android - 为什么我在 Android 上使用 Guice 2.0 时会收到 ClassNotFoundException?

java - 如何在不破坏和重新创建 fragment 的情况下在 fragment 之间切换? (以类似静态的方式)

android - 无需PayPal账户或信用卡的PayPal Android SDK支付

android - 错误 : cannot find symbol variable abc_ic_ab_back_mtrl_am_alpha

android - 从当前位置到已知位置的谷歌方向路线

android - WebView 显示某些链接的空白 View