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

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

概述我有一个应用于ImageView的无限翻译动画:Animationanimation=newTranslateAnimation(0,0,-500,500);animation.setDuration(4000);animation.setFillAfter(false);myimage.startAnimation(animation);animation.setRepeatCount(Animation.INFINITE);我注意到的是,当

我有一个应用于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行)):

@OverrIDeprotected 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);
总结

以上是内存溢出为你收集整理的android – 如何在翻译动画中实现统一的速度?全部内容,希望文章能够帮你解决android – 如何在翻译动画中实现统一的速度?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1095953.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存