Android 屬性動畫(Property Animation)介紹

介紹一下android的屬性動畫,爲什麼需要android的屬性動畫?Android提供了幾種動畫類型:View Animation 、Drawable Animation 、Property Animation 。View Animation相當簡單,不過只能支持簡單的縮放、平移、旋轉、透明度基本的動畫,且有一定的侷限性;比如我們使用動畫將按鈕變大,你希望當動畫停止時,View的位置就是當前的位置;這些View Animation都無法做到;

佈局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context="com.example.zhiwenyan.animationdemo.ButtonActivity">


    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="動畫屬性" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="動畫屬性" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="組合動畫屬性" />

    <Button
        android:id="@+id/btn3"
        android:background="#f57171"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:text="動畫屬性" />
  <Button
        android:id="@+id/btn4"
        android:background="#f57171"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:text="動畫屬性" />

</LinearLayout>
package com.example.zhiwenyan.animationdemo;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.widget.Button;
import android.widget.Toast;

public class ButtonActivity extends AppCompatActivity {

    private Button btn;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button);
        btn = (Button) findViewById(R.id.btn);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //使按鈕的寬度變爲500像素
                //直接設置Width
                ObjectAnimator.ofInt(btn, "width", 500).setDuration(1000).start();
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator oa = ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0.5f);
                oa.setDuration(1000);
                oa.start();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //組合動畫的使用
                PropertyValuesHolder pv1 = PropertyValuesHolder.ofInt("width", 600);
                PropertyValuesHolder pv2 = PropertyValuesHolder.ofFloat("alpha", 1, 0f, 0.5f);
                ObjectAnimator.ofPropertyValuesHolder(v, pv1, pv2).setDuration(1000).start();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //ObjectAnimator動畫執行的類
                ObjectAnimator oa1 = ObjectAnimator.ofFloat(v, "rotation", 0.0f, 360.0f);
                //x軸縮放 1.0f-1.2f
                ObjectAnimator oa2 = ObjectAnimator.ofFloat(v, "scaleX", 1.0f, 1.2f, 1.0f, 1.2f);
                //y軸縮放 1.0f-1.2f
                ObjectAnimator oa3 = ObjectAnimator.ofFloat(v, "scaleY", 1.0f, 1.2f, 1.0f, 1.2f);
                AnimatorSet as = new AnimatorSet();
                as.playTogether(oa1, oa2, oa3);
                as.setDuration(2000);
                as.start();

            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                ObjectAnimator oa1 = ObjectAnimator.ofFloat(v, "rotation", 0.0f, 360.0f);
                ObjectAnimator oa2 = ObjectAnimator.ofFloat(v, "scaleX", 1.0f, 1.2f, 1.0f, 1.2f);
                ObjectAnimator oa3 = ObjectAnimator.ofFloat(v, "scaleY", 1.0f, 1.2f, 1.0f, 1.2f);
                //Y軸平移100像素
               ObjectAnimator oa4 = ObjectAnimator.ofFloat(v, "translationY", 0f, 100f);
                //動畫一起執行的類
                AnimatorSet as = new AnimatorSet();
                //一起執行oa1,oa2,oa3,接着執行oa4
                as.play(oa1).with(oa2);
                as.play(oa2).with(oa3);
                as.play(oa4).after(oa3);

                //設置動畫插值器
                //AccelerateDecelerateInterpolator 加速插值器
                //BounceInterpolator   //彈性插值器
                as.setInterpolator(new AccelerateDecelerateInterpolator());
                as.setInterpolator(new BounceInterpolator());
                as.setDuration(2000);
                as.start();
                //動畫監聽的適配器
                as.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        //通過ViewGroup刪除
                        ViewGroup vg = (ViewGroup) v.getParent();
                        if (vg != null) {
                            vg.removeView(v);
                            Toast.makeText(ButtonActivity.this, "移除", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
//                //動畫監聽器
//                as.addListener(new Animator.AnimatorListener() {
//                    @Override
//                    public void onAnimationStart(Animator animation) {
//                    }
//                    //動畫結束後,移除掉
//                    @Override
//                    public void onAnimationEnd(Animator animation) {
//                        //通過ViewGroup刪除
//                        ViewGroup vg = (ViewGroup) v.getParent();
//                        if (vg != null) {
//                            vg.removeView(v);
//                            Toast.makeText(ButtonActivity.this, "移除", Toast.LENGTH_SHORT).show();
//                        }
//                    }
//                    @Override
//                    public void onAnimationCancel(Animator animation) {
//                    }
//                    @Override
//                    public void onAnimationRepeat(Animator animation) {
//
//                    }
//                });
//
            }
        });
    }
}

當然還可以通過xml文件設置屬動畫,在這就不介紹了,關鍵是要理解屬性的動畫的用途

小編的能力有限,如果那裏錯了,歡迎指出來!

發佈了33 篇原創文章 · 獲贊 95 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章