Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 702
Adham Avatar asked Jan 08 '14 23:01

Adham


2 Answers

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

like image 136
Chrisport Avatar answered Oct 07 '22 04:10

Chrisport


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

like image 24
Gilian Marques Avatar answered Oct 07 '22 02:10

Gilian Marques