Android遊戲開發入門:SurfaceView+SurfaceHolder+Thread

一、框架核心(SurfaceView+SurfaceHolder+Thread)
1、SurfaceView的API介紹:
Provides a dedicated drawing surface embedded inside of a view hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen
The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes.
Access to the underlying surface is provided via the SurfaceHolder interface, which can be retrieved by calling getHolder().
The Surface will be created for you while the SurfaceView’s window is visible; you should implement surfaceCreated(SurfaceHolder) and surfaceDestroyed(SurfaceHolder) to discover when the Surface is created and destroyed as the window is shown and hidden.
One of the purposes of this class is to provide a surface in which a secondary thread can render in to the screen. If you are going to use it this way, you need to be aware of some threading semantics:
•All SurfaceView and SurfaceHolder.Callback methods will be called from the thread running the SurfaceView’s window (typically the main thread of the application). They thus need to correctly synchronize with any state that is also touched by the drawing thread.
•You must ensure that the drawing thread only touches the underlying Surface while it is valid – between SurfaceHolder.Callback.surfaceCreated() and SurfaceHolder.Callback.surfaceDestroyed().

對應的中文翻譯:
SurfaceView是視圖(View)的繼承類,這個視圖裏內嵌了一個專門用於繪製的Surface。你可以控制這個Surface的格式和尺寸。Surfaceview控制這個Surface的繪製位置。
surface是縱深排序(Z-ordered)的,這表明它總在自己所在窗口的後面。surfaceview提供了一個可見區域,只有在這個可見區域內 的surface部分內容纔可見,可見區域外的部分不可見。surface的排版顯示受到視圖層級關係的影響,它的兄弟視圖結點會在頂端顯示。這意味者 surface的內容會被它的兄弟視圖遮擋,這一特性可以用來放置遮蓋物(overlays)(例如,文本和按鈕等控件)。注意,如果surface上面 有透明控件,那麼它的每次變化都會引起框架重新計算它和頂層控件的透明效果,這會影響性能。
你可以通過SurfaceHolder接口訪問這個surface,getHolder()方法可以得到這個接口。
surfaceview變得可見時,surface被創建;surfaceview隱藏前,surface被銷燬。這樣能節省資源。如果你要查看 surface被創建和銷燬的時機,可以重載surfaceCreated(SurfaceHolder)和surfaceDestroyed(SurfaceHolder)。
surfaceview的核心在於提供了兩個線程:UI線程和渲染線程。這裏應注意:
1> 所有SurfaceView和SurfaceHolder.Callback的方法都應該在UI線程裏調用,一般來說就是應用程序主線程。渲染線程所要訪問的各種變量應該作同步處理。
2> 由於surface可能被銷燬,它只在SurfaceHolder.Callback.surfaceCreated()和 SurfaceHolder.Callback.surfaceDestroyed()之間有效,所以要確保渲染線程訪問的是合法有效的surface。

2、核心框架
SurfaceView:

SurfaceView是View的子類,等同於TextView、ImageView等一系列控件。
核心功能可以通過子線程進行界面的繪製.
繪製需要注意的內容:
所有SurfaceView和SurfaceHolder.Callback的方法都應該在UI線程裏調
用,一般來說就是應用程序主線程。渲染線程所要訪問的各種變量應該作同步處
理。
由於surface可能被銷燬,它只在SurfaceHolder.Callback.surfaceCreated()和 SurfaceHolder.Callback.surfaceDestroyed()之間有效,
所以要確保渲染線程訪問的是合法有效的surface

SurfaceHolder

對象獲取:SurfaceView. getHolder();
用於管理Surface,核心的內容是Callback接口,我們需要依據繪圖中提到的注意內容確保Callback中的方法是在UI線程中調用。
Callback的調用:使用SurfaceHolder.addCallback(Callback callback)方法進行調用

Thread 工作

 需要滿足兩個條件 :在surfaceCreated之後創建, 在surfaceDestroyed 銷燬

3、繪製界面


 1. 在子線程(Thread)中繪製界面
 2. 繪製的週期:

    SurfaceHolder.Callback.surfaceCreated()—— SurfaceHolder.Callback.surfaceDestroyed() 

 3. 一個週期的繪製步驟:
    SurfaceHolder鎖定界面:SurfaceHolder.lockCanvas();
    圖像繪製(繪製一個矩形,說明當前的座標系統)
    SurfaceHolder解除界面鎖定:SurfaceHolder.unlockCanvasAndPost(Canvas canvas);
    繪製內容的量決定着繪製時間,在一個時間段內繪製的次數越多那麼用戶的體驗越好。
發佈了20 篇原創文章 · 獲贊 6 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章