人人IT網

人人IT網

當前位置: 主頁 > JAVA編程 > JAVA >

簡單說說JAVA層中Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之間的關系

時間:2012-08-03 19:29來源:Internet 作者:Internet 點擊:
1、Surface Surface extends Object implements Parcelable java.lang.Object   &n

1、Surface


Surface

extends Object
implements Parcelable
java.lang.Object
   ↳ android.view.Surface

Class Overview


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

簡單翻譯:

Surface是原始圖像緩沖區(raw buffer)的一個句柄,而原始圖像緩沖區是由屏幕圖像合成器(screen compositor)管理的。




1.1、 就如在C語言編程一样,通過一個文件的句柄,就可以操作文件,獲取文件的內容。 同样的,通過Surface就可以獲取raw buffer其中的內容。原生緩沖區(raw buffer)存儲着當前窗口的像素數據。


1.2、事實上,當得到一個Surface對象時,同時會得到一個Canvas(畫布)對象。這一點可以通過查看\frameworks\base\core\java\android\view\Surface.java文件可知道Surface類定義了一個Canvas成員變量


    //@\frameworks\base\core\java\android\view\Surface.java
    // The mSurfaceControl will only be present for Surfaces used by the window
    // server or system processes. When this class is parceled we defer to the
    // mSurfaceControl to do the parceling. Otherwise we parcel the
    // mNativeSurface.
    private int mSurfaceControl;
    private int mSaveCount;
    private Canvas mCanvas;
    private int mNativeSurface;
    private int mSurfaceGenerationId;
    private String mName;

1.3、 理解Canvas對象,可以把它當做畫布,Canvas的方法大多數是設置畫布的大小、形狀、畫布背景顏色等等,要想在畫布上面畫畫,一般要與Paint對象結合使用,顧名思義,Paint就是畫筆的風格,顏料的色彩之類的。

        // 創建畫筆  
        Paint paint = new Paint();  
        paint.setColor(Color.RED);// 設置紅色  

        canvas.drawCircle(60, 20, 10, paint);// 畫一個圓  


1.4、Surface本身的作用類似一個句柄,得到了這個句柄就可以得到其中的Canvas、原生緩沖器以及其它方面的內容。


1.5、Surface實現了Parcelable接口,(implements Parcelable),也就是說Surface對象可以把顯示內容的數據寫入到 Parcel 中,並且能夠從Parcel讀回數據。

Parcelable

android.os.Parcelable
Known Indirect Subclasses

Class Overview


Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a static field calledCREATOR, which is an object implementing theParcelable.Creator interface.

簡單翻譯:
       實現這個接口的對象可以寫入數據到Parcel,同時也可以把數據讀出來。



2、SurfaceView


SurfaceView

extends View
java.lang.Object
   ↳ android.view.View
     ↳ android.view.SurfaceView
Known Direct Subclasses

Class Overview


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 callinggetHolder().

The Surface will be created for you while the SurfaceView's window is visible; you should implementsurfaceCreated(SurfaceHolder) andsurfaceDestroyed(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 into the screen. If you are going to use it this way, you need to be aware of some threading semantics:

簡單翻譯:

         SurfaceView提供了一個專門用於繪制的surface,這個surface內嵌於。你可以控制這個Surface的格式和尺寸。Surfaceview控制這個Surface在屏幕的正確繪制位置。

        surface是Z-ordered的(關於Z-ordered,可以参看下一篇博文)這表明它總在自己所在窗口的後面。surfaceview在顯示窗口處为Surface提供了一個可見區域,通過這個區域,才能看到Surface裏面的內容。可以放置一些覆蓋圖層(overlays)在Surface上面,如Button、Textview之類的。但是,需要注意的是,如果Surface上面有全透明的控件,那麼隨着Surface的每一次變化,這些全透明的控件就會重新渲染,這样的話,就影響性能與顯示的效果。

        你可以通過SurfaceHolder這個接口去訪問Surface,而執行getHolder()方法可以得到SurfaceHolder接口。

        當SurfaceView的窗口可見時,Surface就會被創建,當SurfaceView窗口隱藏時,Surface就會被銷毀。當然了,你也可以通過复寫surfaceCreated(SurfaceHolder)surfaceDestroyed(SurfaceHolder)  這兩個方法來驗證一下Surface何時被創建與何時被銷毀。

        SurfaceView提供了一個運行在渲染線程的surface,若你要更新屏幕,你需要了解以下線程知識。



