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每次预览都会黑屏一下的问题。

相关推荐
淡淡的香烟1 小时前
AndroidAI使用心得
android
康谋自动驾驶1 小时前
GMSL2相机ROS2采集链路:多相机时序偏差降低方案
数码相机·自动驾驶·汽车·仿真测试
a1117761 小时前
工业视觉软件入门:VisionMaster(VM)、HALCON、VisionPro
嵌入式硬件·数码相机
chjif1 小时前
Android 设备管控开发实战:无障碍如何准确识别当前前台 App?
android
chjif2 小时前
Android 设备管控开发实战:企业受控终端的 DNS 域名策略实现
android·华为·harmonyos
cxoptics2 小时前
机器视觉/工业检测中光源选择?
数码相机
c238563 小时前
MySQL 基础用法(下):查询进阶与核心特性
android·c语言·c++·mysql
math_hongfan4 小时前
鸿蒙Agent高级技能自定义开发:自定义意图服务/能力注册/Agent技能编排/服务插件开发实战
android·学习·华为·harmonyos·鸿蒙
2501_915106325 小时前
iOS 证书类型及作用说明 账号证书与签名配置的完整解读
android·ios·小程序·https·uni-app·iphone·webview
九皇叔叔5 小时前
RHEL9 安装 MySQL 8.4.11.LTS 版本
android·mysql·adb