Understanding Canvas and Surface concepts

Here are some definitions:

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

Also please keep in mind that, as this implies, a SurfaceView is a pretty heavy-weight object. If you have multiple SurfaceViews in a particular UI, stop and think about whether this is really needed. If you have more than two, you almost certainly have too many.

时间: 2024-10-11 23:00:22

Understanding Canvas and Surface concepts的相关文章

Android利用canvas画各种图形

canvas通俗的说就是一张画布,我们可以使用画笔paint,在其上面画任意的图形. 原理: 可以把canvas视为Surface的替身或者接口,图形便是绘制在Surface上的.Canvas封装了所有的绘制调用.通过Canvas, 绘制到Surface上的内容首先存储到一个内存区域(也就是对应的Bitmapz中),该Bitmap最终会呈现到窗口上. 使用: 1.Canvas可以直接new Canvas(): 2.在View中重写OnDraw()方法,里面有一个Canvas,今天讨论的内容. 方

Canvas标签初探

学了一点基础知识,感觉好神奇,全部练习代码 <html> <head> <meta http-equiv=Content-Type content="text/html;charset=utf-8"> <title>Canvas element size: 600 x 300, Canvas drawing surface size: 600 x 300</title> <style> body { backgro

「Android」 Surface分析

本篇针对Surface模块进行分析,从Java层的Activity创建开始,到ViewRoot.WindowsManagerService,再到JNI层和Native层. 首先推荐一个Android源码查看的网站:http://androidxref.com/ Surface的创建涉及三个部分: App 进程App需要将自己的内容显示在屏幕上,所以App负责发起Surface创建请求,创建好Surface后, 就可以直接可以在canvas上画图等,最终都会保存到Surface里的buffer里,

PhantomJS

PhantomJS 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 目录 概述 REPL环境 webpage模块 open() evaluate() includeJs() render() viewportSize,zoomFactor onResourceRequested onResourceReceived system模块 应用 过滤资源 截图 抓取图片 生成网页 参考链接 概述 有时,我们需要浏览器处理网页,但并不需要浏览,比如生成网页的截图.抓取网页数据等

Windows Kernel Security Training Courses

http://www.codemachine.com/courses.html#kerdbg Windows Kernel Internals for Security Researchers This course takes a deep dive into the internals of the Windows kernel from a security perspective. Attendees learn about behind the scenes working of va

微软职位内部推荐-SENIOR DEVELOPMENT LEAD

微软近期Open的职位: Job posting title: MSIT Dynamics CRM Sr. Dev Lead (Sr. Dev Lead) Location: China, Beijing Are you interested in shaping the future vision of how we implement Dynamics CRM across various businesses scenarios and evolve the platform strate

Wikipedia : OIT history

http://en.wikipedia.org/wiki/Order-independent_transparency Order-independent transparency From Wikipedia, the free encyclopedia The importance of blending order. The top produces an incorrect result with unordered alpha blending, while the bottom co

SurfaceView源码以及崩溃剖析

1 在eclipse中查看Android源代码 假设我们想参看Activity类的源代码,按着Ctrl键,左击它,现实的结果却看不到代码的,提示的信息便是"找不到Activity.class文件".下载好Android源码之后,点击Attached Source,选择External location External Folder文件夹菜单,选择Android所在的目录即可 http://jingyan.baidu.com/article/5d368d1e01df803f60c057

Erget 显示对象

核心显示类: 类 描述 DisplayObject 显示对象基类,所有显示对象均继承自此类 Bitmap 位图,用来显示图片 Shape 用来显示矢量图,可以使用其中的方法绘制矢量图形 TextField 文本类 BitmapText 位图文本类 DisplayObjectContainer 显示对象容器接口,所有显示对象容器均实现此接口 Sprite:DisplayObjectContainer 带有矢量绘制功能的显示容器 Stage:DisplayObjectContainer 舞台类 根节