本文介绍了在Android中如何使用ObjectAnimator沿曲线移动x点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像视图石头",并将其从当前位置移动到X,Y位置.我希望它沿着曲线移动.请让我知道我该怎么做(我已将min api设置为11)

I have an image view "stone" and am moving it from its current position to a X,Y position. I want it to move along a curve. Please let me know how I can do that(i have set the min api as 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的回答对我来说似乎非常有用.

The answer by Budius seems perfectly useful to me.

这是我使用的动画对象:

Here are the animator objects I use:

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

Purpose: Move View "view" along Path "path"

Android v21 +:

Android v21+:

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

Android v11 +:

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,沿路径移动位图吗?

这篇关于在Android中如何使用ObjectAnimator沿曲线移动x点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!