Tween動畫介紹

來源:互聯網
上載者:User

 Tween動畫又稱“補間動畫”、“中間動畫”,這並不重要,就好像很多人都知道魯迅,卻不知道他叫:周樹人。

   Tween動畫在Android中分為4類,它們分別是:AlphaAnimation(透明度動畫)、TranslateAnimation(平移動畫)、ScaleAnimation(縮放動畫)、RotateAnimation(旋轉動畫)。都繼承自android.view.Animation類,它們都是表示從一個狀態A向狀態B變化的一個過程,所以英文名字叫Tween動畫、中文名叫:“補間動畫”、“中間動畫”。它們總的說來有兩種實現方式:java code(java原始碼)、xml(xml設定檔),這裡先從java
code開始

   以前就是因為每中Tween動畫都有很多建構函式不清楚,現在仔細看了下,記錄下來方便以後查看

    AlphaAnimation(透明度動畫)

    AlphaAnimation有兩個建構函式,分別是:

                       —— AlphaAnimation(Context context, AttributeSet attrs):第二個參數是個屬性集,之後會詳細對AttributeSet 講解

                       ——AlphaAnimation(float fromAlpha, float toAlpha):第一個參數是初始透明度,第二個參數是終止透明度

    TranslateAnimation(平移動畫)

    TranslateAnimation有三個建構函式,分別是:

                       ——TranslateAnimation(Context context, AttributeSet attrs):略過

                       ——TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta):分別對應x軸的起始、終點                                          座標,與y軸的起始、終點座標

                      ——TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int                                 toYType, float toYValue):第一個參數是x軸方向的值的參照(Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF,
                                or Animation.RELATIVE_TO_PARENT);第二個參數是第一個參數類型的起始值;第三個參數與第四個參數是x軸方向的

                              終點參照與對應值;後面四個參數就不用解釋了。如果全部選擇Animation.ABSOLUTE,其實就是第二個建構函式。

                              以x軸為例介紹參照與對應值的關係

                              如果選擇參照為Animation.ABSOLUTE,那麼對應的值應該是具體的座標值,比如100到300,指絕對的螢幕像素單位

                              如果選擇參照為Animation.RELATIVE_TO_SELF或者 Animation.RELATIVE_TO_PARENT指的是相對於自身或父控制項,

                              對應值應該理解為相對於自身或者父控制項的幾倍或百分之多少。一定要多試試這幾個參數類型!

       ScaleAnimation(縮放動畫)

       ScaleAnimation(縮放動畫)有四個建構函式,分別是:

                       ——ScaleAnimation(Context context, AttributeSet attrs):略過

                       ——ScaleAnimation(float fromX, float toX, float fromY, float toY):同TranslateAnimation(float fromXDelta, float toXDelta,                                    float fromYDelta, float toYDelta)

                       ——ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY):這裡解釋後面兩個參數,pivot

                               英文意思為“樞軸”,也就是支點。通過這兩個參數可以控制縮放動畫的放大方向,這個點不會隨對象大小變化而變化

                      ——ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float                                       pivotYValue):如果理解了前面所講的,這個就不做多的說明,如果不清楚,請回頭多用代碼試試。

        RotateAnimation(旋轉動畫)

         RotateAnimation(旋轉動畫)同樣有四個建構函式,分別是:

                      ——RotateAnimation(Context context, AttributeSet attrs)

                     ——RotateAnimation(float fromDegrees, float toDegrees)

                     ——RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)

                     ——RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float                                                pivotYValue)

                    這裡不廢話了!

 

說了這麼多,直接上代碼。

這裡是Java原始碼

package com.tfsp.training.testtweenanimation;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.AlphaAnimation;

import android.view.animation.Animation;

import android.view.animation.RotateAnimation;

import android.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Spinner;

 

public class TestTweenAnimation extends Activity {

//定義開始按鈕

private Button start = null;

//定義動畫類型下拉式清單

private Spinner select = null;

//這張圖片是動畫執行者

private ImageView img = null;

//定義動畫

private Animation tAnimation = null;

//定義一個String數組用於構造下拉式清單的適配器

private String str[] = {

"平移動畫", "透明度動畫", "旋轉動畫", "縮放動畫"

};

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //分別從xml檔案中得到每個控制項

        start = (Button) findViewById(R.id.startButton);

        select = (Spinner) findViewById(R.id.select);

        img = (ImageView) findViewById(R.id.img);

        //執行個體化適配器

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str);

        select.setAdapter(adapter);

        //為開始按鈕設定監聽

        start.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

InitialAnimation();

img.startAnimation(tAnimation);

}

});

    }

    

    //初始化動畫

    public void InitialAnimation(){

 

    switch(select.getSelectedItemPosition()){

    case 0:

    tAnimation = new TranslateAnimation(0, 300, 50, 50);

//    tAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_PARENT, -0.5f, Animation.RELATIVE_TO_PARENT, -0.5f);

    break;

    case 1:tAnimation = new AlphaAnimation(0.1f, 1.0f);

    break;

    case 2:tAnimation = new RotateAnimation(0.0f, +360.0f);

    break;

    case 3:

//    tAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);

    tAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, 200.0f, 0.0f);

    break;

    }

    //為動畫設定完成所需時間

    tAnimation.setDuration(2000);

    }

}

這裡是main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <Spinner

    android:id="@+id/select"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    />

    <Button

    android:id="@+id/startButton"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_below="@id/select"

    android:text="開始播放"

    />

    <ImageView

    android:id="@+id/img"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignParentBottom="true"

    android:src="@drawable/sun"

    />

</RelativeLayout>

 

源碼:http://www.rayfile.com/zh-cn/files/62056542-5b72-11e0-9308-0015c55db73d/

 

轉至:http://hi.baidu.com/soodroid/blog/item/8f7e661e8d69f4144b90a7e8.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.