2.1、SurfaceView與Surface的聯系

簡單來說,SurfaceView與Surface的聯系就是,Surface是管理顯示內容的數據(implementsParcelable),包括存儲於數據的交換。而SurfaceView就是把這些數據顯示出來到屏幕上面。

兩者聯系如圖所示:



3、SurfaceHolder


SurfaceHolder

android.view.SurfaceHolder

Class Overview


Abstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface. This interface is typically available through theSurfaceView class.

When using this interface from a thread other than the one running its SurfaceView, you will want to carefully read the methodslockCanvas() andCallback.surfaceCreated().

簡單翻譯:

        SurfaceHolder是控制surface的一個抽象接口,你可以通過SurfaceHolder來控制surface的尺寸和格式,或者修改surface的像素,監視surface的變化等等,SurfaceHolder是SurfaceView的典型接口。

        與直接控制SurfaceView來修改surface不同,使用SurfaceHolder來修改surface時,需要注意lockCanvas()Callback.surfaceCreated().這兩個方法。

SurfaceHolder控制surface的流程所使用的幾個方法。

3.1、abstract void    addCallback(SurfaceHolder.Callback callback)
             Add a Callback interface for this holder.// 给SurfaceHolder一個回調對象。

3.2、abstract Canvas    lockCanvas(Rect dirty)
             Just like lockCanvas() but allows specification of a dirty rectangle.
             // 锁定畫布中的某一個區域,返回的畫布對象Canvas(當更新的內容只有一個區域時,同時要追求高效,可以只更
             新一部分的區域,而不必更新全部畫布區域)

3.3、abstract Canvas    lockCanvas()
             Start editing the pixels in the surface.// 锁定畫布,返回的畫布對象Canvas

3.4、abstract void    removeCallback(SurfaceHolder.Callback callback)
             Removes a previously added Callback interface from this holder.//移除回調對象

3.5、abstract void    unlockCanvasAndPost(Canvas canvas)
             Finish editing pixels in the surface.// 結束锁定畫圖,並提交改變。

4、SurfaceHolder.Callback

SurfaceHolder.Callback

android.view.SurfaceHolder.Callback
Known Indirect Subclasses

Class Overview


A client may implement this interface to receive information about changes to the surface. When used with aSurfaceView, the Surface being held is only available between calls tosurfaceCreated(SurfaceHolder) andsurfaceDestroyed(SurfaceHolder). The Callback is set withSurfaceHolder.addCallback method.

簡單翻譯:

SurfaceHolder.Callback是監聽surface改變的一個接口

4.1、public abstract voidsurfaceChanged(SurfaceHolder holder, int format, int width, int height)

holder The SurfaceHolder whose surface has changed.
format The new PixelFormat of the surface.
width The new width of the surface.
height The new height of the surfa
//surface發生改變時被調用


4.2、public abstract voidsurfaceCreated(SurfaceHolder holder)

Parameters
holder The SurfaceHolder whose surface is being created

//在surface創建時被調用,一般在這個方法裏面開启渲染屏幕的線程。


4.3、public abstract voidsurfaceDestroyed(SurfaceHolder holder)

Parameters
holder The SurfaceHolder whose surface is being destroyed.
  //銷毀時被調用,一般在這個方法裏將渲染的線程停止。

 

5、Demo小程序

附上上述所說幾種的聯系方法

SurfaceHolder = SurfaceView.getHolder();

Surface = SurfaceHolder.getSurface();

Canvas =SurfaceHolder.LockCanvas(Rect dirty)

Canvas   =Surface.lockCanvas(Rect dirty)





From:CSDN
頂一下
(0)
0%
踩一下
(0)
0%
------分隔線----------------------------
發表評論
請自覺遵守互聯網相關的政策法規,嚴禁發布色情、暴力、反動的言論。
評價:
表情:
驗證碼:點擊我更換圖片
欄目列表
推薦內容