关于Surface,Window,View,SurfaceView,Bitmap的理解

原文来自于StackOverFlow上的一个问题,问题地址:http://stackoverflow.com/questions/4576909/understanding-canvas-and-surface-concepts

通过该问题Vote最多的回答内容,个人觉得可以对这几个概念有一个形象一些的理解,首次尝试翻译可能有些误差~~

  • 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.

       Surface对象用来保存将要合成到屏幕上的像素点,在屏幕上你能看到的每一个窗口(如一个对话框,全屏的Activity,状态栏)都有一个属于它自己的surface用于绘制,并且SurfaceFlinger会按照它们的Z轴顺序将其渲染出来。一个Surface通常会有多于一个的缓冲区(通常是两个)来做双缓冲渲染:SurfaceFlinger会用前一个缓冲区的数据进行屏幕画面合成,与此同时应用程序可以绘制下一个UI界面将数据放入另外一个缓冲区,并不需要等待应用程序前一个缓冲区数据全部绘制完成。

        

       源代码中Surface.java中对其自身的注释:

       "Handle onto a raw buffer that is being managed by the screen compositor."

        用来处理画面混合器所管理的原始缓冲区


  • 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.

       Window基本上就是如你所想的桌面窗口。Window会有一个surface用于渲染窗口中的内容。应用程序通过Window Manager来创建窗口;Window Manager会为每一个窗口创建一个surface,并将其交给应用程序进行绘制。应用程序可以在surface上绘制任何其想要绘制的东西;而对于Window Manager来说窗口仅仅是一个透明的矩形。


      源代码Window.java中对其的注释:

     * Abstract base class for a top-level window look and behavior policy.  An

     * instance of this class should be used as the top-level view added to the
     * window manager. It provides standard UI policies such as a background, title
     * area, default key processing, etc.
     *
     * The only existing implementation of this abstract class is
     * android.policy.PhoneWindow, which you should instantiate when needing a
     * Window. 

     顶层窗口外观以及行为策略的抽象基类。Window类的实例应该以顶层视图的身份加入到窗口管理器中。它提供了标准的UI策略例如背景,标题区域,默认按键处理等。

     仅存的实现该抽象类的是当你需要一个窗口是都需要实例化的PhoneWindow。


  • 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.

       View是窗口中可交互的UI元素。一个窗口仅有一个视图层级(View Tree)挂载,其会负责提供窗口上的所有交互行为的响应。每当窗口需要被重绘(比如视图被其自己标记为无效,也就调用invalidate相关方法),这些绘制工作会在surface中完成。当surface被锁住之后,会返回一个canvas用于绘制内容到该surface中。一次遍历绘制的完成会沿着视图层级进行,传递canvas沿着每一view去绘制其UI部分,一旦完成,surface就会解锁并且发布,这样刚绘制的缓冲区就会被将换到前台可以被SurfaceFlinger合成显示到屏幕上。


  • A SurfaceView is a special implementation of View that also creates its own dedicated Surface for the application to directly draw into (outside of the normal view hierarchy, which otherwise must share the single Surface for the window). The way this works is simpler than you may expect -- all SurfaceView does is ask the window manager to create a new window, telling it to Z-order that window either immediately behind or in front of the SurfaceView's window, and positioning it to match where the SurfaceView appears in the containing window. If the surface is being placed behind the main window (in Z order), SurfaceView also fills its part of the main window with transparency so that the surface can be seen.

      SurfaceView是View的一个特殊的实现,它也需要创建其自己专用的surface用于应用程序直接绘制(在普通视图层级意外,通用需要将仅有的surface共享到窗口)。这种方式运作比你期望的要简单---所有的SurfaceView均会要求窗口管理器为其创建按一个新的窗口,会告知窗口管理器新创建的这个窗口是应该在surfaceview所在窗口Z轴方向的上面或者下面,并且将该窗口定位在surfaceview所在窗口的位置。如果该surface被放在了主窗口Z轴防线的下面,SurfaceView会将其在主窗口上的部分设置为透明以便新创建的surface可以被看到。


  • A Bitmap is just an interface to some pixel data. The pixels may be allocated by Bitmap itself when you are directly creating one, or it may be pointing to pixels it doesn't own such as what internally happens to hook a Canvas up to a Surface for drawing. (A Bitmap is created and pointed to the current drawing buffer of the Surface.)

       Bitmap仅是一个承载部分像素点的界面。当你直接创建一个Bitmap时其可能自己分配一些像素点,或者可能指向并不属于其自己的像素点例如将一个canvas挂载到surface上去绘制。(Bitmap被创建之后指向suface的当前绘制缓冲区)


 最后说说个人的理解,当启动一个Activity的时候在startActivity的后期会给当前启动的Activity添加一个window,而如上翻译所述WindowManager在创建window的同时会为其创建一个surface用来绘制界面用,Activity启动之后对应的Window对象其实是PhoneWindow,PhoneWindow中创建了当前窗口中的根view---DecorView,之后此window对应的View tree就挂在这个DecorView上,窗口上所有的View会通过Canvas调用Hwui相关的接口将其绘制到屏幕上,而用户交互的事件由ViewRoot一层一层的传递到顶层View进行处理。另外再说一句关于Canvas,Bitmap以及Surface的理解:Bitmap以及surface均用来进行pixels的存储,而canvas则是包含了各种绘制接口的类。







  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值