下面列出了android.hardware.display.DisplayManager#VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override // Binder call
public int applyVirtualDisplayFlags(int flags) {
if (mType == MediaProjectionManager.TYPE_SCREEN_CAPTURE) {
flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR
| DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION;
return flags;
} else if (mType == MediaProjectionManager.TYPE_MIRRORING) {
flags &= ~(DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR);
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION;
return flags;
} else if (mType == MediaProjectionManager.TYPE_PRESENTATION) {
flags &= ~DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC |
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION |
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
return flags;
} else {
throw new RuntimeException("Unknown MediaProjection type");
}
}
public void onResume() {
if (mVirtualDisplay == null) {
DisplayManager manager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
Display defaultDisplay = manager.getDisplay(Display.DEFAULT_DISPLAY);
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
defaultDisplay.getMetrics(mDefaultMetrics);
mVirtualDisplay = manager.createVirtualDisplay("OffscreenViews", mWidth, mHeight,
mDefaultMetrics.densityDpi, mSurface, flags);
}
if (mPresentation == null) {
mPresentation = new OffscreenPresentation(mContext, mVirtualDisplay.getDisplay());
mPresentation.show();
if (mContentView != null) {
mPresentation.setContentView(mContentView);
}
}
}
/******************************************
* Factoring Virtual Display creation
****************/
@TargetApi(minAPILevel)
private void createVirtualDisplay() {
if (android.os.Build.VERSION.SDK_INT < minAPILevel) {
Log.w(TAG, "Can't run createVirtualDisplay() due to a low API level. API level 21 or higher is required.");
return;
}
// get width and height
Point size = new Point();
mDisplay.getRealSize(size);
int mWidth = (int)(size.x / downscale);
int mHeight = (int)(size.y / downscale);
if (vncServer != null) {
vncServer.shutdown();
vncServer.run(mWidth, mHeight);
}
startRepeatingPing();
int VIRTUAL_DISPLAY_FLAGS = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
// run capture reader
mImageReader = ImageReader.newInstance(mWidth, mHeight, mMediaProjectionPixelFormat, 2);
mVirtualDisplay = sMediaProjection.createVirtualDisplay("cvio", mWidth, mHeight, mDensity, VIRTUAL_DISPLAY_FLAGS, mImageReader.getSurface(), null, mHandler);
mImageReader.setOnImageAvailableListener(new ImageAvailableListener(), mHandler);
}
/**
* Starts the virtual display if one does not already exist.
*/
private void startVirtualDisplay() {
if (DEBUG) {
Log.d(TAG, "Request to start VD, DM:" + mDisplayManager);
}
if (mDisplayManager == null) {
Log.w(TAG, "Cannot create virtual display because mDisplayManager == null");
return;
}
synchronized (mVdLock) {
if (mVirtualDisplay != null) {
Log.i(TAG, "VD already exists, ignoring request");
return;
}
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_SUPPORTS_TOUCH;
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_ROTATES_WITH_CONTENT;
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC;
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY;
flags |= DisplayManager.VIRTUAL_DISPLAY_FLAG_DESTROY_CONTENT_ON_REMOVAL;
mVirtualDisplay = mDisplayManager.createVirtualDisplay(null /* projection */,
DISPLAY_NAME, mVirtualDisplayWidth, mVirtualDisplayHeight, mVirtualDisplayDpi,
null /* surface */, flags, null /* callback */, null /* handler */,
UNIQUE_DISPLAY_ID);
if (mVirtualDisplay != null) {
updateDisplayId(mVirtualDisplay.getDisplay().getDisplayId());
// Now create the ImageReader to supply a Surface to the new virtual display.
startImageReader();
} else {
Log.w(TAG, "Virtual display id is null after createVirtualDisplay");
updateDisplayId(INVALID_DISPLAY);
return;
}
}
Log.i(TAG, "VD created: " + mVirtualDisplay);
}
/**
* 创建一个录屏 VirtualDisplay
*/
private void createScreenRecordVirtualDisplay() {
VMLog.i("createScreenRecordVirtualDisplay");
getScreenInfo();
initRecorder();
String virtualDisplayName = "ScreenRecord";
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC | DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
virtualDisplay = mediaProjection.createVirtualDisplay(virtualDisplayName, width, height, density, flags, mediaRecorder
.getSurface(), null, null);
}
/**
* 创建一个ImageReader VirtualDisplay
*/
private void createScreenShortVirtualDisplay() {
VMLog.i("createScreenShortVirtualDisplay");
getScreenInfo();
initImageReader();
String virtualDisplayName = "ScreenShort";
int flags = DisplayManager.VIRTUAL_DISPLAY_FLAG_OWN_CONTENT_ONLY | DisplayManager.VIRTUAL_DISPLAY_FLAG_PUBLIC | DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
virtualDisplay = mediaProjection.createVirtualDisplay(virtualDisplayName, width, height, density, flags, imageReader
.getSurface(), null, null);
}