前言
SpringAnimation是由com.android.support:support-dynamic-animation支持包提供的弹性动画方案。SpringAnimation是由Springforce驱动的动画。弹簧力定义弹簧的刚度、阻尼比以及静止位置。一旦启动SpringAnimation,在每个帧上,弹簧力将更新动画的值和速度。动画将继续运行,直到弹簧力达到平衡。如果动画中使用的弹簧没有阻尼,动画将永远不会达到平衡,它将永远振荡。官方文档1;官方文档2
动画代码实例
Activity文件
package com.xol.viewpagerfragment;
import android.os.Bundle;
import android.support.animation.DynamicAnimation;
import android.support.animation.SpringAnimation;
import android.support.animation.SpringForce;
import android.support.annotation.FloatRange;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Toast;
public class ActivityForSpringAnim1 extends AppCompatActivity {
float STIFFNESS = SpringForce.STIFFNESS_MEDIUM;//硬度
float DAMPING_RATIO = SpringForce.DAMPING_RATIO_HIGH_BOUNCY;//阻尼
boolean isMove;
SpringAnimation xAnimation;//x方向
SpringAnimation yAnimation;//y方向
View movingView;//图片
int lastX = 0;
int lastY = 0;
private static final String TAG = "ActivityForSpringAnim1";
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_for_spring1);
movingView = findViewById(R.id.movingView);
// 以图片的初始位置创建动画对象
movingView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
xAnimation = createSpringAnimation(
movingView, SpringAnimation.X, movingView.getX(), STIFFNESS, DAMPING_RATIO);
yAnimation = createSpringAnimation(
movingView, SpringAnimation.Y, movingView.getY(), STIFFNESS, DAMPING_RATIO);
//初始位置确定,移除监听
movingView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
movingView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
isMove = false;
lastX = x;
lastY = y;
// 取消动画以便按住图片
xAnimation.cancel();
yAnimation.cancel();
break;
case MotionEvent.ACTION_MOVE:
int offX = x - lastX;
int offY = y - lastY;
movingView.offsetLeftAndRight(offX);
movingView.offsetTopAndBottom(offY);
isMove = true;
break;
case MotionEvent.ACTION_UP:
xAnimation.start();
yAnimation.start();
//没有移动是点击事件
if (!isMove) {
Toast.makeText(ActivityForSpringAnim1.this,
"click", Toast.LENGTH_LONG).show();
}
break;
}
return true;
}
});
}
/**
* 创建弹性动画
*
* @param view 动画关联的控件
* @param property 动画作用的属性
* @param finalPosition 动画结束的位置
* @param stiffness 硬度
* @param dampingRatio 阻尼
* @return
*/
SpringAnimation createSpringAnimation(View view,
DynamicAnimation.ViewProperty property,
Float finalPosition,
@FloatRange(from = 0.0) Float stiffness,
@FloatRange(from = 0.0) Float dampingRatio) {
//创建弹性动画类SpringAnimation
SpringAnimation animation = new SpringAnimation(view, property);
SpringForce spring = new SpringForce(finalPosition);
spring.setStiffness(stiffness);
spring.setDampingRatio(dampingRatio);
//关联弹性特质
animation.setSpring(spring);
return animation;
}
}
activity_for_spring1.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/movingView"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:src="@mipmap/ic_launcher_round"
tools:ignore="ContentDescription" />
</RelativeLayout>
效果展示
希望对您有所帮助!