Android-View驯用、SurfaceView脸秽、GLSurfaceView的區(qū)別認(rèn)識

老規(guī)矩,看看官方定義

View | Android Developers

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游戲說口柳,就你了苹粟,來試試...

GLSurfaceView的定義

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í)踐下如何正確使用骇窍。

image

官方基本介紹就是上面瓜晤,當(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博客

Android GLSurfaceView詳解

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市遍希,隨后出現(xiàn)的幾起案子等曼,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 210,978評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件禁谦,死亡現(xiàn)場離奇詭異胁黑,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)州泊,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評論 2 384
  • 文/潘曉璐 我一進(jìn)店門丧蘸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人遥皂,你說我怎么就攤上這事力喷。” “怎么了演训?”我有些...
    開封第一講書人閱讀 156,623評論 0 345
  • 文/不壞的土叔 我叫張陵弟孟,是天一觀的道長。 經(jīng)常有香客問我样悟,道長拂募,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,324評論 1 282
  • 正文 為了忘掉前任窟她,我火速辦了婚禮陈症,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘震糖。我一直安慰自己录肯,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,390評論 5 384
  • 文/花漫 我一把揭開白布吊说。 她就那樣靜靜地躺著论咏,像睡著了一般。 火紅的嫁衣襯著肌膚如雪疏叨。 梳的紋絲不亂的頭發(fā)上潘靖,一...
    開封第一講書人閱讀 49,741評論 1 289
  • 那天,我揣著相機(jī)與錄音蚤蔓,去河邊找鬼。 笑死糊余,一個(gè)胖子當(dāng)著我的面吹牛秀又,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播贬芥,決...
    沈念sama閱讀 38,892評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼吐辙,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蘸劈?” 一聲冷哼從身側(cè)響起昏苏,我...
    開封第一講書人閱讀 37,655評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后贤惯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體洼专,經(jīng)...
    沈念sama閱讀 44,104評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年孵构,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了屁商。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,569評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡颈墅,死狀恐怖蜡镶,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情恤筛,我是刑警寧澤官还,帶...
    沈念sama閱讀 34,254評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站毒坛,受9級特大地震影響望伦,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜粘驰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,834評論 3 312
  • 文/蒙蒙 一屡谐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蝌数,春花似錦愕掏、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,725評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至唆貌,卻和暖如春滑潘,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背锨咙。 一陣腳步聲響...
    開封第一講書人閱讀 31,950評論 1 264
  • 我被黑心中介騙來泰國打工语卤, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人酪刀。 一個(gè)月前我還...
    沈念sama閱讀 46,260評論 2 360
  • 正文 我出身青樓粹舵,卻偏偏與公主長得像,于是被迫代替她去往敵國和親骂倘。 傳聞我的和親對象是個(gè)殘疾皇子眼滤,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,446評論 2 348

推薦閱讀更多精彩內(nèi)容