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

你會(huì)想,自己的動(dòng)畫里加上 BounceInterpolator或OvershootInterpolator 插值器也能達(dá)到這種效果,然而實(shí)際上達(dá)不到。當(dāng)然你也可以自己寫插值器,如果你不嫌麻煩的話。
SpringAnimation彈性動(dòng)畫實(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)畫對(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)聽 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)畫以便按住圖片 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)畫 * @param view 動(dòng)畫關(guān)聯(lián)的控件 * @param property 動(dòng)畫作用的屬性 * @param finalPosition 動(dòng)畫結(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)畫類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>
觸摸改變圖片的位置,松開手啟動(dòng)動(dòng)畫。
翻譯自https://www.thedroidsonroids.com/blog/android/springanimation-examples/,原作者使用Kotlin語(yǔ)言實(shí)現(xiàn)的。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選