下面列出了怎么用android.view.animation.BounceInterpolator的API类实例代码及写法,或者点击链接到github查看源代码。
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public ObjectAnimator getSwipeInAnimator(View view, float deltaX, float deltaY){
float deltaXAbs = Math.abs(deltaX);
float fractionCovered = 1.f - (deltaXAbs / view.getWidth());
long duration = Math.abs((int) ((1 - fractionCovered) * 200 * mSpeedFactor));
// Animate position and alpha of swiped item
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view,
PropertyValuesHolder.ofFloat("alpha", 1.f),
PropertyValuesHolder.ofFloat("translationX", 0.f),
PropertyValuesHolder.ofFloat("rotationY", 0.f));
animator.setDuration(duration).setInterpolator(new BounceInterpolator());
return animator;
}
private void initShuttersConsumerBeforeAddToWrapper() {
shuttersConsumer = new ShuttersConsumer()
.setRefreshable(true)
.setLeavesCount(4)
.setScrimColor(0xAF000000)
.setRefreshFrameRate(30)
.setMaxSettleDuration(MAX_SETTLE_DURATION)
.setOpenDistance(SmartSwipe.dp2px(100, coverView.getContext()))
.setOverSwipeFactor(2F)
.setInterpolator(new BounceInterpolator())
.enableAllDirections()
.setWidth(width)
.setHeight(height)
.addListener(swipeListener)
;
}
@Override
public void onRefreshing(MaterialRefreshLayout br) {
setHeadHeight((int) (Util.dip2px(getContext(), DefaulHeadHeight)));
ValueAnimator animator = ValueAnimator.ofInt(getWaveHeight(),0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.i("anim", "value--->" + (int) animation.getAnimatedValue());
setWaveHeight((int) animation.getAnimatedValue());
invalidate();
}
});
animator.setInterpolator(new BounceInterpolator());
animator.setDuration(200);
animator.start();
}
public void showCountDownLayout() {//显示倒计时动画
View mCountDown3 = findViewById(R.id.tidal_pat_record_video_count_down_img_3);
View mCountDown6 = findViewById(R.id.tidal_pat_record_video_count_down_img_6);
View mCountDown9 = findViewById(R.id.tidal_pat_record_video_count_down_img_9);
mCountDown3.setVisibility(View.VISIBLE);
mCountDown6.setVisibility(View.VISIBLE);
mCountDown9.setVisibility(View.VISIBLE);
ArrayList<Animator> animators = new ArrayList<>();
animators.add(AnimatorUtils
.translationAnimator(mCountDown3, 0, -DensityUtils.dp2px(50), 0, 0, 400, new BounceInterpolator(), null));
animators.add(AnimatorUtils.translationAnimator(mCountDown6, 0, -DensityUtils.dp2px(100), 0, 0, 400, new BounceInterpolator(), null));
animators.add(AnimatorUtils.translationAnimator(mCountDown9, 0, -DensityUtils.dp2px(150), 0, 0, 400, new BounceInterpolator(), null));
animators.add(AnimatorUtils.rotationAnimator(mCountDown3, 360, 0, 400, null));
animators.add(AnimatorUtils.rotationAnimator(mCountDown6, 360, 0, 400, null));
animators.add(AnimatorUtils.rotationAnimator(mCountDown9, 360, 0, 400, null));
AnimatorUtils.playAnimatorArray(animators, AnimatorUtils.AnimatorPlayType.Together);
}
private Animation getAnimationSet(boolean fromXML) {
if (fromXML) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
return animation;
} else {
AnimationSet innerAnimationSet = new AnimationSet(true);
innerAnimationSet.setInterpolator(new BounceInterpolator());
innerAnimationSet.setStartOffset(1000);
innerAnimationSet.addAnimation(getScaleAnimation());
innerAnimationSet.addAnimation(getTranslateAnimation());
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new LinearInterpolator());
animationSet.addAnimation(getAlphaAnimation());
animationSet.addAnimation(getRotateAnimation());
animationSet.addAnimation(innerAnimationSet);
return animationSet;
}
}
@Override
public SkinAnimator apply(@NonNull View view, @Nullable final Action action) {
this.targetView = view;
preAnimator = ObjectAnimator.ofPropertyValuesHolder(targetView,
PropertyValuesHolder.ofFloat("translationX",
view.getLeft(), view.getRight()))
.setDuration(PRE_DURATION * 3);
preAnimator.setInterpolator(new AccelerateInterpolator());
afterAnimator = ObjectAnimator.ofPropertyValuesHolder(targetView,
PropertyValuesHolder.ofFloat("translationX",
view.getLeft() - view.getWidth(), view.getLeft()))
.setDuration(AFTER_DURATION * 3);
afterAnimator.setInterpolator(new BounceInterpolator());
preAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (action != null) {
action.action();
}
afterAnimator.start();
}
});
return this;
}
@Override
public void onClick(View v) {
super.onClick(v);
switch (v.getId()){
case R.id.point_view:
//ofInt方法后面的可变参数如果只传递一个参数
//在没有定义getRadius方法的情况下会出现如下警告:
//W/PropertyValuesHolder: Method getRadius() with type null not found on target class MyPointView
//ObjectAnimator objectAnimator = ObjectAnimator.ofInt(viewPoint,"radius",200);
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(viewPoint,"radius",20,200);
objectAnimator.setDuration(1000);
objectAnimator.setInterpolator(new BounceInterpolator());
objectAnimator.start();
break;
}
}
@OnClick({R.id.start_window, R.id.jump_other})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.start_window:
if (FloatWindow.get() != null) {
return;
}
FloatPlayerView floatPlayerView = new FloatPlayerView(getApplicationContext());
FloatWindow
.with(getApplicationContext())
.setView(floatPlayerView)
.setWidth(Screen.width, 0.4f)
.setHeight(Screen.width, 0.4f)
.setX(Screen.width, 0.8f)
.setY(Screen.height, 0.3f)
.setMoveType(MoveType.slide)
.setFilter(false)
.setMoveStyle(500, new BounceInterpolator())
.build();
FloatWindow.get().show();
break;
case R.id.jump_other:
startActivity(new Intent(this, EmptyActivity.class));
break;
}
}
private void initInterpolations() {
ArrayList<Class> interpolatorList = new ArrayList<Class>() {{
add(FastOutSlowInInterpolator.class);
add(BounceInterpolator.class);
add(LinearInterpolator.class);
add(DecelerateInterpolator.class);
add(CycleInterpolator.class);
add(AnticipateInterpolator.class);
add(AccelerateDecelerateInterpolator.class);
add(AccelerateInterpolator.class);
add(AnticipateOvershootInterpolator.class);
add(FastOutLinearInInterpolator.class);
add(LinearOutSlowInInterpolator.class);
add(OvershootInterpolator.class);
}};
try {
interpolatorSelector = (Interpolator) interpolatorList.get(animateSelector).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
private void startTootipAnimation() {
ObjectAnimator scaleY = ObjectAnimator.ofFloat(mTooltip, "scaleY", 0.8f);
scaleY.setDuration(200);
ObjectAnimator scaleYBack = ObjectAnimator.ofFloat(mTooltip, "scaleY", 1f);
scaleYBack.setDuration(500);
scaleYBack.setInterpolator(new BounceInterpolator());
final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setStartDelay(1000);
animatorSet.playSequentially(scaleY, scaleYBack);
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
animatorSet.setStartDelay(2000);
animatorSet.start();
}
});
mTooltip.setLayerType(View.LAYER_TYPE_HARDWARE, null);
animatorSet.start();
}
private void animateStar(final View view, int delay) {
ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 0, 1f);
alpha.setDuration(100);
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(alpha, scaleX, scaleY);
animatorSet.setInterpolator(new BounceInterpolator());
animatorSet.setStartDelay(delay);
animatorSet.setDuration(600);
view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
animatorSet.start();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
Music.showStar();
}
}, delay);
}
private void setupView() {
// 这个Interpolator你可以设置别的 我这里选择的是有弹跳效果的Interpolator
Interpolator polator = new BounceInterpolator();
mScroller = new Scroller(mContext, polator);
// 获取屏幕分辨率
WindowManager wm = (WindowManager) (mContext
.getSystemService(Context.WINDOW_SERVICE));
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
mScreenHeigh = dm.heightPixels;
mScreenWidth = dm.widthPixels;
// 这里你一定要设置成透明背景,不然会影响你看到底层布局
this.setBackgroundColor(Color.argb(0, 0, 0, 0));
mImgView = new ImageView(mContext);
mImgView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
mImgView.setScaleType(ImageView.ScaleType.FIT_XY);// 填充整个屏幕
// mImgView.setImageResource(R.drawable.ic_launcher); // 默认背景
mImgView.setBackgroundColor(Color.parseColor("#60000000"));
addView(mImgView);
}
@Override
public void onRefreshing(MaterialRefreshLayout br) {
setHeadHeight((int) (Util.dip2px(getContext(), DefaulHeadHeight)));
ValueAnimator animator = ValueAnimator.ofInt(getWaveHeight(),0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//Log.i("anim", "value--->" + (int) animation.getAnimatedValue());
setWaveHeight((int) animation.getAnimatedValue());
invalidate();
}
});
animator.setInterpolator(new BounceInterpolator());
animator.setDuration(200);
animator.start();
}
private Animation getAnimationSet(boolean fromXML) {
if (fromXML) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
return animation;
} else {
AnimationSet innerAnimationSet = new AnimationSet(true);
innerAnimationSet.setInterpolator(new BounceInterpolator());
innerAnimationSet.setStartOffset(1000);
innerAnimationSet.addAnimation(getScaleAnimation());
innerAnimationSet.addAnimation(getTranslateAnimation());
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new LinearInterpolator());
animationSet.addAnimation(getAlphaAnimation());
animationSet.addAnimation(getRotateAnimation());
animationSet.addAnimation(innerAnimationSet);
return animationSet;
}
}
public void testObjectAnimator(View v) {
if (v.getId() == R.id.sdi_objectanimator_btn) {
// 简单示例:View的横向移动
ObjectAnimator.ofFloat(mAnimView, "translationX", 0.0f, -200.0f)
.setDuration(C.Int.ANIM_DURATION * 2)
.start();
} else {
// 复合示例:View弹性落下然后弹起,执行一次
ObjectAnimator yBouncer = ObjectAnimator.ofFloat(mAnimView, "y", mAnimView.getY(), 400.0f);
yBouncer.setDuration(C.Int.ANIM_DURATION * 2);
// 设置插值器(用于调节动画执行过程的速度)
yBouncer.setInterpolator(new BounceInterpolator());
// 设置重复次数(缺省为0,表示不重复执行)
yBouncer.setRepeatCount(1);
// 设置重复模式(RESTART或REVERSE),重复次数大于0或INFINITE生效
yBouncer.setRepeatMode(ValueAnimator.REVERSE);
// 设置动画开始的延时时间(200ms)
yBouncer.setStartDelay(200);
yBouncer.start();
}
}
@Override
public void onRefreshing(MaterialRefreshLayout br) {
setHeadHeight((int) (Util.dip2px(getContext(), DefaulHeadHeight)));
ValueAnimator animator = ValueAnimator.ofInt(getWaveHeight(),0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.i("anim", "value--->" + (int) animation.getAnimatedValue());
setWaveHeight((int) animation.getAnimatedValue());
invalidate();
}
});
animator.setInterpolator(new BounceInterpolator());
animator.setDuration(200);
animator.start();
}
/**
* 进场动画
*/
private void in() {
int height = AndroidUtils.getScreenParams(getContext())[1];
ValueAnimator animator = ValueAnimator.ofObject(new IntEvaluator(), -height / 2, 0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override public void onAnimationUpdate(ValueAnimator animation) {
mWpm.y = (int) animation.getAnimatedValue();
mWindow.setAttributes(mWpm);
}
});
animator.setInterpolator(new BounceInterpolator()); //弹跳
Animator alpha = ObjectAnimator.ofFloat(mRootView, "alpha", 0f, 1f);
AnimatorSet set = new AnimatorSet();
set.play(animator).with(alpha);
set.setDuration(2000).start();
}
private void startNewSteadyAnim() {
final String propertyNameRotateX = "canvasRotateX";
final String propertyNameRotateY = "canvasRotateY";
PropertyValuesHolder holderRotateX = PropertyValuesHolder.ofFloat(propertyNameRotateX, canvasRotateX, 0);
PropertyValuesHolder holderRotateY = PropertyValuesHolder.ofFloat(propertyNameRotateY, canvasRotateY, 0);
steadyAnim = ValueAnimator.ofPropertyValuesHolder(holderRotateX, holderRotateY);
steadyAnim.setDuration(1000);
steadyAnim.setInterpolator(new BounceInterpolator());
steadyAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
canvasRotateX = (float) animation.getAnimatedValue(propertyNameRotateX);
canvasRotateY = (float) animation.getAnimatedValue(propertyNameRotateY);
}
});
steadyAnim.start();
}
@Override
public void onRefreshing(MaterialRefreshLayout br) {
setHeadHeight((int) (Util.dip2px(getContext(), DefaulHeadHeight)));
ValueAnimator animator = ValueAnimator.ofInt(getWaveHeight(),0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.i("anim", "value--->" + (int) animation.getAnimatedValue());
setWaveHeight((int) animation.getAnimatedValue());
invalidate();
}
});
animator.setInterpolator(new BounceInterpolator());
animator.setDuration(200);
animator.start();
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public ObjectAnimator getSwipeInAnimator(View view, float deltaX, float deltaY){
float deltaXAbs = Math.abs(deltaX);
float fractionCovered = 1.f - (deltaXAbs / view.getWidth());
long duration = Math.abs((int) ((1 - fractionCovered) * 200 * mSpeedFactor));
// Animate position and alpha of swiped item
ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view,
PropertyValuesHolder.ofFloat("alpha", 1.f),
PropertyValuesHolder.ofFloat("translationX", 0.f),
PropertyValuesHolder.ofFloat("rotationY", 0.f));
animator.setDuration(duration).setInterpolator(new BounceInterpolator());
return animator;
}
/**
* @param container
* @hide
*/
@Override
public void updatePreLayout(ConstraintLayout container) {
if (mContainer!=container) {
View[] views = getViews(container);
for (int i = 0; i < mCount; i++) {
View view = views[i];
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "translationX", - 2000, 0).setDuration(1000);
animator.setInterpolator(new BounceInterpolator());
animator.start();
}
}
mContainer = container;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
setPressed(true);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
animate().scaleX(0.98f).scaleY(0.98f).setDuration(100)
.setInterpolator(new DecelerateInterpolator());
} else {
ViewCompat.setElevation(this, 8.f);
}
break;
}
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: {
setPressed(false);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
animate().scaleX(1.f).scaleY(1.f).setDuration(50)
.setInterpolator(new BounceInterpolator());
} else {
ViewCompat.setElevation(this, 0.f);
}
break;
}
}
return super.onTouchEvent(event);
}
private void initUI() {
mImageView = new ImageView(getApplicationContext());
mImageView2 = new ImageView(getApplicationContext());
mBuilderA = FloatWindow.with(getApplicationContext()).setView(mImageView).setWidth(EScreen.WIDTH, 0.2f)
.setHeight(EScreen.WIDTH, 0.2f).setX(EScreen.WIDTH, 0.8f).setY(EScreen.HEIGHT, 0.3f)
.setMoveType(EMoveType.SLIDE).setMoveStyle(500, new BounceInterpolator()).setDesktopShow(true)
.setTag("mFirstWindow");
}
/**
* marker点击时跳动一下
*/
public void jumpPoint(final Marker marker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = aMap.getProjection();
final LatLng markerLatlng = marker.getPosition();
Point markerPoint = proj.toScreenLocation(markerLatlng);
markerPoint.offset(0, -100);
final LatLng startLatLng = proj.fromScreenLocation(markerPoint);
final long duration = 1500;
final Interpolator interpolator = new BounceInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * markerLatlng.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * markerLatlng.latitude + (1 - t)
* startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
handler.postDelayed(this, 16);
}
}
});
}
/**
* marker点击时跳动一下
*/
public void jumpPoint(final Marker marker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = aMap.getProjection();
Point startPoint = proj.toScreenLocation(Constants.XIAN);
startPoint.offset(0, -100);
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 1500;
final Interpolator interpolator = new BounceInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * Constants.XIAN.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * Constants.XIAN.latitude + (1 - t)
* startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
handler.postDelayed(this, 16);
}
}
});
}
private void initDoorConsumerBeforeAddToWrapper() {
doorConsumer = new DoorConsumer()
.setRefreshable(true)
.setScrimColor(0xAF000000)
.setMaxSettleDuration(MAX_SETTLE_DURATION)
.setInterpolator(new BounceInterpolator())
.enableAllDirections()
.setWidth(width)
.setHeight(height)
.addListener(swipeListener)
;
}
private void initTranslucentSlidingConsumerBeforeAddToWrapper() {
translucentSlidingConsumer = new TranslucentSlidingConsumer()
.showScrimAndShadowOutsideContentView()
.setScrimColor(0xAF000000)
.setMaxSettleDuration(MAX_SETTLE_DURATION)
.setInterpolator(new BounceInterpolator())
.enableAllDirections()
.setWidth(width)
.setHeight(height)
.addListener(swipeListener)
;
}
public ObjectAnimator newEnterAnimator(DialogEnterAnimation enterAnimation, long durationMs) {
if (enterAnimation == null) {
throw new IllegalStateException("enter animation mustn't be null");
}
ObjectAnimator animator;
switch (enterAnimation) {
case SLIDE_IN_FROM_RIGHT:
animator = ObjectAnimator.ofFloat(
getDialogDecorView(),
"translationX",
requireActivity().getWindow().getDecorView().getWidth(),
0
);
break;
case SLIDE_IN_AND_BOUNCE_FROM_TOP:
animator = ObjectAnimator.ofFloat(
getDialogDecorView(),
"translationY",
-requireActivity().getWindow().getDecorView().getHeight(),
0
);
animator.setInterpolator(new BounceInterpolator());
break;
default:
throw new RuntimeException("unhandled enter animation: " + enterAnimation);
}
animator.setDuration(durationMs);
return animator;
}
private void doBounceAnimation(View targetView) {
ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 100, 0);
animator.setInterpolator(new BounceInterpolator());
animator.setStartDelay(500);
animator.setDuration(2500);
animator.start();
}
/**
* 显示floatingButton
*/
@SuppressLint("RestrictedApi")
private void showFloatingButton(){
if(fbtnUp.getVisibility() == View.INVISIBLE){
fbtnUp.setVisibility(View.VISIBLE);
mShowFbtnAnimator = fbtnUp.animate().setDuration(500).setInterpolator(new BounceInterpolator()).translationY(0);
mShowFbtnAnimator.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
fbtnUp.setVisibility(View.VISIBLE);
}
});
}
}