Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 補間動畫入門:縮放、旋轉與淡入淡出一次學會

編輯:Android 開發實例

Android 上的動畫方案不少,其中最簡單也最廣泛使用的莫過於補間動畫(Tween Animation)。你只需要給定起始值、結束值、尺寸、時間長度、旋轉角度等參數,系統就會自動補出中間的過渡畫面,而且幾乎能套用在任何物件上。Android 為此提供了 Animation 類別。

載入與套用動畫

透過 AnimationUtils 的靜態方法 loadAnimation() 就能把 XML 定義的動畫讀成物件:

 Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);

注意第二個參數指的是動畫 XML 檔名。你必須在 res 目錄下建立一個名為 anim 的資料夾,並把動畫檔放在裡面。

Animation 類別常用的方法:

start():啟動動畫。
setDuration(long duration):設定動畫持續時間。
getDuration():取得前面設定的時間長度。
end():結束動畫。
cancel():取消動畫。

要讓動畫作用於某個物件,只要呼叫該物件的 startAnimation():

 ImageView image1 = (ImageView)findViewById(R.id.imageView1);
 image.startAnimation(animation);

縮放動畫的參數怎麼讀

在 res/anim 底下新增 XML 檔,寫入 scale 標籤:

 <set xmlns:android="https://schemas.android.com/apk/res/android">
 <scale xmlns:android="https://schemas.android.com/apk/res/android"
 android:fromXScale="0.5"
 android:toXScale="3.0"
 android:fromYScale="0.5"
 android:toYScale="3.0"
 android:duration="5000"
 android:pivotX="50%"
 android:pivotY="50%" >
 </scale>
 </set>

fromXScale 與 fromYScale 定義起始比例,toXScale 與 toYScale 定義結束比例;duration 是動畫總長(毫秒);pivotX 與 pivotY 則決定縮放的中心點,寫成百分比會相對該 View 自身的尺寸計算。

完整範例

這個範例做出一個選單,三個選項分別觸發縮放、旋轉與淡入淡出,並套用在同一張 ImageView 上。

準備工作包含:建立專案、在 MainActivity 加入動畫程式碼、配置 activity_main.xml、在 res 底下新增 anim 資料夾並建立 myanimation.xml、clockwise.xml、fade.xml 三個檔案、補上 string.xml 與 main.xml 的內容,最後執行驗收。

MainActivity.java

 package com.example.animation;
 import com.example.animation.R;
 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.animation.Animation;
 import android.view.animation.AnimationUtils;
 import android.widget.ImageView;

 public class MainActivity extends Activity {
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
 @Override public boolean onCreateOptionsMenu(Menu menu) {
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }
 public boolean onOptionsItemSelected(MenuItem item) {
 super.onOptionsItemSelected(item);
 switch(item.getItemId()) {
 case R.id.zoomInOut:
 ImageView image = (ImageView)findViewById(R.id.imageView1);
 Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);
 image.startAnimation(animation);
 return true;
 case R.id.rotate360:
 ImageView image1 = (ImageView)findViewById(R.id.imageView1);
 Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.clockwise);
 image1.startAnimation(animation1);
 return true;
 case R.id.fadeInOut:
 ImageView image2 = (ImageView)findViewById(R.id.imageView1);
 Animation animation2 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
 image2.startAnimation(animation2);
 return true;
 }
 return false;
 }
 }

版面與資源檔

主版面只需要一個置中的 ImageView:

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
 xmlns:tools="https://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="top"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context=".MainActivity" >
 <ImageView android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="179dp"
 android:src="@drawable/ic_launcher" />
 </RelativeLayout>

myanimation.xml 用 startOffset 把兩段縮放串成「放大再縮回」的循環:

 <?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="https://schemas.android.com/apk/res/android">
 <scale xmlns:android="https://schemas.android.com/apk/res/android"
 android:fromXScale="0.5"
 android:toXScale="3.0"
 android:fromYScale="0.5"
 android:toYScale="3.0"
 android:duration="5000"
 android:pivotX="50%"
 android:pivotY="50%" >
 </scale>
 <scale xmlns:android="https://schemas.android.com/apk/res/android"
 android:startOffset="5000"
 android:fromXScale="3.0"
 android:toXScale="0.5"
 android:fromYScale="3.0"
 android:toYScale="0.5"
 android:duration="5000"
 android:pivotX="50%"
 android:pivotY="50%" >
 </scale>
 </set>

clockwise.xml 同樣用兩段 rotate 做出正轉再反轉:

 <?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="https://schemas.android.com/apk/res/android">
 <rotate xmlns:android="https://schemas.android.com/apk/res/android"
 android:fromDegrees="0"
 android:toDegrees="360"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="5000" >
 </rotate>
 <rotate xmlns:android="https://schemas.android.com/apk/res/android"
 android:startOffset="5000"
 android:fromDegrees="360"
 android:toDegrees="0"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="5000" >
 </rotate>
 </set>

fade.xml 用 alpha 標籤控制透明度,並掛上加速插值器:

 <?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="https://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator" >
 <alpha android:fromAlpha="0"
 android:toAlpha="1"
 android:duration="2000" >
 </alpha>
 <alpha android:startOffset="2000"
 android:fromAlpha="1"
 android:toAlpha="0"
 android:duration="2000" >
 </alpha>
 </set>

選單檔:

 <menu xmlns:android="https://schemas.android.com/apk/res/android" >
 <item android:id="@+id/rotate360"
 android:orderInCategory="100"
 android:showAsAction="never"
 android:title="@string/rotate_String"/>
 <item android:id="@+id/zoomInOut"
 android:orderInCategory="100"
 android:title="@string/zoom_In_Out"/>
 <item android:id="@+id/fadeInOut"
 android:orderInCategory="100"
 android:title="@string/fade_String"/>
 </menu>

字串資源:

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
 <string name="app_name">Animation</string>
 <string name="action_settings">Settings</string>
 <string name="hello_world">Hello world!</string>
 <string name="zoom_In_Out">Zoom In/Out</string>
 <string name="rotate_String">Clockwise/Anti Clockwise</string>
 <string name="fade_String">Fade In/Out</string>
 </resources>

清單檔:

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="https://schemas.android.com/apk/res/android"
 package="com.yiibai.animation"
 android:versionCode="1"
 android:versionName="1.0" >
 <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
 <application android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity android:name="com.yiibai.animation.MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>
 </manifest>

執行結果

選擇要執行的裝置:

App 啟動後從手機選單叫出選項:

選擇 Zoom In / Out:

選擇順時針旋轉:

選擇淡入淡出:

要提醒的是,若在模擬器上執行,動畫可能會看起來不太順;建議用實機測試才能感受到真正的流暢度。另外,把動畫定義寫在 XML 而非程式碼裡有個實務好處:設計模糊的調整不需要重新編譯,改 duration 或 pivot 就能立刻看到差異,也更容易交給設計師參與。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved