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

你會(huì)想,自己的動(dòng)畫(huà)里加上 BounceInterpolator或OvershootInterpolator 插值器也能達(dá)到這種效果,然而實(shí)際上達(dá)不到。當(dāng)然你也可以自己寫(xiě)插值器,如果你不嫌麻煩的話(huà)。
SpringAnimation彈性動(dòng)畫(huà)實(shí)現(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對(duì)象
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)建動(dòng)畫(huà)對(duì)象
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)聽(tīng)
movingView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
movingView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// 計(jì)算到左上角的距離
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
// 取消動(dòng)畫(huà)以便按住圖片
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)建彈性動(dòng)畫(huà)
* @param view 動(dòng)畫(huà)關(guān)聯(lián)的控件
* @param property 動(dòng)畫(huà)作用的屬性
* @param finalPosition 動(dòng)畫(huà)結(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)建彈性動(dòng)畫(huà)類(lèi)SpringAnimation
SpringAnimation animation = new SpringAnimation(view, property);
//SpringForce類(lèi),定義彈性特質(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>
觸摸改變圖片的位置,松開(kāi)手啟動(dòng)動(dòng)畫(huà)。
翻譯自https://www.thedroidsonroids.com/blog/android/springanimation-examples/,原作者使用Kotlin語(yǔ)言實(shí)現(xiàn)的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程中關(guān)于單線(xiàn)程模型的理解與分析
這篇文章主要介紹了Android編程中關(guān)于單線(xiàn)程模型的理解與分析,較為詳細(xì)的分析了Android編程中關(guān)于單線(xiàn)程的原理與相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
詳解Android Activity中的幾種監(jiān)聽(tīng)器和實(shí)現(xiàn)方式
這篇文章主要介紹了Activity中的幾種監(jiān)聽(tīng)器和實(shí)現(xiàn)方式的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)教程第四篇之簡(jiǎn)單實(shí)現(xiàn)短信發(fā)送器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android實(shí)現(xiàn)列表元素動(dòng)態(tài)效果
本文將利用AnimatedList組件實(shí)現(xiàn)列表元素的一些動(dòng)態(tài)效果,例如添加元素時(shí)的漸現(xiàn)效果,刪除元素逐漸消失的效果等,感興趣的小伙伴可以了解一下2022-03-03

