简体   繁体   中英

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.

Short Answer: From the name I guess your easyIn should be an 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.
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

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
"If xy% of total duration is passed, how much % of total animation should be passed?"

Take LinearInterpolator as example. The implementation will look like this:

public float getInterpolation(float t) {
        return t
}

Example: You animate from 0px to 200px with LinearInterpolator:
After 45% (0.45) of time 45% (return 0.45) of animation should be passed, which is 90px (200px*0.45)

Please read this for detailed information:

Android Animations Tutorial 5: More on Interpolators

Access https://matthewlein.com/tools/ceaser create a custom interpolator by manipulating the chart than copy values below the line

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.

this uses the cubic Bezier method and need 4 parameters.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM