老規(guī)矩,看看官方定義
SurfaceView | Android Developers
GLSurfaceView | Android Developers
View定義
public class View
extends Object implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource
java.lang.Object
? android.view.View
Known direct subclasses
AnalogClock, ImageView, KeyboardView, MediaRouteButton, ProgressBar, Space, SurfaceView, TextView, TextureView, ViewGroup, ViewStub
Known indirect subclasses
AbsListView, AbsSeekBar, AbsSpinner, AbsoluteLayout, ActionMenuView, AdapterView<T extends Adapter>, AdapterViewAnimator, AdapterViewFlipper, AppWidgetHostView, AutoCompleteTextView, Button, CalendarView, and 52 others.
This class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). The ViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.
翻譯:繼承自O(shè)bject, 直接繼承或者間接繼承該View的相關(guān)了有Textview, Recyclview,ViewGroup等蝴乔〖遣停基本上組合控件大多都是繼承ViewGroup(間接的就繼承了View). 該View占據(jù)了屏幕的一塊矩形區(qū)域以及負(fù)責(zé)繪制和事件的處理。它是所有控件的基類薇正,用于創(chuàng)建UI組件(像buttons片酝, 文本區(qū)域等)巩剖。之后就是View的很多方法,如何定義View的一些介紹钠怯。之前我們學(xué)習(xí)自定義View也有所了解了佳魔。自定義控件的水還是蠻深的。
繪制刷新是屬于被動繪制刷新晦炊,只能在主線程中做刷新操作鞠鲜。一般我們需要刷新都需要invalidate();一下。
SurfaceView的定義
public class SurfaceView
extends View
java.lang.Object
? android.view.View
? android.view.SurfaceView
Known direct subclasses
GLSurfaceView, VideoView
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.
The transparent region that makes the surface visible is based on the layout positions in the view hierarchy. If the post-layout transform properties are used to draw a sibling view on top of the SurfaceView, the view may not be properly composited with the surface.
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 SurfaceHolder.Callback.surfaceCreated(SurfaceHolder) and SurfaceHolder.Callback.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 into 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().
Note: Starting in platform version Build.VERSION_CODES.N, SurfaceView's window position is updated synchronously with other View rendering. This means that translating and scaling a SurfaceView on screen will not cause rendering artifacts. Such artifacts may occur on previous versions of the platform when its window is positioned asynchronously.
翻譯: SurfaceView繼承自View断国,并提供了一個(gè)可以嵌入到View結(jié)構(gòu)樹中的獨(dú)立的繪圖層贤姆,你可以完全控制這個(gè)繪圖層,比如說設(shè)定它的大小稳衬。SurfaceView負(fù)責(zé)將surface放置在屏幕上的正確位置霞捡。 這個(gè)Surface的Z軸方向允許窗體在它后面持有該SurfaceView,就是在窗體上打了個(gè)洞允許surface顯示薄疚。由于視圖的相互置頂重疊碧信,就會發(fā)生繪制混合(就是alpha混合處理), 這對性能有影響街夭。如果在SurfaceView上面繪制一個(gè)姊妹控件砰碴,有可能不能正確混合。
可以通過getHolder()獲取SurfaceHolder - Return the SurfaceHolder providing access and control over this SurfaceView's underlying surface.
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 the SurfaceView class.
When using this interface from a thread other than the one running its SurfaceView, you will want to carefully read the methods lockCanvas() and Callback.surfaceCreated().
顧名思義就是Surface的持有者板丽,SurfaceView就是通過過SurfaceHolder來對Surface進(jìn)行管理控制的呈枉。比如控件大小, 格式埃碱,編輯像素猖辫,以及監(jiān)視其變化。通過SurfaceView可以獲取該接口砚殿。
當(dāng)窗體顯示時(shí)你需要實(shí)現(xiàn)SurfaceHolder.Callback.surfaceCreated(SurfaceHolder)啃憎, 當(dāng)窗體隱藏后需要實(shí)現(xiàn)SurfaceHolder.Callback.surfaceDestroyed(SurfaceHolder)。分別處理surface的創(chuàng)建和銷毀瓮具。
用SurfaceView的一個(gè)主要目的是為了開啟第二個(gè)線程去在片屏幕上做渲染荧飞。如果你打算這樣做,你需要遵循一些語法:
1. 所有的SurfaceView and[SurfaceHolder.Callback](https://link.zhihu.com/?target=https%3A//developer.android.google.cn/reference/android/view/SurfaceHolder.Callback.html%3Fhl%3Dzh-cn)
回調(diào)都是在窗體的主線程中調(diào)用名党。因此我們需要處理好這些回調(diào)與繪制的同步叹阔。
2. 你必須要保證繪制線程可用。 在 [SurfaceHolder.Callback.surfaceCreated()](https://link.zhihu.com/?target=https%3A//developer.android.google.cn/reference/android/view/SurfaceHolder.Callback.html%3Fhl%3Dzh-cn%23surfaceCreated%28android.view.SurfaceHolder%29)
and[SurfaceHolder.Callback.surfaceDestroyed()](https://link.zhihu.com/?target=https%3A//developer.android.google.cn/reference/android/view/SurfaceHolder.Callback.html%3Fhl%3Dzh-cn%23surfaceDestroyed%28android.view.SurfaceHolder%29)
之間才能調(diào)用传睹。
注意: 從Android 版本<u style="text-decoration: none; border-bottom: 1px dashed grey;">[Build.VERSION_CODES.N](https://link.zhihu.com/?target=https%3A//developer.android.google.cn/reference/android/os/Build.VERSION_CODES.html%3Fhl%3Dzh-cn%23N)</u>
, 開始耳幢, SurfaceView的窗口位置與其他視圖呈現(xiàn)同步更新, 這意味著平移縮放不會有rendering artifacts問題了。而早期的版本則會產(chǎn)生rendering artifacts的問題. rendering artifacts - 渲染偽影(百度翻譯)睛藻,小白理解就是如果不處理好同步的問題启上,可能就是產(chǎn)生畫面花,比如按鈕畫了一半店印,又去畫文本冈在,然后又交叉,就會刪除渲染花的問題按摘。 小白還是不太懂包券。
SurfaceView - 后面需要實(shí)踐下如何正確使用。爭取多實(shí)踐下....
1. 不過從上面看主要的特點(diǎn)就是可以新開一個(gè)線程用作繪制炫贤,進(jìn)而避免必須在主線程中做長時(shí)間UI更新導(dǎo)致的卡頓甚至ANR的問題溅固。
2. 新開一個(gè)線程做繪制除了避免阻塞UI線程,還能提供“主動刷新”兰珍,可以不停的進(jìn)行繪制(比如人在不停的跑)更新侍郭。 這樣想動畫也可以做了哈。Animation相關(guān)動畫類是不是也是這樣呢(值得研究)掠河?或者某些情況下亮元,surfaceview能更好的掌控和優(yōu)化動畫性能。
3. 2D游戲說口柳,就你了苹粟,來試試...
public class GLSurfaceView
extends SurfaceView implements SurfaceHolder.Callback2
java.lang.Object
? android.view.View
? android.view.SurfaceView
? android.opengl.GLSurfaceView
An implementation of SurfaceView that uses the dedicated surface for displaying OpenGL rendering.
A GLSurfaceView provides the following features:
Manages a surface, which is a special piece of memory that can be composited into the Android view system.
Manages an EGL display, which enables OpenGL to render into a surface.
Accepts a user-provided Renderer object that does the actual rendering.
Renders on a dedicated thread to decouple rendering performance from the UI thread.
Supports both on-demand and continuous rendering.
Optionally wraps, traces, and/or error-checks the renderer's OpenGL calls.
翻譯:針對SurfaceView,Google又?jǐn)U展了GLSurfaceView, 專門用于OpenGL rendering使用跃闹。OpenGL喲∶茫【 對這個(gè)感興趣的可以了解下OpenGL的相關(guān)知識望艺。小白剛開始工作是做的地圖,當(dāng)時(shí)就是OpenGL封裝的底層渲染引擎(采用c語言肌访,結(jié)合了cairo(開羅2d繪制引擎庫))找默。所以小白還是有一定了解。 同時(shí)第二份工作就是Android 3D做三維模型建模(OpenGL es2.0+的版本)吼驶,處理起來很麻煩惩激,性能也不太行,后面才采用了U3D引擎(引擎性能上蟹演,渲染风钻,手勢等什么的處理的比較好),順帶提下哈】酒请。
GLSurfaceView提供了如下特性:
1> 提供并且管理一個(gè)獨(dú)立的Surface骡技。
2> 提供并且管理一個(gè)EGL display,它能讓opengl把內(nèi)容渲染到上述的Surface上。
3> 支持用戶自定義渲染器(Render)布朦,通過setRenderer設(shè)置一個(gè)自定義的Renderer囤萤。
4> 讓渲染器在獨(dú)立的GLThread線程里運(yùn)作,和UI線程分離是趴。
5> 支持按需渲染(on-demand)和連續(xù)渲染(continuous)兩種模式涛舍。
6> 另外還針對OpenGL調(diào)用進(jìn)行追蹤和錯(cuò)誤檢查。
另外:
GPU加速:GLSurfaceView的效率是SurfaceView的30倍以上唆途,SurfaceView使用畫布進(jìn)行繪制做盅,GLSurfaceView利用GPU加速提高了繪制效率。
View的繪制onDraw(Canvas canvas)使用Skia渲染引擎渲染窘哈,而GLSurfaceView的渲染器Renderer的onDrawFrame(GL10 gl)使用opengl繪制引擎進(jìn)行渲染吹榴。
感覺很騷的樣子。GPU渲染你不得不服滚婉,就是快图筹。基本上3D就是它了让腹。如果要做純Android 3D的小游戲远剩,重點(diǎn)關(guān)注下這個(gè)GLSurfaceView。
GLSurfaceView- 后面需要實(shí)踐下如何正確使用骇窍。
官方基本介紹就是上面瓜晤,當(dāng)然每個(gè)還有很多知識點(diǎn)。我們先了解基本的介紹吧腹纳,后面可以先直接做個(gè)實(shí)踐痢掠,加深下基本理解,給自己一個(gè)提前的研究指導(dǎo)嘲恍,一旦有更多時(shí)間和經(jīng)歷就應(yīng)該深入擴(kuò)展學(xué)習(xí)了喲足画!
網(wǎng)上有很多解釋,有些已經(jīng)過時(shí)佃牛,不過可以了解下淹辞,了解自己想了解的就行:
QT3D | IT自習(xí)室 - 小白早期的學(xué)習(xí)記錄(Qt平臺實(shí)踐)
VBO、FBO俘侠、PBO 學(xué)習(xí)總結(jié)貼 (OpenGL ES) - 小白早期的學(xué)習(xí)記錄
Android 視頻展示控件之 SurfaceView象缀、GLSurfaceView、SurfaceTexture爷速、TextureView 對比總結(jié)
Android中View央星,SurfaceView和GLSurfaceView繪圖的區(qū)別 - doom20082004的博客 - CSDN博客