android - 什么是安卓窗口?

标签 android android-activity android-window

什么是Android中的Window

我以为Android中最顶层的叫做Activity,就是你看到的屏幕。

谁能告诉我 Android 中的 Window 是什么?我们只有一个还是多个。

最佳答案

[UPDATE](让我分享一下我在原始答案后对Window的了解)

一句话,A Window 是一个矩形区域,有一个查看层次结构。下图中的彩色矩形是窗口。

enter image description here

如您所见,一个屏幕中可以有多个窗口,WindowManager管理他们。当前屏幕的窗口列表可以通过Hierarchy Viewer获取, 或 adb shell dumpsys 窗口

层次结构查看器示例中的窗口列表: enter image description here

(以下为原答案)


我也有同样的问题,希望对大家有所帮助。

根据Android Developer Documentation ,

"Each activity is given a window in which to draw its user interface."

和,Dianne Hackborn是一名Android框架工程师,给出了一些定义here .她说,

A window is basically like you think of a window on the desktop. It has a single Surface in which the contents of the window is rendered. An application interacts with the Window Manager to create windows; the Window Manager creates a Surface for each window and gives it to the application for drawing. The application can draw whatever it wants in the Surface; to the Window Manager it is just an opaque rectangle.

A Surface is an object holding pixels that are being composited to the screen. Every window you see on the screen (a dialog, your full-screen activity, the status bar) has its own surface that it draws in to, and Surface Flinger renders these to the final display in their correct Z-order. A surface typically has more than one buffer (usually two) to do double-buffered rendering: the application can be drawing its next UI state while the surface flinger is compositing the screen using the last buffer, without needing to wait for the application to finish drawing.

A View is an interactive UI element inside of a window. A window has a single view hierarchy attached to it, which provides all of the behavior of the window. Whenever the window needs to be redrawn (such as because a view has invalidated itself), this is done into the window's Surface. The Surface is locked, which returns a Canvas that can be used to draw into it. A draw traversal is done down the hierarchy, handing the Canvas down for each view to draw its part of the UI. Once done, the Surface is unlocked and posted so that the just drawn buffer is swapped to the foreground to then be composited to the screen by Surface Flinger.

另外,我从 Romain Guy 找到了一些其他信息的演讲(您可以在 here 观看他在旧金山 Android 用户组的演讲,并从 here 下载完整幻灯片)

enter image description here

所以,简而言之:

  • Activity 有一个窗口(在其中绘制用户界面),
  • Window 有一个 Surface 和一个附加的 View 层次结构,
  • 一个 Surface 包括 ViewGroup 包含 View 。

关于android - 什么是安卓窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9451755/

相关文章:

Android Studio - Instant App - list 合并错误

android - 我应该用 Fragments 替换 Android Activity 吗?

android - 在 eclipse 中导入 javax.microedition

java - 有什么方法可以在全局范围内处理每个 Ion 请求吗?

java - 如何从assets子文件夹中读取PDF文件

java - 从 CardView 中的按钮初始化正确的类

android - 测试 Activity 识别转换 API

Android - 全屏而不移动内容

android - 在状态栏下方显示 DialogFragment 内容

java - 在运行时移除 FLAG_TRANSLUCENT_STATUS