了解画布和表面概念

发布于 2024-10-10 03:42:59 字数 896 浏览 4 评论 0原文

我正在努力理解绘制到 SurfaceView 的过程,以及整个 Surface/Canvas/Bitmap 系统,在 Android 中使用。

我已经阅读了所有文章和 API 文档页面,我可以在 android-developers 网站上找到它们,一些 android 图形教程,LunarLander 源代码和 这个问题

请告诉我,这些陈述哪些是正确的,哪些不是,以及为什么。

  1. Canvas 附加有自己的BitmapSurface 附加有自己的 Canvas
  2. 窗口的所有View共享相同的Surface,从而共享相同的Canvas
  3. SurfaceViewView 的子类,与其他 View 的子类和 View 本身不同,它有自己的 < code>Surface 来绘制。

还有一个额外的问题:

  • 如果已经有一个用于高画质的 Canvas 类,为什么还需要一个 Surface 类?位图级操作。举例说明 Canvas 不适合执行 Surface 可以执行的工作的情况。

I'm struggling to understand the process of drawing to SurfaceView and therefore the whole Surface/Canvas/Bitmap system, which is used in Android.

I've read all articles and API documentation pages, which I was able to find on android-developers site, a few tutorials of android graphics, LunarLander source code and this question.

Please tell me, which of these statements are true, which are not, and why.

  1. Canvas has its own Bitmap attached to it. Surface has its own Canvas attached to it.
  2. All View's of window share the same Surface and thus share the same Canvas.
  3. SurfaceView is subclass of View, which, unlike other View's subclasses and View itself, has its own Surface to draw in.

There is also one additional question:

  • Why is there a need for a Surface class, if there is already a Canvas for high-level operations with bitmap. Give an example of a situation where Canvas is non-suitable for doing work which Surface can do.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-10-17 03:42:59

以下是一些定义:

  • 表面是一个包含正在合成到屏幕上的像素的对象。您在屏幕上看到的每个窗口(对话框、全屏活动、状态栏)都有其自己的绘制表面,SurfaceFlinger 将它们按照正确的 Z 顺序渲染到最终显示。一个表面通常有多个缓冲区(通常是两个)来进行双缓冲渲染:当 Surface Flinger 使用最后一个缓冲区合成屏幕时,应用程序可以绘制其下一个 UI 状态,而无需等待应用程序完成绘图。

  • 窗口基本上就像您想象的桌面上的窗口一样。它有一个单独的 Surface,在其中呈现窗口的内容。应用程序与窗口管理器交互以创建窗口;窗口管理器为每个窗口创建一个 Surface 并将其提供给应用程序进行绘图。应用程序可以在Surface上绘制任何它想要的东西;对于窗口管理器来说,它只是一个不透明的矩形。

  • 视图是窗口内的交互式 UI 元素。窗口有一个附加的视图层次结构,它提供了窗口的所有行为。每当需要重新绘制窗口时(例如因为视图本身已失效),都会在窗口的 Surface 中完成此操作。 Surface 被锁定,这会返回一个可用于在其中进行绘制的 Canvas。绘制遍历是沿着层次结构向下完成的,将 Canvas 向下传递给每个视图以绘制其 UI 部分。完成后,Surface 将解锁并发布,以便将刚刚绘制的缓冲区交换到前台,然后由 Surface Flinger 合成到屏幕。

  • SurfaceView 是 View 的一种特殊实现,它还创建自己的专用 Surface 供应用程序直接绘制(在正常视图层次结构之外,否则必须为窗口共享单个 Surface)。它的工作方式比您想象的要简单——SurfaceView 所做的就是要求窗口管理器创建一个新窗口,告诉它对该窗口进行 Z 顺序排列,紧邻 SurfaceView 窗口的后面或前面,并将其定位以匹配其中 SurfaceView 出现在包含窗口中。如果表面放置在主窗口后面(按 Z 顺序),SurfaceView 还会用透明度填充主窗口的一部分,以便可以看到表面。

  • 位图只是一些像素数据的接口。当您直接创建位图时,像素可能由位图本身分配,或者它可能指向它不拥有的像素,例如内部发生的将 Canvas 连接到 Surface 进行绘图的情况。 (创建一个 Bitmap 并指向 Surface 的当前绘图缓冲区。)

