原文:The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
packageandroid.animation;/** * A time interpolator defines the rate of change of an animation. This allows animations * to have non-linear motion, such as acceleration and deceleration. */publicinterfaceInterpolator{/** * Maps a value representing the elapsed fraction of an animation to a value that represents * the interpolated fraction. This interpolated value is then multiplied by the change in * value of an animation to derive the animated value at the current elapsed animation time. * * @param input 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 * @return The interpolation value. This value can be more than 1.0 for * interpolators which overshoot their targets, or less than 0 for * interpolators that undershoot their targets. */floatgetInterpolation(floatinput);}
packageandroid.view.animation;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.util.AttributeSet;/** * An interpolator where the rate of change starts out slowly and * and then accelerates. * */publicclassAccelerateInterpolatorimplementsInterpolator{privatefinalfloatmFactor;privatefinaldoublemDoubleFactor;publicAccelerateInterpolator(){mFactor=1.0f;mDoubleFactor=2.0;}/** * Constructor * * @param factor Degree to which the animation should be eased. Seting * factor to 1.0f produces a y=x^2 parabola. Increasing factor above * 1.0f exaggerates the ease-in effect (i.e., it starts even * slower and ends evens faster) */publicAccelerateInterpolator(floatfactor){mFactor=factor;mDoubleFactor=2*mFactor;}publicAccelerateInterpolator(Contextcontext,AttributeSetattrs){TypedArraya=context.obtainStyledAttributes(attrs,com.android.internal.R.styleable.AccelerateInterpolator);mFactor=a.getFloat(com.android.internal.R.styleable.AccelerateInterpolator_factor,1.0f);mDoubleFactor=2*mFactor;a.recycle();}publicfloatgetInterpolation(floatinput){if(mFactor==1.0f){returninput*input;}else{return(float)Math.pow(input,mDoubleFactor);}}}