Android SpringAnimation彈性動畫解析
也許你想在Android上實現(xiàn)這種反彈的動畫效果。Android Support Library 25.3.0引入了Dynamic-animation增強動畫,里面提供了幾個類用于使動畫呈現(xiàn)實現(xiàn)真實的物理效果。

你會想,自己的動畫里加上 BounceInterpolator或OvershootInterpolator 插值器也能達到這種效果,然而實際上達不到。當然你也可以自己寫插值器,如果你不嫌麻煩的話。
SpringAnimation彈性動畫實現(xiàn)方法
gradle引入,最低支持API16
dependencies {
compile 'com.android.support:support-dynamic-animation:25.3.0'
}
定義SpringForce,定義彈性特質(zhì)
SpringForce spring = new SpringForce(finalPosition); spring.setStiffness(stiffness); spring.setDampingRatio(dampingRatio);
定義SpringAnimation,并關(guān)聯(lián)SpringForce對象
SpringAnimation animation = new SpringAnimation(view, property); animation.setSpring(spring);
代碼如下
PositionActivity.java
public class PositionActivity extends AppCompatActivity {
float STIFFNESS = SpringForce.STIFFNESS_MEDIUM;//硬度
float DAMPING_RATIO = SpringForce.DAMPING_RATIO_HIGH_BOUNCY;//阻尼
SpringAnimation xAnimation;//x方向
SpringAnimation yAnimation;//y方向
View movingView;//圖片
float dX = 0f;
float dY = 0f;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_position);
movingView = findViewById(R.id.movingView);
// 以圖片的初始位置創(chuàng)建動畫對象
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);
//初始位置確定,移除監(jiān)聽
movingView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
movingView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// 計算到左上角的距離
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
// 取消動畫以便按住圖片
xAnimation.cancel();
yAnimation.cancel();
break;
case MotionEvent.ACTION_MOVE:
// 另一種改變View的LayoutParams(位置)的方式
movingView.animate()
.x(event.getRawX() + dX)
.y(event.getRawY() + dY)
.setDuration(0)
.start();
break;
case MotionEvent.ACTION_UP:
xAnimation.start();
yAnimation.start();
break;
}
return true;
}
});
}
/**
* 創(chuàng)建彈性動畫
* @param view 動畫關(guān)聯(lián)的控件
* @param property 動畫作用的屬性
* @param finalPosition 動畫結(jié)束的位置
* @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) {
//創(chuàng)建彈性動畫類SpringAnimation
SpringAnimation animation = new SpringAnimation(view, property);
//SpringForce類,定義彈性特質(zhì)
SpringForce spring = new SpringForce(finalPosition);
spring.setStiffness(stiffness);
spring.setDampingRatio(dampingRatio);
//關(guān)聯(lián)彈性特質(zhì)
animation.setSpring(spring);
return animation;
}
}
activity_position.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context=".PositionActivity"> <ImageView android:id="@+id/movingView" android:layout_width="128dp" android:layout_height="128dp" android:layout_gravity="center" android:src="@drawable/android" android:tint="@color/colorPrimary" tools:ignore="ContentDescription"/> </FrameLayout>
觸摸改變圖片的位置,松開手啟動動畫。
翻譯自https://www.thedroidsonroids.com/blog/android/springanimation-examples/,原作者使用Kotlin語言實現(xiàn)的。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android使用CountDownTimer實現(xiàn)倒數(shù)定時器效果
這篇文章主要介紹了Android使用CountDownTimer實現(xiàn)倒數(shù)定時器效果的資料,這里整理了詳細的代碼,有需要的小伙伴可以參考下。2017-02-02
Android ImageView的selector效果實例詳解
這篇文章主要介紹了Android ImageView的selector效果實例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android Studio徹底刪除項目 Android Studio徹底刪除Module
這篇文章主要為大家詳細介紹了Android Studio徹底刪除項目,Android Studio徹底刪除Module,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
android startActivityForResult的使用方法介紹
android startActivityForResult的使用方法介紹,需要的朋友可以參考一下2013-05-05
Android使用RadioGroup實現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細介紹了Android使用RadioGroup實現(xiàn)底部導(dǎo)航欄,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

