Skip to content Skip to sidebar Skip to footer

Custom Android Acceleratedecelerateinterpolator

I am trying to use AccelerateDecelerateInterpolator and custom it. I can see that Interpolators like DecelerateInterpolator have a 'factor' field so you can change its behaviors. b

Solution 1:

You can implement standard easing function with your own Interpolator. For example, this would be the implementation for the easeInOutQuint:

publicclassMVAccelerateDecelerateInterpolatorimplementsInterpolator {

    // easeInOutQuintpublicfloatgetInterpolation(float t) {
        floatx= t*2.0f;
        if (t<0.5f) return0.5f*x*x*x*x*x;
        x = (t-0.5f)*2-1;
        return0.5f*x*x*x*x*x+1;
    }
}

enter image description here

Solution 2:

I tried to define my own TimeInterpolator as a custom AccelerateDecelerateInterpolator. I'm not too happy with the result, but it might give a few ideas. There is also sort of the factor in the code: 0.05f. Check it out:

TimeInterpolatorinterpolator=newTimeInterpolator() {
        @OverridepublicfloatgetInterpolation(float input) {
            return input + 0.05f * (float) Math.sin(2 * Math.PI * input);
        }
    };

Here is more refined solution. Tangent is probably a better function for the job, than sine. This solution acelerates at begining and in the end. There is also a factor to determine aceleration.

    TimeInterpolator interpolator = newTimeInterpolator() {
        privatefinalfloat mFactor   = 1.1f; // less than pi/2privatefloat       oldRetVal;
        privatefloat       oldInputVal;
        privatedouble      initValue = Math.tan(-mFactor);
        privatedouble      endValue  = 2 * Math.tan(mFactor);

        @Override
        publicfloatgetInterpolation(float input){
            if (oldInputVal != input) {
                oldInputVal = input;
                oldRetVal = (float) ((Math.tan(mFactor * (2 * input - 1)) - initValue) / endValue);
            }
            return oldRetVal;
        }
    };

Solution 3:

Android has added PathInterpolatorCompat to the v4 support library. Now using this: https://gist.github.com/ebabel/8ff41cad01e9ce1dd9ce you can specify a easeInOutQuint, easeInOutQuart, or an easeInOutExpo with ease!

publicstaticvoidexpand(final View v) {
        v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        finalinttargetHeight= v.getMeasuredHeight();

        if ( v.getHeight() != targetHeight ) {
            // Older versions of android (pre API 21) cancel animations for views with a height of 0 so use 1 instead.
            v.getLayoutParams().height = 1;
            v.setVisibility(View.VISIBLE);


            Animationa=newAnimation() {
                @OverrideprotectedvoidapplyTransformation(float interpolatedTime, Transformation t) {
                    v.getLayoutParams().height = interpolatedTime == 1
                            ? ViewGroup.LayoutParams.WRAP_CONTENT
                            : (int) (targetHeight * interpolatedTime);
                    v.requestLayout();
                }

                @OverridepublicbooleanwillChangeBounds() {
                    returntrue;
                }
            };

            a.setInterpolator(EasingsConstants.easeInOutQuart);
            a.setDuration(computeDurationFromHeight(v));
            v.startAnimation(a);
        } else {
            Log.d("AnimationUtil", "expand Already expanded ");
        }
    }

/**
 * 1dp/ms * multiplier
 */privatestaticintcomputeDurationFromHeight(View v) {
    return (int) (v.getMeasuredHeight() / v.getContext().getResources().getDisplayMetrics().density) * DURATION_MULTIPLIER;
}

And don't forget your build.gradle:

compile"com.android.support:support-v4:22.2.0"

Post a Comment for "Custom Android Acceleratedecelerateinterpolator"