使Android相机在所有活动中保持打开 [英] Keeping Android camera open across activities
问题描述
我正在尝试打开Android相机,并使其在多个活动中始终在后台运行,以供Glass和非Glass设备的各种长时间运行的镜框使用者使用。这意味着我不能简单地在一个活动停止时关闭相机,而在下一个活动开始时重新打开相机;它必须始终保持打开状态。我尝试使用应用程序作为上下文(而不是活动)创建SurfaceView,并将其直接添加到WindowManager。这的确使我保持了相机的打开状态,但这是我将其添加为系统叠加层的唯一方法,它会在实际活动的顶部渲染并使其无法使用。我尝试过的其他方法均无效:
I'm trying to open the Android camera and keep it running in the background constantly across multiple activities for a variety of long-running frame consumers for both Glass and non-Glass devices. This means I can not simply close the camera when one activity stops and re-open it when the next one starts; it must stay open the entire time. I've tried creating a SurfaceView using the Application as the Context (rather than an Activity) and adding it directly to the WindowManager. This does let me keep the camera open, but the only if I add it as a system overlay, which renders on top of the actual activities and makes them unusable. Other things I have tried that did not work:
- 使用Activity生命周期回调在活动出现时将其查找并将内容视图带到前面(系统覆盖所有内容)
- 调用
SetWillNotDraw(false)
并覆盖OnDraw()自定义SurfaceView的code>实际上不会呈现任何内容
- 调用
SetWillNotDraw(false)
并覆盖<$ c $自定义SurfaceView的c> OnDraw()渲染一个黑色的矩形,覆盖不透明和透明的屏幕 - 将预览尺寸设置为(1、1 )后,打开相机(仍以全屏呈现)
- 使用我的初始Activity作为上下文创建SurfaceView,并将其作为具有非触摸标志的应用程序类型添加到WindowManager中(仍然呈现
- 将
WindowManager.LayoutParams
中的位置设置为不在屏幕上显示( s直到显示在屏幕上)
- Using Activity lifecycle callbacks to find Activities as they come up and bring their content views to the front (system overlay covers all)
- Calling
SetWillNotDraw(false)
and overriding theOnDraw()
of a custom SurfaceView to not actually render anything - Calling
SetWillNotDraw(false)
and overriding theOnDraw()
of a custom SurfaceView to render a black rectangle that covers the screen, both opaque and transparent - Setting the preview size to (1, 1) after opening the camera (still rendered full-screen)
- Creating the SurfaceView using my initial Activity as the Context and adding it to WindowManager as an Application type with no-touch flags (still rendered on top even after pushing to back, and consumed input)
- Setting the position in the
WindowManager.LayoutParams
to go off-screen (still rendered on screen)
我的想法已经用完了,不确定我想要什么。有任何建议吗?
I'm running out of ideas, and am not sure what I want is even possible. Any suggestions?
推荐答案
使用服务来处理任何特定Activity上下文之外的预览框架。为了避免隐藏SurfaceView的问题,请改用SurfaceTexture,并且不要将其连接到任何东西。
Use a Service to process the preview frames outside the context of any particular Activity. To avoid the problem with hiding the SurfaceView, use a SurfaceTexture instead, and just don't connect it to anything.
请参见 https://github.com/Affectiva/android-sdk-samples/tree/master/ServiceFrameDetectorDemo 这种方法的一个例子。
See https://github.com/Affectiva/android-sdk-samples/tree/master/ServiceFrameDetectorDemo for an example of this approach.
这篇关于使Android相机在所有活动中保持打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!