Android SurfaceView预览相机黑屏问题解决方案

解决方案

1、使用动态添加的方式添加surfaceView

在xml文件中添加FrameLayout

复制代码
<FrameLayout
    android:id="@+id/colorsurface"
    android:layout_width="match_parent"
    android:layout_height="match_parent" /> 

2.创建SurfaceView并添加到FrameLayout上

复制代码
surfaceView = new SurfaceView(this);
screenBinding.colorsurface.addView(surfaceView);

3.最后设置surfaceview两个属性

复制代码
surfaceView.setZOrderMediaOverlay(true);
surfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);

即可解决surfaceview每次预览都会黑屏一下的问题。

相关推荐
千里马学框架10 小时前
app性能优化:优化布局层次结构
android·面试·性能优化·framework·分屏·布局·小米汽车
dustcell.10 小时前
高性能web服务器
android·服务器·前端
zh_xuan10 小时前
React Native Demo
android·javascript·react native·ts
zh_xuan10 小时前
kotlin 挂起函数2
android·kotlin·挂起函数
kyle~10 小时前
MySQL基础知识点与常用SQL语句整理
android·sql·mysql
XiaoLeisj11 小时前
Android RecyclerView 实战:从基础列表到多类型 Item、分割线与状态复用问题
android·java
zh_xuan11 小时前
kotlin async异步协程构建器
android·kotlin·协程
阿林来了11 小时前
Flutter三方库适配OpenHarmony【flutter_web_auth】— Android 端 Chrome Custom Tabs 实现分析
android·chrome·flutter
zh_xuan13 小时前
kotlin Channel的用法
android·kotlin·协程·channel
zh_xuan13 小时前
kotlin Flow的用法
android·开发语言·kotlin·协程·flow