Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

安卓動畫知識大整理:Animation、AnimationSet 與 LayoutAnimation

編輯:關於 Android 程式設計

本文由 PurpleSword(jzj1993) 原創,轉載請註明原文網址 https://blog.csdn.net/jzj1993

常見的動畫大致可分為幾類

控制項 View 的動畫(例如 Dialog 視窗中的圓形進度條);

視窗 Window 的動畫(例如 DialogWindow、PopupWindow 的動畫,以及 Activity 切換時整個 Window 頁面的動畫);

ViewGroup 的 LayoutAnimation 動畫,讓每個子控制項按照設定的順序與延遲依次播放動畫。

動畫通常在 anim/*.xml 中定義

在 XML 中定義動畫時,可以直接用 <scale>、<rotate> 這類標籤單獨定義,也可以把它們放進 <set> 標籤裡,此時標籤內定義的動畫會同時開始播放。兩者都能透過 AnimationUtils.loadAnimation 方法來載入。如果是用 set 標籤定義的,載入時回傳的會是一個 AnimationSet 實例(AnimationSet 繼承自 Animation)。

要特別注意的是,在 set 標籤上設定的一些屬性,會直接覆蓋它裡面各動畫的對應屬性;而 AnimationSet 從 Animation 繼承而來的另外一些屬性則會失效。下面是 AnimationSet 類別的官方說明:

Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values.

The way that AnimationSet inherits behavior from Animation is important to understand. Some of the Animation attributes applied to AnimationSet affect the AnimationSet itself, some are pushed down to the children, and some are ignored, as follows:

  • duration、repeatMode、fillBefore、fillAfter:在 AnimationSet 上設定這些屬性時,會被下推到所有子動畫。
  • repeatCount、fillEnabled:這兩個屬性對 AnimationSet 會被忽略。
  • startOffset、shareInterpolator:這兩個屬性只作用於 AnimationSet 本身。

從 android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH 開始,這些屬性在 XML 資源與執行期的行為才保持一致(在此之前,於 XML 中設定的值會對 AnimationSet 失效)。也就是說,對一個 AnimationSet 呼叫 setDuration(500),其效果等同於在 XML 資源中為該 AnimationSet 宣告 android:duration="500"。

常規補間動畫:彈跳(移動)

Layout/activity_welcome_anim.xml(0%p 表示佔父組件尺寸的百分比)



    
    
    
    
    
    
    

再看幾個例子。

常規補間動畫:縮放、透明度



    
    

再如上浮效果(移動、透明度)



    
    
    

這些動畫都能透過程式來載入:

this.setContentView(R.layout.activity_welcome); anim = AnimationUtils.loadAnimation(this, R.anim.welcome_anim); // 動畫播放完畢後,讓 View 物件停留在終止的位置 anim.setFillEnabled(true); anim.setFillAfter(true);

this.findViewById(R.id.point).setAnimation(anim);

此外還可以設定動畫監聽器

anim.setAnimationListener(listener); /** * 動畫監聽器 */ private AnimationListener listener = new AnimationListener() { @Override public void onAnimationEnd(Animation animation) { // startMain(); } @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } };

在 Dialog 中載入動畫

LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.dialog_voice, null); ImageView img = (ImageView) v.findViewById(R.id.dialog_img); Animation anim = AnimationUtils.loadAnimation(context, R.anim.center_rotate_repeat); img.startAnimation(anim); new AlertDialog.Builder(context).setView(v);

為整個 Dialog 設定動畫

dialog.getWindow().setWindowAnimations(R.style.quick_scale_anim);

為 PopupWindow 設定動畫

pop.setAnimationStyle(R.style.quick_scale_anim);

Activity 的切換動畫

用程式碼實作 Activity 切換動畫

startActivity(new Intent(this, ListAlarm.class)); overridePendingTransition(R.anim.anim_activity_in, R.anim.anim_activity_unchange);

或者:

ActivityGuide.this.finish(); overridePendingTransition(R.anim.alpha_stay, R.anim.alpha_exit);

在 XML 中定義 Activity 切換動畫

提醒:Activity 切換動畫不能設定 SingleInstance 屬性,否則會導致動畫失效。

Manifest.xml 中為 Activity 套用主題:

android:theme="@style/app_theme_fullscreen"

style.xml:



對應的 anim/quick_alpha_enter.xml 與 anim/quick_alpha_stay.xml。

LayoutAnimation

1、先在 layout_anim_item.xml 中定義子 View 動畫;

2、再在 layout_anim.xml 中定義 Layout 動畫,並引用子 View 動畫:

3、最後在 ViewGroup 中引用自訂的 Layout 動畫:

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