下面列出了怎么用android.view.animation.Interpolator的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 设置viewPager滑动动画持续时间
* API>19
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setAnimationDuration(final int during){
try {
// viewPager平移动画事件
Field mField = ViewPager.class.getDeclaredField("mScroller");
mField.setAccessible(true);
// 动画效果与ViewPager的一致
Interpolator interpolator = new Interpolator() {
@Override
public float getInterpolation(float t) {
t -= 1.0f;
return t * t * t * t * t + 1.0f;
}
};
FixedSpeedScroller scroller = new FixedSpeedScroller(getContext(),
interpolator, mRecentTouchTime);
scroller.setDuration(during);
mField.set(this, scroller);
} catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) {
e.printStackTrace();
}
}
private void reverse(Animation animation) {
Interpolator interpolator = animation.getInterpolator();
if (interpolator != null) {
if (interpolator instanceof ReverseInterpolator) {
animation.setInterpolator(((ReverseInterpolator) interpolator).delegate);
} else {
animation.setInterpolator(new ReverseInterpolator(interpolator));
}
} else {
animation.setInterpolator(new ReverseInterpolator());
}
if (animation instanceof AnimationSet) {
List<Animation> animationList = ((AnimationSet) animation).getAnimations();
for (int i = 0; i < animationList.size(); i++) {
reverse(animationList.get(i));
}
}
}
/**
* Creates and returns a layout listener, which allows to start a peek animation to add a tab,
* once its view has been inflated.
*
* @param item
* The item, which corresponds to the tab, which should be added, as an instance of the
* class {@link AbstractItem}. The item may not be null
* @param peekAnimation
* The peek animation, which should be started, as an instance of the class {@link
* PeekAnimation}. The peek animation may not be null
* @return The listener, which has been created, as an instance of the type {@link
* OnGlobalLayoutListener}. The listener may not be null
*/
private OnGlobalLayoutListener createPeekLayoutListener(@NonNull final AbstractItem item,
@NonNull final PeekAnimation peekAnimation) {
return new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
long totalDuration =
peekAnimation.getDuration() != -1 ? peekAnimation.getDuration() :
peekAnimationDuration;
long duration = totalDuration / 3;
Interpolator interpolator =
peekAnimation.getInterpolator() != null ? peekAnimation.getInterpolator() :
new AccelerateDecelerateInterpolator();
float peekPosition =
getArithmetics().getTabContainerSize(Axis.DRAGGING_AXIS, false) * 0.66f;
animatePeek(item, duration, interpolator, peekPosition, peekAnimation);
}
};
}
public static Interpolator getFastOutSlowInInterpolator(Context context) {
if (fastOutSlowIn == null) {
fastOutSlowIn = AnimationUtils.loadInterpolator(context,
android.R.interpolator.fast_out_slow_in);
}
return fastOutSlowIn;
}
@Test
public void partialInterpolator_isModified_when_setPartialInterpolator_isCalled() {
//given
StaggeredAnimationGroup spiedGroup = prepareSpiedGroup();
Interpolator testInterpolator = new LinearOutSlowInInterpolator();
//when
spiedGroup.setPartialInterpolator(testInterpolator);
//then
assertThat(spiedGroup.partialInterpolator).isEqualTo(testInterpolator);
}
private LineMorphingDrawable(State[] states, int curState, int paddingLeft, int paddingTop, int paddingRight, int paddingBottom, int animDuration, Interpolator interpolator, int strokeSize, int strokeColor, Paint.Cap strokeCap, Paint.Join strokeJoin, boolean clockwise, boolean isRtl){
mStates = states;
mPaddingLeft = paddingLeft;
mPaddingTop = paddingTop;
mPaddingRight = paddingRight;
mPaddingBottom = paddingBottom;
mAnimDuration = animDuration;
mInterpolator = interpolator;
mStrokeSize = strokeSize;
mStrokeColor = strokeColor;
mStrokeCap = strokeCap;
mStrokeJoin = strokeJoin;
mClockwise = clockwise;
mIsRtl = isRtl;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(mStrokeCap);
mPaint.setStrokeJoin(mStrokeJoin);
mPaint.setColor(mStrokeColor);
mPaint.setStrokeWidth(mStrokeSize);
mDrawBound = new RectF();
mPath = new Path();
switchLineState(curState, false);
}
public CanvasTransformer translate(final int openedX, final int closedX,
final int openedY, final int closedY, final Interpolator interp) {
initTransformer();
mTrans = new CanvasTransformer() {
public void transformCanvas(Canvas canvas, float percentOpen) {
mTrans.transformCanvas(canvas, percentOpen);
float f = interp.getInterpolation(percentOpen);
canvas.translate((openedX - closedX) * f + closedX,
(openedY - closedY) * f + closedY);
}
};
return mTrans;
}
/**
* <p>Performs a zoom animation from <code>scaleFrom</code> to <code>scaleTo</code> using the given <code>ScaleAnimatorHandler</code>.</p>
* @param scaleFrom
* @param scaleTo
* @param duration
* @param scaleAnimatorHandler
* @param interpolator
*/
private void animateZoom(float scaleFrom, float scaleTo, long duration, ScaleAnimatorHandler scaleAnimatorHandler, Interpolator interpolator) {
if(isAnimating()) {
throw new IllegalStateException("An animation is currently running; Check isAnimating() first!");
}
valueAnimator = ValueAnimator.ofFloat(scaleFrom, scaleTo);
valueAnimator.setDuration(duration);
valueAnimator.addUpdateListener(scaleAnimatorHandler);
if(interpolator != null) valueAnimator.setInterpolator(interpolator);
valueAnimator.start();
}
@Nullable
private static WeakReference<Interpolator> getInterpolator(int hash) {
// This must be synchronized because get and put isn't thread safe because
// SparseArrayCompat has to create new sized arrays sometimes.
synchronized (KeyframeParser.class) {
return pathInterpolatorCache().get(hash);
}
}
public static Interpolator parse(Integer type, Float cycles) {
if (type != null) {
switch (type) {
case 0:
return new AccelerateDecelerateInterpolator();
case 1:
return new AccelerateInterpolator();
case 2:
return new AnticipateInterpolator();
case 3:
return new AnticipateOvershootInterpolator();
case 4:
return new BounceInterpolator();
case 5:
return new CycleInterpolator((cycles != null && cycles > 0) ? cycles : 1f);
case 6:
return new DecelerateInterpolator();
case 7:
return new LinearInterpolator();
case 8:
return new OvershootInterpolator();
default:
return new LinearInterpolator();
}
} else {
return new LinearInterpolator();
}
}
@NonNull
public static Interpolator getInterpolator(int id) {
switch (id) {
case 0:
return new LinearInterpolator();
case 1:
return new AccelerateInterpolator();
case 2:
return new DecelerateInterpolator();
case 3:
return new AccelerateDecelerateInterpolator();
case 4:
return new OvershootInterpolator();
case 5:
return new AnticipateInterpolator();
case 6:
return new AnticipateOvershootInterpolator();
case 7:
return new BounceInterpolator();
case 8:
return new FastOutLinearInInterpolator();
case 9:
return new LinearInterpolator();
case 10:
return new LinearOutSlowInInterpolator();
default:
return new FastOutSlowInInterpolator();
}
}
public Morpho build(AnimationType animationType, int duration, Interpolator interpolator) {
if (animationType != null) {
this.completeAnimationAnimationType = animationType;
}
this.completeAnimationDuration = duration;
if (interpolator != null) {
this.completeAnimationInterpolator = interpolator;
}
return this;
}
/**
* Prepares the specified animation with a standard duration, interpolator, etc.
*/
Animation prepareThumbnailAnimationWithDuration(Animation a, int appWidth, int appHeight,
long duration, Interpolator interpolator) {
if (duration > 0) {
a.setDuration(duration);
}
a.setFillAfter(true);
if (interpolator != null) {
a.setInterpolator(interpolator);
}
a.initialize(appWidth, appHeight, appWidth, appHeight);
return a;
}
private static float lerp(float startValue, float endValue, float fraction,
Interpolator interpolator) {
if (interpolator != null) {
fraction = interpolator.getInterpolation(fraction);
}
return AnimUtils.lerp(startValue, endValue, fraction);
}
/**
* 屏幕中心marker 跳动
*/
public void startJumpAnimation() {
if (screenMarker != null ) {
//根据屏幕距离计算需要移动的目标点
final LatLng latLng = screenMarker.getPosition();
Point point = aMap.getProjection().toScreenLocation(latLng);
point.y -= dip2px(this,125);
LatLng target = aMap.getProjection()
.fromScreenLocation(point);
//使用TranslateAnimation,填写一个需要移动的目标点
Animation animation = new TranslateAnimation(target);
animation.setInterpolator(new Interpolator() {
@Override
public float getInterpolation(float input) {
// 模拟重加速度的interpolator
if(input <= 0.5) {
return (float) (0.5f - 2 * (0.5 - input) * (0.5 - input));
} else {
return (float) (0.5f - Math.sqrt((input - 0.5f)*(1.5f - input)));
}
}
});
//整个移动所需要的时间
animation.setDuration(600);
//设置动画
screenMarker.setAnimation(animation);
//开始动画
screenMarker.startAnimation();
} else {
Log.e("amap","screenMarker is null");
}
}
public CanvasTransformer zoom(final int openedX, final int closedX,
final int openedY, final int closedY,
final int px, final int py, final Interpolator interp) {
initTransformer();
mTrans = new CanvasTransformer() {
public void transformCanvas(Canvas canvas, float percentOpen) {
mTrans.transformCanvas(canvas, percentOpen);
float f = interp.getInterpolation(percentOpen);
canvas.scale((openedX - closedX) * f + closedX,
(openedY - closedY) * f + closedY, px, py);
}
};
return mTrans;
}
/**
* Create a Scroller with the specified interpolator. If the interpolator is
* null, the default (viscous) interpolator will be used. Specify whether or
* not to support progressive "flywheel" behavior in flinging.
*/
public LauncherScroller(Context context, Interpolator interpolator, boolean flywheel) {
mFinished = true;
mInterpolator = interpolator;
mPpi = context.getResources().getDisplayMetrics().density * 160.0f;
mDeceleration = computeDeceleration(ViewConfiguration.getScrollFriction());
mFlywheel = flywheel;
mPhysicalCoeff = computeDeceleration(0.84f); // look and feel tuning
}
private RippleDrawable(Drawable backgroundDrawable, int backgroundAnimDuration, int backgroundColor, int rippleType, int delayClickType, int maxRippleRadius, int rippleAnimDuration, int rippleColor, Interpolator inInterpolator, Interpolator outInterpolator, int type, int topLeftCornerRadius, int topRightCornerRadius, int bottomRightCornerRadius, int bottomLeftCornerRadius, int left, int top, int right, int bottom){
setBackgroundDrawable(backgroundDrawable);
mBackgroundAnimDuration = backgroundAnimDuration;
mBackgroundColor = backgroundColor;
mRippleType = rippleType;
setDelayClickType(delayClickType);
mMaxRippleRadius = maxRippleRadius;
mRippleAnimDuration = rippleAnimDuration;
mRippleColor = rippleColor;
if(mRippleType == TYPE_TOUCH && mMaxRippleRadius <= 0)
mRippleType = TYPE_TOUCH_MATCH_VIEW;
mInInterpolator = inInterpolator;
mOutInterpolator = outInterpolator;
setMask(type, topLeftCornerRadius, topRightCornerRadius, bottomRightCornerRadius, bottomLeftCornerRadius, left, top, right, bottom);
mFillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mFillPaint.setStyle(Paint.Style.FILL);
mShaderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mShaderPaint.setStyle(Paint.Style.FILL);
mBackground = new Path();
mBackgroundBounds = new RectF();
mRipplePoint = new PointF();
mMatrix = new Matrix();
mInShader = new RadialGradient(0, 0, GRADIENT_RADIUS, new int[]{mRippleColor, mRippleColor, 0}, GRADIENT_STOPS, Shader.TileMode.CLAMP);
if(mRippleType == TYPE_WAVE)
mOutShader = new RadialGradient(0, 0, GRADIENT_RADIUS, new int[]{0, ColorUtil.getColor(mRippleColor, 0f), mRippleColor}, GRADIENT_STOPS, Shader.TileMode.CLAMP);
}
private CircularProgressDrawable(int padding, float initialAngle, float progressPercent, float secondaryProgressPercent, float maxSweepAngle, float minSweepAngle, int strokeSize, int[] strokeColors, int strokeSecondaryColor, boolean reverse, int rotateDuration, int transformDuration, int keepDuration, Interpolator transformInterpolator, int progressMode, int inAnimDuration, float inStepPercent, int[] inStepColors, int outAnimDuration, int alpha) {
mPadding = padding;
mInitialAngle = initialAngle;
mAlpha = alpha;
mProgressPercent = Math.min(1f, Math.max(0f, progressPercent));
// setProgress(progressPercent);
setSecondaryProgress(secondaryProgressPercent);
mMaxSweepAngle = maxSweepAngle;
mMinSweepAngle = minSweepAngle;
mStrokeSize = strokeSize;
mStrokeColors = strokeColors;
mStrokeSecondaryColor = strokeSecondaryColor;
mReverse = reverse;
mRotateDuration = rotateDuration;
mTransformDuration = transformDuration;
mKeepDuration = keepDuration;
mTransformInterpolator = transformInterpolator;
mProgressMode = progressMode;
mInAnimationDuration = inAnimDuration;
mInStepPercent = inStepPercent;
mInColors = inStepColors;
mOutAnimationDuration = outAnimDuration;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeCap(Paint.Cap.BUTT);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mRect = new RectF();
mState = createConstantState(null, null);
}
public static void alpha(View view, float fromAlpha, float toAlpha, int duration, Interpolator interpolator) {
AlphaAnimation animation = new AlphaAnimation(fromAlpha, toAlpha);
animation.setDuration(duration);
animation.setFillAfter(true);
animation.setInterpolator(interpolator);
view.startAnimation(animation);
}
private static Interpolator getInterpolator(InterpolatorType type, ReadableMap params) {
Interpolator interpolator;
if (type.equals(InterpolatorType.SPRING)) {
interpolator = new SimpleSpringInterpolator(SimpleSpringInterpolator.getSpringDamping(params));
} else {
interpolator = INTERPOLATOR.get(type);
}
if (interpolator == null) {
throw new IllegalArgumentException("Missing interpolator for type : " + type);
}
return interpolator;
}
public void setTextSizeInterpolator(Interpolator interpolator) {
mTextSizeInterpolator = interpolator;
recalculate();
}
public void setInterpolator(Interpolator interpolator) {
mInterpolator = interpolator;
}
public void translateXInterpolator(Interpolator interpolator) {
mTransX.setInterpolator(interpolator);
}
public ViewPagerScroller(Context context, Interpolator interpolator,
boolean flywheel) {
super(context, interpolator, flywheel);
}
public Morpho dimensions(float width, float height, int duration, Interpolator interpolator) {
if (width >= 0 && height >= 0) {
animationParts.add(new DimensionsAnimation(width, height, duration, interpolator));
}
return this;
}
public static Interpolator create(float controlX1, float controlY1, float controlX2, float controlY2) {
if (VERSION.SDK_INT >= 21) {
return PathInterpolatorCompatApi21.create(controlX1, controlY1, controlX2, controlY2);
}
return PathInterpolatorCompatBase.create(controlX1, controlY1, controlX2, controlY2);
}
@Override
public Interpolator createInterpolatorFor(AnimationBody animationBody) {
return PathInterpolatorCompat.create(0.19f, 1f, 0.22f, 1f);
}
SwipeBehaviour(float range, Interpolator interpolator) {
this.range = range;
this.interpolator = interpolator;
}
public void setPositionInterpolator(Interpolator interpolator) {
mPositionInterpolator = interpolator;
recalculate();
}