1. 程式人生 > >android屬性動畫與補間動畫的區別及用法

android屬性動畫與補間動畫的區別及用法

最近開始喜歡上了寫部落格,目的當然是記錄學過的內容,方便下一次來查詢,直接進入主題。最近在寫demo的時候發現了一個BUG,就是當我用補間動畫,也就是檢視動畫,去實現某個控制元件的動畫效果的時候,點選圖片沒有反應,這是為什麼呢?反覆查找了一些資料之後,終於找到了答案。android在3.0之後有了屬性動畫,這時我用屬性動畫代替了之前的補間動畫,接下來展示測試的一個小demo。
首先在activity_main.xml中

<?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>

在MainActivity中

package com.pbq.propertyanimation;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
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;
    private TextView tv_animation_msg;

    @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(getApplicationContext(), "點選了圖片", 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;
    /**
     * testPropertyAnimation
     * @param v
     */
    public void testPropertyAnimation(View v) {


        ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv_animation,"translationX",0,iv_animation.getWidth());
        ObjectAnimator animator4 = ObjectAnimator.ofFloat(iv_animation,"translationY",0,iv_animation.getHeight());

        AnimatorSet set = new AnimatorSet();
        set.playTogether(animator3,animator4);
        set.setDuration(2000);
        set.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();

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


    }

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

}

這裡具體是展示一張圖片平移的過程。使用補間動畫中平移動畫(TranslateAnimation),TranslateAnimation構造方法中,第一個引數表示開始時X軸的座標,第二個引數表示結束時X軸的座標,第三個引數表示開始時Y軸的座標,第四個表示結束時Y軸的座標。座標型別預設相對於控制元件本身。然後設定延遲和設定最終固定為平移完成時的狀態。最後開啟動畫。
使用屬性動畫來完成平移圖片過程中,使用ObjectAnimator類來實現,ObjectAnimator類提供了許多方法,有ofFloat,ofInt,ofObject等等,具體使用哪一個呢?從view的setTranslationX()中看出裡面需要float型別的引數,因此使用ofFloat這個方法。當中第一個引數表示需要去進行動畫效果的控制元件,即目標控制元件。第二個引數是一個String型別的引數,表示需要進行的是平移操作,即setTranslationX()中“translationX”表示向x軸平移。第三個引數表示開始時X軸的座標,第四個引數表示結束時X軸的座標。向y軸平移也是一樣的步驟。
下面來看效果圖:

這裡寫圖片描述

點選測試補間動畫,然後再點選圖片,此時沒有執行點選操作,圖片的點選操作還在原來圖片的位置。
這裡寫圖片描述

點選測試屬性動畫,然後點選圖片,此時能夠執行圖片的點選事件
這裡寫圖片描述