Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 安卓動畫知識總結 Animation AnimationSet LayoutAnimation

安卓動畫知識總結 Animation AnimationSet LayoutAnimation

編輯:關於Android編程

本文由PurpleSword(jzj1993)原創,轉載請注明 原文網址 http://blog.csdn.net/jzj1993
常見動畫有幾種
控件View的動畫(如Dialog窗口中的圓形進度條) 空間Window的動畫(如DialogWindow,PopupWindow的動畫,Activity切換時整個Window頁面的動畫) ViewGroup的LayoutAnimation動畫,每個子控件按照設定的順序、延遲播放動畫
動畫常用anim/*.xml定義

xml中定義動畫,可直接使用標簽直接定義,也可以放在標簽中,裡面定義的動畫將同時開始播放。 兩者都可使用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: These properties, when set on an AnimationSet object, will be pushed down to all child animations.repeatCount, fillEnabled: These properties are ignored for AnimationSet.startOffset, shareInterpolator: These properties apply to the AnimationSet itself.
    Starting with android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH, the behavior of these properties is the same in XML resources and at runtime (prior to that release, the values set in XML were ignored for AnimationSet). That is, calling setDuration(500) on an AnimationSet has the same effect as declaring android:duration="500" in an XML resource for an AnimationSet object.


    常規補間動畫:彈跳(移動)
    Layout/activity_welcome_anim.xml
    (0%p表示占父組件尺寸的百分比)

    "1.0" encoding="utf-8"?> "http://schemas.android.com/apk/res/android" > "500" android:fromXDelta="0" android:fromYDelta="0%p" android:interpolator="@android:anim/accelerate_interpolator" android:toXDelta="0" android:toYDelta="42%p" /> "350" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/decelerate_interpolator" android:startOffset="500" android:toXDelta="0" android:toYDelta="-21%p" /> "350" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="850" android:toXDelta="0" android:toYDelta="21%p" /> "250" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/decelerate_interpolator" android:startOffset="1200" android:toXDelta="0" android:toYDelta="-10%p" /> "250" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="1450" android:toXDelta="0" android:toYDelta="10%p" /> "150" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/decelerate_interpolator" android:startOffset="1700" android:toXDelta="0" android:toYDelta="-5%p" /> "150" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="1850" android:toXDelta="0" android:toYDelta="5%p" />

    再例如常規補間動畫:縮放、透明度
    "1.0" encoding="utf-8"?> "http://schemas.android.com/apk/res/android" > "800" android:fromXScale="0.0" android:fromYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.0" android:toYScale="1.0" /> "800" android:fromAlpha="0.0" android:toAlpha="1.0" />
    再如上浮效果(移動、透明度)
    "1.0" encoding="utf-8"?> "http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" > "500" android:fromXDelta="0" android:fromYDelta="5%" android:toXDelta="0" android:toYDelta="0%" /> "500" android:fromAlpha="0.0" android:toAlpha="1.0" /> "100" android:fromAlpha="1.0" android:startOffset="1400" android:toAlpha="1.0" />
    可使用Java程序加載

    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 "@style/app_theme" > ".ui.ActivityWelcome" 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動畫
    "http://schemas.android.com/apk/res/android" android:animation="@anim/layout_anim_item" android:animationOrder="normal" android:delay="0.25" />

    3、在ViewGroup中引用自定義Layout動畫 "@anim/layout_anim" >



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved