微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何在翻译动画中实现统一的速度?

我有一个应用于ImageView的无限翻译动画:

Animation animation = new TranslateAnimation(0, 0, -500, 500);
animation.setDuration(4000);
animation.setFillAfter(false);
myimage.startAnimation(animation);
animation.setRepeatCount(Animation.INFINITE);

我注意到的是,当图像接近起点和终点时,与距离近一半(中点)时相比,平移过程较慢.

我想在android上翻译动画的速度并不统一.

如何在整个过程中使速度均匀?

解决方法:

我做了一些消息来源调查这个.首先,请注意,如果使用线性插值器为TranslateAnimation的applyTransformation方法提供插值时间值,则生成的平移将具有恒定的速度(因为偏移dx和dy是interpolatedTime的线性函数(第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由基础Animation类的getTransformation方法调用(第869-870行):

...
final float interpolatedTime = mInterpolator.getInterpolation(normalizedTime);
applyTransformation(interpolatedTime, outTransformation);
...

根据setInterpolator方法的文档(第382-392行),mInterpolator应该认为线性插值器:

/**
 * 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;
}

但是,这似乎是错误的:Animation类中的两个构造函数调用ensureInterpolator方法(第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.这解释了您在问题中描述的行为.

要实际回答您的问题,您似乎应该按如下方式修改代码

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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