还请记住,正如这所暗示的那样,SurfaceView 是一个相当重量级的对象。如果您在特定 UI 中有多个 SurfaceView,请停下来思考这是否真的需要。如果您有两个以上,则几乎可以肯定您的数量太多了。

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-17 03:42:59

Window、Surface、 Canvas 和 Bitmap

这是关于 Window、Surface、Canvas 和 Bitmap 之间如何进行交互的非常基本且简单的概念性概述。
有时,视觉表示有助于理解扭曲的概念。
我希望这张图可以帮助别人。

A conceptual overview of Window, Surface, Canvas, and Bitmap

Here is a very basic and simple conceptual overview of how interaction happens among the Window, Surface, Canvas, and Bitmap.
Sometimes, a visual representation helps a lot in understanding twisted concepts.
I hope this graphic could help someone.

半﹌身腐败 2024-10-17 03:42:59

位图只是像素集合的包装。将其视为具有一些其他方便功能的像素数组。

Canvas 只是包含所有绘图方法的类。如果您熟悉的话,它类似于 AWT/Swing 中的 Graphics 类。关于如何绘制圆形或盒子等的所有逻辑都包含在 Canvas 中。画布在位图或开放式 GL 容器上绘制,但将来没有理由可以将其扩展为在其他类型的栅格上绘制。

SurfaceView是包含Surface的View。表面类似于位图(它具有像素存储)。我不知道它是如何实现的,但我想它是某种位图包装器,具有与屏幕显示直接相关的额外方法(这就是表面的原因,位图太通用了)。您可以从 Surface 获取 Canvas,这实际上是获取与底层位图关联的 Canvas。

你的问题。

1.Canvas 附有自己的位图。 Surface 附有自己的画布。

是的,画布在位图(或打开的 GL 面板)上运行。 Surface 为您提供了一个 Canvas,它可以在任何 Surface 用于其位图样式像素存储的地方上运行。

2.窗口的所有View共享同一个Surface,从而共享同一个Canvas。

不会。您可以拥有任意数量的表面视图。

3.SurfaceView是View的子类,与其他View的子类和View本身不同,它有自己的Surface来绘制。

是的。就像ListView是View的子类一样,有自己的List数据结构。 View 的每个子类都会做不同的事情。

A Bitmap is simply a wrapper for a collection of pixels. Think of it as an array of pixels with some other convenient functions.

The Canvas is simply the class that contains all the drawing methods. It is similar to the Graphics class in AWT/Swing if you are familiar with that. All the logic on how to draw a circle, or a box, etc is contained inside Canvas. A canvas draws on a Bitmap or an open GL container but there is no reason why in the future it could be extended to draw onto other types of rasters.

SurfaceView is a View that contains a Surface. A surface is similar to a bitmap (it has a pixel store). I do not know how it is implemented but I'd imagine it is a some kind of Bitmap wrapper with extra methods for things that are directly related to screen displays (That is the reason for a surface, a Bitmap is too generic). You can get a Canvas from your Surface which is really getting the Canvas associated with the underlying Bitmap.

Your questions.

1.Canvas has its own Bitmap attached to it. Surface has its own Canvas attached to it.

Yes, a canvas operates on a Bitmap (or an open GL panel). Surface gives you a Canvas that is operating on whatever Surface is using for its Bitmap style pixel store.

2.All View's of window share the same Surface and thus share the same Canvas.

No. You could have as many surface views as you want.

3.SurfaceView is subclass of View, which, unlike other View's subclasses and View itself, has its own Surface to draw in.

Yes. Just like ListView is a subclass of View that has its own List data structure. Each subclass of View does something different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文