SurfaceView
程序员文章站
2022-03-02 10:22:12
...
Surface
Surface继承于Parcelable,内部拥有一系列native方法和一个canvas
private final Canvas mCanvas = new CompatibleCanvas();
private Matrix mCompatibleMatrix
CompatibleCanvas是Surface的内部类,主要包装了Canvas的Matrix方法
SurfaceView继承自View,说明其本质还是个View
SurfaceView
public class SurfaceView extends View {}
在SurfaceView内部有两个Surface
final Surface mSurface = new Surface();
final Surface mNewSurface = new Surface();
还包含Window的相关对象
IWindowSession mSession;
MyWindow mWindow;
IWindowSession是一个aidl文件,是app上层连接底层WindowServiceManager的桥梁
MyWindow是SurfaceView的内部类,继承自BaseIWindow,BaseIWindow又继承自IWindow.Stub
private static class MyWindow extends BaseIWindow{}
public class BaseIWindow extends IWindow.Stub{}
在SurfaceView的onAttachtoWindow中,向父view申请为自己扣个洞
mParent.requestTransparentRegion(this)
在视图发生变化比如可见性发生变化的时候,会调用
updateWindow(false, false);
在updateWindow内部,首先初始化window
mWindow = new MyWindow(this);
mSession.addToDisplayWithoutInputChannel(mWindow, mWindow.mSeq, mLayout,mVisible ? VISIBLE : GONE, display.getDisplayId(), mContentInsets,mStableInsets);
之后申请surface
relayoutResult = mSession.relayout(
mWindow, mWindow.mSeq, mLayout, mWidth, mHeight,
visible ? VISIBLE : GONE,
WindowManagerGlobal.RELAYOUT_DEFER_SURFACE_DESTROY,
mWinFrame, mOverscanInsets, mContentInsets,
mVisibleInsets, mStableInsets, mConfiguration, mNewSurface);
之后调用SurfaceCallBack销毁当前的Surface
callbacks = getSurfaceCallbacks();
for (SurfaceHolder.Callback c : callbacks) {
c.surfaceDestroyed(mSurfaceHolder);
}
把新申请的Surface替换为当前的Surface
mSurface.transferFrom(mNewSurface);
最后调用callback的另外两个方法
for (SurfaceHolder.Callback c : callbacks) {
c.surfaceCreated(mSurfaceHolder);
}
for (SurfaceHolder.Callback c : callbacks) {
c.surfaceChanged(mSurfaceHolder, mFormat, myWidth, myHeight);
}
其他的View都是利用在activityAttch时生成的PhoneWindow管理Surface,只能在主线程操作,SurfaceView拥有独立的Surface,独立于PhoneWindow,因此不受主线程限制,可以在子线程刷新,同时由于不在View树里面,所以无法进行缩放,旋转等动画
推荐阅读
-
Android截屏SurfaceView黑屏问题的解决办法
-
android图像绘制(四)自定义一个SurfaceView控件
-
Android Fragment中使用SurfaceView切换时闪一下黑屏的解决办法
-
Android利用SurfaceView实现下雨的天气动画效果
-
Android编程之SurfaceView学习示例详解
-
Android中SurfaceView用法简单实例
-
两个surfaceView实现切换效果
-
Android SurfaceView画板操作
-
android surfaceView实现播放视频功能
-
android使用surfaceview+MediaPlayer播放视频