序
如何创建一个窗口动画?我们通过先从APP创建一个窗口,以这个窗口的创建过程的窗口动画为例
这个demo就是点击BUTTON显示窗口,点击CLOSE WINDOW关闭窗口,下面简述关键代码
//定义WindowManager和LayoutParams
private WindowManager mWindowManager;
private WindowManager.LayoutParams mLayoutParams;
//取得系统窗体
mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
//窗体的布局样式
mLayoutParams = new WindowManager.LayoutParams();
//窗口设置动画
mLayoutParams.windowAnimations = R.style.MyWindow;
//设置窗口名字
mLayoutParams.setTitle("test-window");
在res/values/styles.xml目录下添加styles
<style name="MyWindow">
<item name="android:windowEnterAnimation">@anim/enter</item>
<item name="android:windowExitAnimation">@anim/exit</item>
</style>
创建res/anim/enter.xml和res/anim/exit.xml
enter.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0"
android:toAlpha="1.0"
android:duration="1000"/>
</set>
设置透明度从0到1
exit.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0"
android:toAlpha="0"
android:duration="1000"/>
</set>
设置透明度从1到0
这两个xml都只是简单的做了一个透明度变化,实现了一个淡入淡出的效果
分析思路
我们如何知道上面的demo所涉及的动画在我们framework侧的哪个部分?
图层
根据上面的demo我们看看在图层上是怎么显示的
我们点击BUTTON后显示窗口,从图层中可以看到WindowToken和我们创建的窗口test-window中间添加了一个动画Surface(name=2257b8c test-window)/@0x825faea - animation-leash of window_animation#529
。同时我们也能看到test-window的color : r:-1.000 g:-1.000 b:-1.000 a:0.193603515625
,其中a:0.193603515625表示透明度,透明度从0逐渐到1就是从透明到显示的过程。
透明度为1时动画退出,窗口完全显示;窗口的移除流程同理,唯一不同的就是透明度从1到0,透明度为0时动画移除,窗口完全退出。
也就是说动画的显示过程就是为其显示的窗口和这个该窗口的父窗口之前添加一个层级用于显示动画;动画播放完成后,再移除这个层级。
代码
从上面的例子来看,不论是窗口显示还是隐藏,都会有类似于Surface(name=2257b8c test-window)/@0x825faea - animation-leash of window_animation#529
的动画,那么我们就可以通过这点切入查找相应的代码,dump信息在代码中基本都有迹可循,搜索animation-leash
找到对应代码位置frameworks/base/services/core/java/com/android/server/wm/SurfaceAnimator.java我们可以在createAnimationLeash方法中添加堆栈来查看其调用流程
android.util.Log.i("WindowManager:","createAnimationLeash type = "+animationTypeToString(type),new Exception());
也可以使用走读代码的方式追踪
堆栈
window动画显示
3-16 22:32:26.512 563 584 I WindowManager: createAnimationLeash type = window_animation
03-16 22:32:26.512 563 584 I WindowManager: java.lang.Exception
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.SurfaceAnimator.createAnimationLeash(SurfaceAnimator.java:458)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.SurfaceAnimator.startAnimation(SurfaceAnimator.java:184)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.startAnimation(WindowContainer.java:2757)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.startAnimation(WindowContainer.java:2764)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.startAnimation(WindowContainer.java:2770)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowState.startAnimation(WindowState.java:5305)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowState.startAnimation(WindowState.java:5281)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowStateAnimator.applyAnimationLocked(WindowStateAnimator.java:655)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowStateAnimator.applyEnterAnimationLocked(WindowStateAnimator.java:583)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowState.performShowLocked(WindowState.java:4648)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowStateAnimator.commitFinishDrawingLocked(WindowStateAnimator.java:276)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.DisplayContent.lambda$new$8$com-android-server-wm-DisplayContent(DisplayContent.java:987)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.DisplayContent$$ExternalSyntheticLambda14.accept(Unknown Source:4)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer$ForAllWindowsConsumerWrapper.apply(WindowContainer.java:2629)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer$ForAllWindowsConsumerWrapper.apply(WindowContainer.java:2619)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowState.applyInOrderWithImeWindows(WindowState.java:4904)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowState.forAllWindows(WindowState.java:4748)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.forAllWindows(WindowContainer.java:1616)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.forAllWindows(WindowContainer.java:1616)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.forAllWindows(WindowContainer.java:1616)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.forAllWindows(WindowContainer.java:1616)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.forAllWindows(WindowContainer.java:1616)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.forAllWindows(WindowContainer.java:1616)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.forAllWindows(WindowContainer.java:1616)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowContainer.forAllWindows(WindowContainer.java:1633)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.DisplayContent.applySurfaceChangesTransaction(DisplayContent.java:4666)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.RootWindowContainer.applySurfaceChangesTransaction(RootWindowContainer.java:1021)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.RootWindowContainer.performSurfacePlacementNoTrace(RootWindowContainer.java:824)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.RootWindowContainer.performSurfacePlacement(RootWindowContainer.java:784)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowSurfacePlacer.performSurfacePlacementLoop(WindowSurfacePlacer.java:177)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowSurfacePlacer.performSurfacePlacement(WindowSurfacePlacer.java:126)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowSurfacePlacer.performSurfacePlacement(WindowSurfacePlacer.java:115)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.wm.WindowSurfacePlacer$Traverser.run(WindowSurfacePlacer.java:57)
03-16 22:32:26.512 563 584 I WindowManager: at android.os.Handler.handleCallback(Handler.java:942)
03-16 22:32:26.512 563 584 I WindowManager: at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 22:32:26.512 563 584 I WindowManager: at android.os.Looper.loopOnce(Looper.java:201)
03-16 22:32:26.512 563 584 I WindowManager: at android.os.Looper.loop(Looper.java:288)
03-16 22:32:26.512 563 584 I WindowManager: at android.os.HandlerThread.run(HandlerThread.java:67)
03-16 22:32:26.512 563 584 I WindowManager: at com.android.server.ServiceThread.run(ServiceThread.java:44)
window动画退出
03-16 22:32:30.315 563 919 I WindowManager: createAnimationLeash type = window_animation
03-16 22:32:30.315 563 919 I WindowManager: java.lang.Exception
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.SurfaceAnimator.createAnimationLeash(SurfaceAnimator.java:458)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.SurfaceAnimator.startAnimation(SurfaceAnimator.java:184)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.WindowContainer.startAnimation(WindowContainer.java:2757)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.WindowContainer.startAnimation(WindowContainer.java:2764)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.WindowContainer.startAnimation(WindowContainer.java:2770)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.WindowState.startAnimation(WindowState.java:5305)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.WindowState.startAnimation(WindowState.java:5281)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.WindowStateAnimator.applyAnimationLocked(WindowStateAnimator.java:655)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.WindowState.removeIfPossible(WindowState.java:2600)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.WindowState.removeIfPossible(WindowState.java:2498)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.WindowManagerService.removeWindow(WindowManagerService.java:2033)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.Session.remove(Session.java:223)
03-16 22:32:30.315 563 919 I WindowManager: at android.view.IWindowSession$Stub.onTransact(IWindowSession.java:684)
03-16 22:32:30.315 563 919 I WindowManager: at com.android.server.wm.Session.onTransact(Session.java:175)
03-16 22:32:30.315 563 919 I WindowManager: at android.os.Binder.execTransactInternal(Binder.java:1285)
03-16 22:32:30.315 563 919 I WindowManager: at android.os.Binder.execTransact(Binder.java:1244)