gpt4 book ai didi

android - 动画自定义对象变量

转载 作者:行者123 更新时间:2023-11-29 20:43:03 24 4
gpt4 key购买 nike

假设我有一个自定义对象,如 Point 类(我没有使用 Android 中的类),当用户双击屏幕时,我需要慢慢地将其坐标从起始值更改为结束值。我已经完成了所有设置,但我无法“激活”此更改。你知道我该怎么做吗?

我已经尝试过类似的方法,但没有任何改变:ObjectAnimator(point, "point.variable", final_value).start()

最佳答案

您可能未正确设置 ObjectAnimator

假设您的 Point 类有一个 Integer 实例变量 xPosition。为了使用 ObjectAnimatorxPosition 设置动画,您可以这样做:

ObjectAnimator.ofInt(new Point(), // Instance of your class
"xPosition", // Name of the property you want to animate
startValue, // The initial value of the property
... intermediateValues ..., // Any other desired values
endValue) // The final value of the property
.setDuration(duration) // Make sure to set a duration for the Animator
.start(); // Make sure to start the animation.

ObjectAnimator 将尝试在每一帧后自动更新属性,但为了使其成功,您的类必须具有适当的属性设置方法,格式为 设置你的属性。因此,在这个特定示例中,您的类必须有一个名为 setXPosition 的方法(注意驼峰式大小写)。

如果出于某种原因,这不起作用,那么您将求助于 ValueAnimator。您可以用类似的方式设置 ValueAnimator

ValueAnimator anim = ValueAnimator.ofInt(startValue // Initial value of the property
endValue) // Final value of the property
.setDuration(duration); // Make sure to set a duration for the Animation.

此处的区别在于您必须手动更新您的属性。为此,我们将 AnimatorUpdateListener 添加到动画中,其 onAnimationUpdate 方法将在动画中的每一帧之后调用。

anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// Manually call the xPosition's setter method.
point.setXPosition(animation.getAnimatedValue());
}
});

别忘了开始动画。

anim.start();

有关 ValueAnimatorObjectAnimator 的更多详细信息,请参阅 Google 关于属性动画的 API 指南: http://developer.android.com/guide/topics/graphics/prop-animation.html#value-animator

关于android - 动画自定义对象变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30745407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com