Android遊戲開發學習筆記(一):tweened animation

來源:互聯網
上載者:User

android中的自訂動畫有兩種模式:tweened animation和frame by frame。這裡介紹一種通過xml實現tweened animation的方法。
tweened animation(漸層動畫),有四種動畫類型:alpha(透明度)、scale(尺寸伸縮)、translate(位置變換)和rotate(圖形旋轉)。
首先,建立一個叫Animation的項目,在res下建立一個anim的目錄,在目錄中建立一個動畫設定的xml檔案myanim.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:duration="2000"
    />         <!-- 透明度的變換 -->
    <!-- fromAlpha屬性為動畫起始時透明度,toAlpha屬性為動畫結束時的透明度, 
        duration為動畫持續的時間 -->
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="0.0"
        android:toXScale="1.4"
        android:fromYScale="0.0"
        android:toYScale="1.4"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="3000"
    />         <!-- 尺寸的變換 -->
    <!-- interpolator指定一個動畫的插入器,fromXScale屬性為動畫起始時x座標上的 
        伸縮尺寸,toCScale屬性為動畫結束時x座標上的伸縮尺寸,fromYScale屬性為動畫 
        起始時y座標上的伸縮尺寸,toYScale屬性為動畫結束時y座標上的伸縮尺寸,pivotX 
        和pivotY設定動畫相對於自身的位置,fillAfter表示動畫的轉換在動畫結束後是否 
        被應用 -->
    <translate
        android:fromXDelta="30"
        android:toXDelta="0"
        android:fromYDelta="30"
        android:toYDelta="50"
        android:duration="3000"
    />         <!-- 位置的變換 -->
    <!-- fromXDelta屬性為動畫起始時x座標上的位置,toXDelta屬性為動畫結束時x座標 
    上的位置,fromYDelta屬性為動畫起始時y座標上的位置,toYDelta屬性為動畫結束時y 
    座標上的位置 -->
    <rotate
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromDegrees="0"
        android:toDegrees="+350"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000"
    />         <!-- 旋轉變換 -->
    <!-- interpolator同樣為一個動畫的插入器,fromDegrees屬性為動畫起始時物件 
    的角度,toDegrees屬性為動畫結束時物件旋轉的角度 -->
</set>
然後編寫布局檔案main.xml,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView
    android:id="@+id/myImageView"   
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:src="@drawable/preview"
    />
</LinearLayout>
其中,preview是放入res/drawable-mdpi的一張圖片,我們以此圖片作為動畫示範。
最後,在MainActivity.java中編寫載入動畫的代碼:
package game.test; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.ImageView; 
 
public class MainActivity extends Activity { 
    Animation myAnimation; 
    ImageView myImageView; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        myAnimation=AnimationUtils.loadAnimation(this, R.anim.myanim); 
        myImageView=(ImageView)this.findViewById(R.id.myImageView); 
        myImageView.startAnimation(myAnimation); 
    } 
}
作者 “Android學習心得”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.