Android用surfacetexture实现投屏的代码实现? 
时间: 2023-04-02 10:03:55 浏览: 93
可以参考以下代码实现:
1. 创建 SurfaceTexture 对象
SurfaceTexture surfaceTexture = new SurfaceTexture();
2. 获取 Surface 对象
Surface surface = new Surface(surfaceTexture);
3. 创建 MediaProjection 对象
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
Intent intent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(intent, REQUEST_CODE);
MediaProjection mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
4. 创建 VirtualDisplay 对象
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height = Resources.getSystem().getDisplayMetrics().heightPixels;
int dpi = Resources.getSystem().getDisplayMetrics().densityDpi;
VirtualDisplay virtualDisplay = mediaProjection.createVirtualDisplay("ScreenCapture",
width, height, dpi,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
surface, null, null);
5. 释放资源
virtualDisplay.release();
surface.release();
mediaProjection.stop();
相关推荐


















