Understanding Canvas and Surface concepts

后端 未结 3 1448
难免孤独
难免孤独 2020-11-28 17:17

I\'m struggling to understand the process of drawing to SurfaceView and therefore the whole Surface/Canvas/Bitmap system,

3条回答
  •  再見小時候
    2020-11-28 17:49

    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.

提交回复
热议问题