1.android videoviewåsurfaceviewçåºå«
2.surfaceviewåviewçåºå«
android videoviewåsurfaceviewçåºå«
ããSurfaceViewæ¯ä»Viewåºç±»ä¸æ´¾çåºæ¥çæ¾ç¤ºç±»ï¼ç´æ¥åç±»æGLSurfaceViewåVideoViewï¼å¯ä»¥çåºGLåè§é¢ææ¾ä»¥åCameraæå头ä¸è¬å使ç¨SurfaceViewï¼å°åºæåªäºä¼å¿å¢?gec完整源码 SurfaceViewå¯ä»¥æ§å¶è¡¨é¢çæ ¼å¼ï¼æ¯å¦å¤§å°ï¼æ¾ç¤ºå¨å±å¹ä¸çä½ç½®ï¼æå ³é®æ¯çæä¾äºSurfaceHolderç±»ï¼ä½¿ç¨getHolderæ¹æ³è·åï¼ç¸å ³çæCanvas lockCanvas()
ããCanvas lockCanvas(Rect dirty) ãvoid removeCallback(SurfaceHolder.Callback callback)ãvoid unlockCanvasAndPost(Canvas canvas) æ§å¶å¾å½¢ä»¥åç»å¶ï¼èå¨SurfaceHolder.Callback æ¥å£åè°ä¸å¯ä»¥éè¿éåä¸é¢æ¹æ³å®ç°ã
ãã使ç¨çSurfaceViewçæ¶åï¼ä¸è¬æ åµä¸è¦å¯¹å ¶è¿è¡å建ï¼éæ¯ï¼æ¹åæ¶çæ åµè¿è¡çè§ï¼è¿å°±è¦ç¨å° SurfaceHolder.Callback.
ããclass XxxView extends SurfaceView implements SurfaceHolder.Callback {
ããpublic void surfaceChanged(SurfaceHolder holder,int format,int width,int height){ }
ãã//çå ¶åç¥å ¶ä¹ï¼å¨surfaceç大å°åçæ¹åæ¶æ¿å
ããpublic void surfaceCreated(SurfaceHolder holder){ }
ãã//åä¸ï¼å¨å建æ¶æ¿åï¼ä¸è¬å¨è¿éè°ç¨ç»å¾ç线ç¨ã
ããpublic void surfaceDestroyed(SurfaceHolder holder) { }
ãã//åä¸ï¼éæ¯æ¶æ¿åï¼ä¸è¬å¨è¿éå°ç»å¾ç线ç¨åæ¢ãéæ¾ã
ãã}
ãã对äºSurfaceç¸å ³çï¼Androidåºå±è¿æä¾äºGPUå éåè½ï¼æ以ä¸è¬å®æ¶æ§å¾å¼ºçåºç¨ä¸ä¸»è¦ä½¿ç¨SurfaceViewèä¸æ¯ç´æ¥ä»Viewæ建ï¼åæ¶åæ¥åandroid 3d OpenGLä¸çGLSurfaceViewä¹æ¯ä»è¯¥ç±»å®ç°ã
ããSurfaceViewåViewææ¬è´¨çåºå«å¨äºï¼surfaceViewæ¯å¨ä¸ä¸ªæ°èµ·çåç¬çº¿ç¨ä¸å¯ä»¥éæ°ç»å¶ç»é¢èViewå¿ é¡»å¨UIç主线ç¨ä¸æ´æ°ç»é¢ã
ããé£ä¹å¨UIç主线ç¨ä¸æ´æ°ç»é¢ å¯è½ä¼å¼åé®é¢ï¼æ¯å¦ä½ æ´æ°ç»é¢çæ¶é´è¿é¿ï¼é£ä¹ä½ ç主UI线ç¨ä¼è¢«ä½ æ£å¨ç»çå½æ°é»å¡ãé£ä¹å°æ æ³ååºæé®ï¼è§¦å±çæ¶æ¯ã
ããå½ä½¿ç¨surfaceView ç±äºæ¯å¨æ°ç线ç¨ä¸æ´æ°ç»é¢æ以ä¸ä¼é»å¡ä½ çUI主线ç¨ãä½è¿ä¹å¸¦æ¥äºå¦å¤ä¸ä¸ªé®é¢ï¼å°±æ¯äºä»¶åæ¥ãæ¯å¦ä½ 触å±äºä¸ä¸ï¼ä½ éè¦surfaceViewä¸threadå¤çï¼ä¸è¬å°±éè¦æä¸ä¸ªevent queueç设计æ¥ä¿åtouch eventï¼è¿ä¼ç¨ç¨å¤æä¸ç¹ï¼å 为æ¶åå°çº¿ç¨åæ¥ã
ããæ以åºäºä»¥ä¸ï¼æ ¹æ®æ¸¸æç¹ç¹ï¼ä¸è¬åæ两类ã
ãã1 被å¨æ´æ°ç»é¢çãæ¯å¦æ£ç±»ï¼è¿ç§ç¨view就好äºãå 为ç»é¢çæ´æ°æ¯ä¾èµäº onTouch æ¥æ´æ°ï¼å¯ä»¥ç´æ¥ä½¿ç¨ invalidateã å 为è¿ç§æ åµä¸ï¼è¿ä¸æ¬¡Touchåä¸ä¸æ¬¡çTouchéè¦çæ¶é´æ¯è¾é¿äºï¼ä¸ä¼äº§çå½±åã
ãã2 主å¨æ´æ°ãæ¯å¦ä¸ä¸ªäººå¨ä¸ç´è·å¨ãè¿å°±éè¦ä¸ä¸ªåç¬çthreadä¸åçéç»äººçç¶æï¼é¿å é»å¡main UI threadãæ以æ¾ç¶viewä¸åéï¼éè¦surfaceViewæ¥æ§å¶ã
ãã
ãã3.Androidä¸çSurfaceView类就æ¯åç¼å²æºå¶ãå æ¤ï¼å¼å游ææ¶å°½é使ç¨SurfaceViewèä¸è¦ä½¿ç¨Viewï¼è¿æ ·çè¯æçè¾é«ï¼èä¸SurfaceViewçåè½ä¹æ´å å®åã
ãã
ããèè以ä¸å ç¹ï¼æ以æä¸ç´é½éç¨ SurfaceView æ¥è¿è¡æ¸¸æå¼åã
ããé£ä¹å¨ä»¥åæºç å®ä¾ä¸ï¼æé½ä¼ä»¥ç»§æ¿sarfaceViewæ¡æ¶æ¥è¿è¡æ¼ç¤ºãä¸ä¸ç« å°è¯¦ç»åæsarfaceview ï¼ä»¥åéä¸æ¬äººåç游æå¼åæ¶æ
surfaceviewåviewçåºå«
ããSurfaceViewæ¯ä»Viewåºç±»ä¸æ´¾çåºæ¥çæ¾ç¤ºç±»ï¼ç´æ¥åç±»æGLSurfaceViewåVideoViewï¼å¯ä»¥çåºGLåè§é¢ææ¾ä»¥åCameraæå头ä¸è¬å使ç¨SurfaceViewï¼å°åºæåªäºä¼å¿å¢? SurfaceViewå¯ä»¥æ§å¶è¡¨é¢çæ ¼å¼ï¼æ¯å¦å¤§å°ï¼æ¾ç¤ºå¨å±å¹ä¸çä½ç½®ï¼æå ³é®æ¯çæä¾äºSurfaceHolderç±»ï¼ä½¿ç¨getHolderæ¹æ³è·åï¼ç¸å ³çæCanvas lockCanvas()
ããCanvas lockCanvas(Rect dirty) ãvoid removeCallback(SurfaceHolder.Callback callback)ãvoid unlockCanvasAndPost(Canvas canvas) æ§å¶å¾å½¢ä»¥åç»å¶ï¼èå¨SurfaceHolder.Callback æ¥å£åè°ä¸å¯ä»¥éè¿éåä¸é¢æ¹æ³å®ç°ã
ãã使ç¨çSurfaceViewçæ¶åï¼ä¸è¬æ åµä¸è¦å¯¹å ¶è¿è¡å建ï¼éæ¯ï¼æ¹åæ¶çæ åµè¿è¡çè§ï¼è¿å°±è¦ç¨å° SurfaceHolder.Callback.
ããclass XxxView extends SurfaceView implements SurfaceHolder.Callback {
ããpublic void surfaceChanged(SurfaceHolder holder,int format,int width,int height){ }
ãã//çå ¶åç¥å ¶ä¹ï¼å¨surfaceç大å°åçæ¹åæ¶æ¿å
ããpublic void surfaceCreated(SurfaceHolder holder){ }
ãã//åä¸ï¼å¨å建æ¶æ¿åï¼ä¸è¬å¨è¿éè°ç¨ç»å¾ç线ç¨ã
ããpublic void surfaceDestroyed(SurfaceHolder holder) { }
ãã//åä¸ï¼éæ¯æ¶æ¿åï¼ä¸è¬å¨è¿éå°ç»å¾ç线ç¨åæ¢ãéæ¾ã
ãã}
ãã对äºSurfaceç¸å ³çï¼Androidåºå±è¿æä¾äºGPUå éåè½ï¼æ以ä¸è¬å®æ¶æ§å¾å¼ºçåºç¨ä¸ä¸»è¦ä½¿ç¨SurfaceViewèä¸æ¯ç´æ¥ä»Viewæ建ï¼åæ¶åæ¥åandroid 3d OpenGLä¸çGLSurfaceViewä¹æ¯ä»è¯¥ç±»å®ç°ã
ããSurfaceViewåViewææ¬è´¨çåºå«å¨äºï¼surfaceViewæ¯å¨ä¸ä¸ªæ°èµ·çåç¬çº¿ç¨ä¸å¯ä»¥éæ°ç»å¶ç»é¢èViewå¿ é¡»å¨UIç主线ç¨ä¸æ´æ°ç»é¢ã
ããé£ä¹å¨UIç主线ç¨ä¸æ´æ°ç»é¢ å¯è½ä¼å¼åé®é¢ï¼æ¯å¦ä½ æ´æ°ç»é¢çæ¶é´è¿é¿ï¼é£ä¹ä½ ç主UI线ç¨ä¼è¢«ä½ æ£å¨ç»çå½æ°é»å¡ãé£ä¹å°æ æ³ååºæé®ï¼è§¦å±çæ¶æ¯ã
ããå½ä½¿ç¨surfaceView ç±äºæ¯å¨æ°ç线ç¨ä¸æ´æ°ç»é¢æ以ä¸ä¼é»å¡ä½ çUI主线ç¨ãä½è¿ä¹å¸¦æ¥äºå¦å¤ä¸ä¸ªé®é¢ï¼å°±æ¯äºä»¶åæ¥ãæ¯å¦ä½ 触å±äºä¸ä¸ï¼ä½ éè¦surfaceViewä¸threadå¤çï¼ä¸è¬å°±éè¦æä¸ä¸ªevent queueç设计æ¥ä¿åtouch eventï¼è¿ä¼ç¨ç¨å¤æä¸ç¹ï¼å 为æ¶åå°çº¿ç¨åæ¥ã
ããæ以åºäºä»¥ä¸ï¼æ ¹æ®æ¸¸æç¹ç¹ï¼ä¸è¬åæ两类ã
ãã1 被å¨æ´æ°ç»é¢çãæ¯å¦æ£ç±»ï¼è¿ç§ç¨view就好äºãå 为ç»é¢çæ´æ°æ¯ä¾èµäº onTouch æ¥æ´æ°ï¼å¯ä»¥ç´æ¥ä½¿ç¨ invalidateã å 为è¿ç§æ åµä¸ï¼è¿ä¸æ¬¡Touchåä¸ä¸æ¬¡çTouchéè¦çæ¶é´æ¯è¾é¿äºï¼ä¸ä¼äº§çå½±åã
ãã2 主å¨æ´æ°ãæ¯å¦ä¸ä¸ªäººå¨ä¸ç´è·å¨ãè¿å°±éè¦ä¸ä¸ªåç¬çthreadä¸åçéç»äººçç¶æï¼é¿å é»å¡main UI threadãæ以æ¾ç¶viewä¸åéï¼éè¦surfaceViewæ¥æ§å¶ã
ãã
ãã3.Androidä¸çSurfaceView类就æ¯åç¼å²æºå¶ãå æ¤ï¼å¼å游ææ¶å°½é使ç¨SurfaceViewèä¸è¦ä½¿ç¨Viewï¼è¿æ ·çè¯æçè¾é«ï¼èä¸SurfaceViewçåè½ä¹æ´å å®åã
ãã
ããèè以ä¸å ç¹ï¼æ以æä¸ç´é½éç¨ SurfaceView æ¥è¿è¡æ¸¸æå¼åã
ããé£ä¹å¨ä»¥åæºç å®ä¾ä¸ï¼æé½ä¼ä»¥ç»§æ¿sarfaceViewæ¡æ¶æ¥è¿è¡æ¼ç¤ºãä¸ä¸ç« å°è¯¦ç»åæsarfaceview ï¼ä»¥åéä¸æ¬äººåç游æå¼åæ¶æ