如何实现整个翻译动画均匀的速度? [英] How to achieve uniform velocity throughout a Translate Animation?
问题描述
我已经申请到一个无限的翻译动画的ImageView
:
I have an infinite translate animation applied to an ImageView
:
Animation animation = new TranslateAnimation(0, 0, -500, 500);
animation.setDuration(4000);
animation.setFillAfter(false);
myimage.startAnimation(animation);
animation.setRepeatCount(Animation.INFINITE);
我所注意到的是,在翻译过程是当图像是接近的起始和结束点时相比,其接近一半的距离(中间点)更慢。
What I have noticed is that the translation process is slower when the image is near the starting and ending point compared to when its near half the distance (middle point).
我想在Android上的不统一翻译动画的速度。
I guess the velocity for translate animations on android is not uniform.
如何让我的速度均匀的整个过程?
How do I make the velocity uniform throughout the process ?
推荐答案
我做了一些源潜水进行调查。首先,注意的如果的的线性插值是用来提供 interpolatedTime
值到 applyTransformation
的<方法href=\"https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/animation/TranslateAnimation.java\"相对=nofollow> TranslateAnimation
,由此产生的翻译将有恒定的速度(因为偏移 DX
和 DY
是 interpolatedTime
的线性函数(线149-160)):
I did some source-diving to investigate this. First, note that if a linear interpolator is used to provide interpolatedTime
values to the applyTransformation
method of TranslateAnimation
, the resulting translation will have constant velocity (because the offsets dx
and dy
are linear functions of interpolatedTime
(lines 149-160)):
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
float dx = mFromXDelta;
float dy = mFromYDelta;
if (mFromXDelta != mToXDelta) {
dx = mFromXDelta + ((mToXDelta - mFromXDelta) * interpolatedTime);
}
if (mFromYDelta != mToYDelta) {
dy = mFromYDelta + ((mToYDelta - mFromYDelta) * interpolatedTime);
}
t.getMatrix().setTranslate(dx, dy);
}
applyTransformation
由基 getTransformation 方法调用href=\"https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/view/animation/Animation.java\"相对=nofollow> 动画
类(行869-870):
applyTransformation
is called by the getTransformation
method of the base Animation
class (lines 869-870):
...
final float interpolatedTime = mInterpolator.getInterpolation(normalizedTime);
applyTransformation(interpolatedTime, outTransformation);
...
据为 setInterpolator
办法(线382-392)的文件, mInterpolator
应该默认为线性内插器:
According to the documentation for the setInterpolator
method (lines 382-392), mInterpolator
should default to a linear interpolator:
/**
* Sets the acceleration curve for this animation. Defaults to a linear
* interpolation.
*
* @param i The interpolator which defines the acceleration curve
* @attr ref android.R.styleable#Animation_interpolator
*/
public void setInterpolator(Interpolator i) {
mInterpolator = i;
}
不过,这似乎是错误的:在动画
类调用两个构造的 ensureInterpolator
办法(行803 -811):
However, this seems to be false: both constructors in the Animation
class call the ensureInterpolator
method (lines 803-811):
/**
* Gurantees that this animation has an interpolator. Will use
* a AccelerateDecelerateInterpolator is nothing else was specified.
*/
protected void ensureInterpolator() {
if (mInterpolator == null) {
mInterpolator = new AccelerateDecelerateInterpolator();
}
}
这表明缺省插入是 AccelerateDecelerateInterpolator
。这说明你在你的问题描述的行为。
which suggests that the default interpolator is an AccelerateDecelerateInterpolator
. This explains the behavior you describe in your question.
要真正回答你的问题,那么,这样看来,你应该修改你的code如下:
To actually answer your question, then, it would appear that you should amend your code as follows:
Animation animation = new TranslateAnimation(0, 0, -500, 500);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(4000);
animation.setFillAfter(false);
myimage.startAnimation(animation);
animation.setRepeatCount(Animation.INFINITE);
这篇关于如何实现整个翻译动画均匀的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!