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

Apa*_*rna 3 android

我有一个图像视图"石头",并将其从当前位置移动到X,Y位置.我希望它沿着曲线移动.请告诉我如何做到这一点(我已将min 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();
Run Code Online (Sandbox Code Playgroud)

Dil*_*P G 6

Budius的答案对我来说似乎非常有用.

这是我使用的动画师对象:

目的:沿路径"路径"移动视图"视图"

Android v21 +:

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

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]);
    }
});
Run Code Online (Sandbox Code Playgroud)

类似于:Android,沿路径移动位图?


A H*_*ard 3

使用插值器。例如为 x 和 y 设置 2 个不同的插值器:

moveX.setInterpolator(new AccelerateDecelerateInterpolator());

moveY.setInterpolator(new DecelerateInterpolator());
Run Code Online (Sandbox Code Playgroud)

还有更多(LinearInterpolator、AccelerateInterpolator...),但我认为这应该是您想要的组合。