欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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树里面,所以无法进行缩放,旋转等动画

相关标签: Surface SurfaceView