1. 程式人生 > >Android中屬性動畫和補間動畫的區別

Android中屬性動畫和補間動畫的區別

 屬性動畫和補間動畫的區別是,補間動畫只是表面上實現了平移,旋轉,漸變,縮放,實際上屬性值不變;
 屬性動畫實現平移,旋轉,漸變,縮放後,屬性值變了

 下面就是測試的例子

程式碼:

package com.atguigu.propertyanimation;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 測試屬性動畫的基本使用
 * 屬性動畫和補間動畫的區別是,補間動畫只是表面上實現了平移,旋轉,漸變,縮放,實際上屬性值不變;
 * 屬性動畫實現平移,旋轉,漸變,縮放後,屬性值變了
 * 下面就是測試的例子
 */
public class MainActivity extends Activity {

    private ImageView iv_animation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv_animation = (ImageView) findViewById(R.id.iv_animation);
        iv_animation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "點選了圖片", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 補間(檢視)動畫
     * @param v
     */
    public void testTweenAnimation(View v) {
        TranslateAnimation animation = new TranslateAnimation(0, iv_animation.getWidth(), 0, iv_animation.getHeight());
        animation.setDuration(3000);
        animation.setFillAfter(true);
        iv_animation.startAnimation(animation);
    }


    private AnimatorSet animatorSet;
    /**
     * 測試屬性動畫
     */
    public void testPropertyAnimation(View v) {
//      x軸上移動
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv_animation,"translationX",0,iv_animation.getWidth());
//      y軸上移動
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(iv_animation,"translationY",0,iv_animation.getHeight());
        AnimatorSet set = new AnimatorSet();
//      兩個動畫一起播放
        set.playTogether(animator3,animator4);
//      播放時間2秒
        set.setDuration(2000);
//      開始播放
        set.start();

//      //另外一種寫法
//        iv_animation.animate()
//                 .translationXBy(iv_animation.getWidth())
//                 .translationYBy(iv_animation.getWidth())
//                 .setDuration(2000)
//                 .setInterpolator(new BounceInterpolator())
//                 .start();


//        ObjectAnimator animator = ObjectAnimator.ofFloat(iv_animation, "translationX", 0,iv_animation.getWidth());
//        ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv_animation, "translationY", 0,iv_animation.getHeight());
//        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.setDuration(2000);
//        animatorSet.setInterpolator(new BounceInterpolator());
//        //兩個動畫一起播放
//        animatorSet.playTogether(animator, animator2);
//        //開始播放
//        animatorSet.start();

    }

    public void reset(View v) {
        iv_animation.clearAnimation();
    }

}
佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="testTweenAnimation"
        android:text="測試補間(檢視)動畫" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="testPropertyAnimation"
        android:text="測試屬性動畫" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="reset"
        android:text="重置補間動畫" />

    <ImageView
        android:id="@+id/iv_animation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/logo" />
</LinearLayout>
原始碼下載:
Myjstojava ---- PropertyAnimation