动画自定义对象变量
Animate custom object variable
假设我有一个自定义对象,比如一个点 class(我没有使用 Android 中的那个),我需要慢慢地将它的坐标从起始值更改为用户双击屏幕时的最终值。我已经完成了所有设置,但无法进行 "animate" 此项更改。你知道我该怎么做吗?
我已经尝试过类似的方法,但没有任何改变:
ObjectAnimator(point, "point.variable", final_value).start()
您可能 ObjectAnimator
设置不正确。
让我们假设您的 Point
class 有一个整数实例变量 xPosition
。为了使用 ObjectAnimator
为 xPosition
设置动画,您可以这样做:
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
会尝试在每一帧后自动更新 属性,但为了成功,您的 class 必须有合适的 setter 属性 格式 setYourProperty
的方法。因此,在这个特定示例中,您的 class 必须有一个名为 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();
有关 ValueAnimator
和 ObjectAnimator
的更多详细信息,请参阅 Google 的 API 属性 动画指南:
http://developer.android.com/guide/topics/graphics/prop-animation.html#value-animator
假设我有一个自定义对象,比如一个点 class(我没有使用 Android 中的那个),我需要慢慢地将它的坐标从起始值更改为用户双击屏幕时的最终值。我已经完成了所有设置,但无法进行 "animate" 此项更改。你知道我该怎么做吗?
我已经尝试过类似的方法,但没有任何改变: ObjectAnimator(point, "point.variable", final_value).start()
您可能 ObjectAnimator
设置不正确。
让我们假设您的 Point
class 有一个整数实例变量 xPosition
。为了使用 ObjectAnimator
为 xPosition
设置动画,您可以这样做:
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
会尝试在每一帧后自动更新 属性,但为了成功,您的 class 必须有合适的 setter 属性 格式 setYourProperty
的方法。因此,在这个特定示例中,您的 class 必须有一个名为 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();
有关 ValueAnimator
和 ObjectAnimator
的更多详细信息,请参阅 Google 的 API 属性 动画指南:
http://developer.android.com/guide/topics/graphics/prop-animation.html#value-animator