[英]How to create custom Interpolator to apply translate animation in android
I want to create a custom interpolate to apply translate animation where the animation should go through the following function:我想创建一个自定义插值来应用翻译动画,动画应该通过以下函数:
public static float easeIn(float t,float b , float c, float d) {
return c*(t/=d)*t + b;
}
where :在哪里 :
t: current time
b: start value
c: change in value
d: duration
I have found one to implement scale animation where if take only one parameter:我找到了一个实现缩放动画的方法,如果只使用一个参数:
import android.view.animation.Interpolator;
public class MyInterpolator implements Interpolator {
public MyInterpolator() {
}
public float getInterpolation(float t) {
float x = 2.0f * t - 1.0f;
return 0.5f * (x * x * x + 1.0f);
}
}
how create in interpolate to make translate using the above function.如何在 interpolate 中创建以使用上述函数进行翻译。
Short Answer: From the name I guess your easyIn should be an AccelerateInterpolator简短回答:从名字上我猜你的 easyIn 应该是一个 AccelerateInterpolator
The function you write is not what the interpolator can do.你写的函数不是插值器能做的。 The interpolator doesn't care if it is a Scale-, Alpha- or TranslateAnimation.
插值器不关心它是 Scale-、Alpha- 还是 TranslateAnimation。
The docs explains Interpolator getInterpolation like this: A value between 0 and 1.0 indicating our current point in the animation where 0 represents the start and 1.0 represents the end该文档解释了 Interpolator getInterpolation 如下: 0 到 1.0 之间的值表示动画中的当前点,其中 0 表示开始,1.0 表示结束
The interpolator provides a function that maps from the elapsed time (relatively) to the progress of the animation.插值器提供了一个函数,可以将经过的时间(相对)映射到动画的进度。 You can imagine it in %, the getInterpolation(xy) will tell you
你可以用 % 来想象,getInterpolation(xy) 会告诉你
"If xy% of total duration is passed, how much % of total animation should be passed?" “如果通过了总持续时间的 xy%,那么应该通过多少百分比的总动画?”
Take LinearInterpolator as example.以 LinearInterpolator 为例。 The implementation will look like this:
实现将如下所示:
public float getInterpolation(float t) {
return t
}
Example: You animate from 0px to 200px with LinearInterpolator:示例:您使用 LinearInterpolator 从 0px 到 200px 设置动画:
After 45% (0.45) of time 45% (return 0.45) of animation should be passed, which is 90px (200px*0.45)在 45% (0.45) 的时间之后,应该传递 45% (返回 0.45) 的动画,即 90px (200px*0.45)
Please read this for detailed information:请阅读本文以获取详细信息:
Android Animations Tutorial 5: More on Interpolators Android 动画教程 5:有关插值器的更多信息
Access https://matthewlein.com/tools/ceaser create a custom interpolator by manipulating the chart than copy values below the line访问https://matthewlein.com/tools/ceaser通过操作图表创建自定义插值器而不是复制线下方的值
Code snippets, short and long-hand:代码片段,简短的和长期的:
Than use比用
Interpolator mInterpolator = PathInterpolatorCompat.create(1.000f, 0.000f, 1.000f, 1.030f)
with the copied values to create your custom interpolator使用复制的值创建自定义插值器
You can use PathInterpolatorCompat class.您可以使用PathInterpolatorCompat类。
this uses the cubic Bezier method and need 4 parameters.这使用三次贝塞尔方法并需要 4 个参数。
you can this site for get parametrs needed or this site你可以在这个网站上获取需要的参数或这个网站
val interpolatorCompat = PathInterpolatorCompat.create(0.12,1.05f,.98f,-0.82f)
anim.setInterpolator(interpolatorCompat);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.