SpringAnimation使用示例

前言

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>

效果展示

在這裏插入圖片描述
希望對您有所幫助!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章