下面列出了android.view.animation.Animation#setInterpolator() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void animateIn(FloatingActionButton button) {
button.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= 14) {
ViewCompat.animate(button)
.scaleX(1.0F)
.scaleY(1.0F)
.alpha(1.0F)
.setInterpolator(INTERPOLATOR)
.withLayer()
.setListener(null)
.start();
} else {
Animation anim = AnimationUtils.loadAnimation(button.getContext(),
android.support.design.R.anim.design_fab_in);
anim.setDuration(200L);
anim.setInterpolator(INTERPOLATOR);
button.startAnimation(anim);
}
}
private static Animation createShrinkAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta,
long startOffset, long duration, Interpolator interpolator) {
AnimationSet animationSet = new AnimationSet(false);
animationSet.setFillAfter(true);
final long preDuration = duration / 2;
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setStartOffset(startOffset);
rotateAnimation.setDuration(preDuration);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setFillAfter(true);
animationSet.addAnimation(rotateAnimation);
Animation translateAnimation = new RotateAndTranslateAnimation(0, toXDelta, 0, toYDelta, 360, 720);
translateAnimation.setStartOffset(startOffset + preDuration);
translateAnimation.setDuration(duration - preDuration);
translateAnimation.setInterpolator(interpolator);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
return animationSet;
}
private static Animation createShrinkAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta,
long startOffset, long duration, Interpolator interpolator) {
AnimationSet animationSet = new AnimationSet(false);
animationSet.setFillAfter(true);
final long preDuration = duration / 2;
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setStartOffset(startOffset);
rotateAnimation.setDuration(preDuration);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setFillAfter(true);
animationSet.addAnimation(rotateAnimation);
Animation translateAnimation = new RotateAndTranslateAnimation(0, toXDelta, 0, toYDelta, 360, 720);
translateAnimation.setStartOffset(startOffset + preDuration);
translateAnimation.setDuration(duration - preDuration);
translateAnimation.setInterpolator(interpolator);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
return animationSet;
}
/**
* 晃动动画
*
* @param counts 1秒钟晃动多少下
* @return
*/
public static Animation shakeAnimation(int counts) {
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(800);
return translateAnimation;
}
private Animation outToLeftAnimation() {
Animation outToLeft = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
outToLeft.setDuration(500);
outToLeft.setInterpolator(new LinearInterpolator());
return outToLeft;
}
private void setupAnimations() {
mAnimation = new Animation() {
@Override
public void applyTransformation(float interpolatedTime, Transformation t) {
setRotate(interpolatedTime);
}
};
mAnimation.setRepeatCount(Animation.INFINITE);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setInterpolator(LINEAR_INTERPOLATOR);
mAnimation.setDuration(ANIMATION_DURATION);
}
private void configureAnimation(Animation animation, Interpolator interpolator, int duration, int startOffset, int repeatMode, int repeatCount) {
animation.setInterpolator(interpolator);
animation.setDuration(duration);
animation.setStartOffset(startOffset);
animation.setRepeatMode(repeatMode);
animation.setRepeatCount(repeatCount);
}
/**
* 晃动动画
*
* @param counts 1秒钟晃动多少下
* @return
*/
public static Animation shakeAnimation(int counts) {
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateAnimation;
}
private void setupAnimations() {
mAnimation = new Animation() {
@Override
public void applyTransformation(float interpolatedTime, Transformation t) {
setRotate(interpolatedTime);
}
};
mAnimation.setRepeatCount(Animation.INFINITE);
mAnimation.setRepeatMode(Animation.RESTART);
mAnimation.setInterpolator(LINEAR_INTERPOLATOR);
mAnimation.setDuration(ANIMATION_DURATION);
}
private void hide(float x) {
this.lastPositionX = x;
float offset = getOffset(x);
int widthAdjustment = getWidthAdjustment();
AnimationSet animation = new AnimationSet(false);
Animation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
Animation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, offset + widthAdjustment,
Animation.ABSOLUTE, widthAdjustment,
Animation.RELATIVE_TO_SELF, -.25f,
Animation.RELATIVE_TO_SELF, -.25f);
scaleAnimation.setInterpolator(new AnticipateOvershootInterpolator(1.5f));
translateAnimation.setInterpolator(new DecelerateInterpolator());
animation.addAnimation(scaleAnimation);
animation.addAnimation(translateAnimation);
animation.setDuration(ANIMATION_DURATION);
animation.setFillBefore(true);
animation.setFillAfter(false);
animation.setInterpolator(new AnticipateOvershootInterpolator(1.5f));
recordButtonFab.setVisibility(View.GONE);
recordButtonFab.clearAnimation();
recordButtonFab.startAnimation(animation);
}
@Override
public void showNoData() {
mSwipeLayout.setVisibility(View.GONE);
refresh.setVisibility(View.VISIBLE);
Animation animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(1000);
animation.setRepeatCount(-1);
animation.setInterpolator(new LinearInterpolator());
refresh.startAnimation(animation);
mPresenter.doRefreshInNoData();
}
private static Animation createHintSwitchAnimation(final boolean expanded) {
Animation animation = new RotateAnimation(expanded ? 45 : 0, expanded ? 0 : 45, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setStartOffset(0);
animation.setDuration(100);
animation.setInterpolator(new DecelerateInterpolator());
animation.setFillAfter(true);
return animation;
}
/**
* Starts an animation that will jank if the UI thread is busy.
* @param animView
*/
public static void startAnimation(View animView) {
Animation tx = new TranslateAnimation(-350, 350, 0, 0);
tx.setDuration(1000);
tx.setRepeatCount(Animation.INFINITE);
tx.setInterpolator(new AccelerateDecelerateInterpolator());
tx.setRepeatMode(Animation.REVERSE);
animView.startAnimation(tx);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public static void fadeOutAnimation(final View view, int duration) {
Animation anim = new AlphaAnimation(1.0f, 0.0f);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.setDuration(duration);
view.setAnimation(anim);
view.setVisibility(View.GONE);
}
public static void expand(final View v) {
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final int targetHeight = v.getMeasuredHeight();
// Older versions of android (pre API 21) cancel animations for views with a height of 0.
v.getLayoutParams().height = 1;
v.setVisibility(View.VISIBLE);
Animation a = new Animation() {
@Override
public boolean willChangeBounds() {
return true;
} @Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
v.getLayoutParams().height = interpolatedTime == 1
? ViewGroup.LayoutParams.WRAP_CONTENT
: (int) (targetHeight * interpolatedTime);
v.requestLayout();
}
};
// 1dp/ms
a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density));
a.setInterpolator(new AccelerateInterpolator(3f));
v.startAnimation(a);
}
/**
* 晃动动画
* @param counts 1秒钟晃动多少下
* @return
*/
public static Animation shakeAnimation(int counts){
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(1000);
return translateAnimation;
}
private void showEclairMr1() {
clearAnimation();
setVisibility(VISIBLE);
Animation anim = createAnimationSet(0.0f, 1.0f, Math.round(mAnimationOffset), 0);
anim.setDuration(SHOW_HIDE_ANIM_DURATION);
anim.setInterpolator(AnimationUtils.LINEAR_OUT_SLOW_IN_INTERPOLATOR);
startAnimation(anim);
}
private static Animation getAlphaAnimation(float from, float to, int duration) {
final Animation anim = new AlphaAnimation(from, to);
anim.setInterpolator(new FastOutSlowInInterpolator());
anim.setDuration(duration);
return anim;
}
private void init() {
final LayoutParams tempFigureLayoutParams = new LayoutParams(this.diameter, this.diameter);
tempFigureLayoutParams.gravity = Gravity.CENTER;
for (int i = 0; i < this.figureViews.length; i++) {
final FigureView figureView = new FigureView(getContext());
figureView.setColor(this.colors[i]);
figureView.setFigureMargin(this.marginFigureCounter += this.marginFigure);
if (this.isRounded) {
figureView.setRounded();
}
Animation tempAnimation = new RotateAnimation(0, this.angle, this.radius, this.radius);
tempAnimation.setStartOffset(this.startOffsetCounter);
tempAnimation.setDuration(this.speed - this.startOffsetCounter);
this.startOffsetCounter -= this.startOffset;
figureView.setDrawingCacheEnabled(true);
tempAnimation.setFillEnabled(true);
tempAnimation.setFillBefore(true);
tempAnimation.setFillAfter(true);
tempAnimation.setRepeatMode(Animation.RESTART);
tempAnimation.setRepeatCount(Animation.INFINITE);
tempAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
figureView.startAnimation(tempAnimation);
figureView.setLayoutParams(tempFigureLayoutParams);
this.figureViews[i] = figureView;
this.addView(figureView);
}
if (this.isRotated) {
final Animation spinningAnimation = new RotateAnimation(0, 360, this.radius, this.radius);
spinningAnimation.setDuration(this.speed * 5);
spinningAnimation.setInterpolator(new LinearInterpolator());
spinningAnimation.setRepeatMode(Animation.RESTART);
spinningAnimation.setRepeatCount(Animation.INFINITE);
startAnimation(spinningAnimation);
}
}
public static Animation FadeOut() {
Animation anim = new AlphaAnimation(1 ,0);
anim.setInterpolator(new DecelerateInterpolator());
anim.setDuration(500);
return anim;
}