1.直接對View設(shè)置傳統(tǒng)動畫
傳統(tǒng)動畫包括幀動畫和補間動畫。幀動畫主要是一幀一幀的播放??梢栽趚ml中使用<animation-list>標(biāo)簽設(shè)置,也可以在代碼中使用AnimationDrawable設(shè)置;補間動畫主要包括alpha, translate, scale, rotate。
ScaleAnimation animation = new ScaleAnimation(0.0f, 1f, 0.0f, 1f, Animation.ABSOLUTE, 100, Animation.ABSOLUTE, 100);
animation.setDuration(100);
view.setAnimation(animation);
animation.start();
mWindowManager.addView(defaultSplashLayout, lp);
進行這樣的設(shè)置,view的動畫無法生效。原因是動畫執(zhí)行的條件是不能直接添加到最頂層的Window,而是需要一個容器。
如果添加一個容器,則只能對容器內(nèi)的view進行動畫設(shè)置,還是無法對容器進行動畫設(shè)置。
2.對WindowManager.LayoutParams的windowAnimations設(shè)置動畫
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
lp.flags = WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
lp.format = PixelFormat.RGB_888;
lp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
lp.windowAnimations = R.style.default_style;
<style name="default_style">
<item name="@android:windowEnterAnimation">@anim/window_enter</item>
<item name="@android:windowExitAnimation">@anim/window_exit</item>
</style>
這樣設(shè)置以后,這個view都會執(zhí)行動畫,但是動畫都是寫死在xml文件中的,無法進行動態(tài)設(shè)置
3.對View設(shè)置屬性動畫
屬性動畫對最頂層的view是可以執(zhí)行的。
view.setPivotX(100);
view.setPivotY(100);
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
valueAnimator.setDuration(200).start();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Float value = (Float) animation.getAnimatedValue();
view.setScaleX(value);
view.setScaleY(value);
}
});
mWindowManager.addView(defaultSplashLayout, lp)
至此,就可以對WindowManager的View設(shè)置任意的動畫了