安卓動畫知識總結 Animation AnimationSet LayoutAnimation

來源:互聯網
上載者:User

安卓動畫知識總結 Animation AnimationSet LayoutAnimation
本文由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" >



聯繫我們

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