Android 相机:线程?哪个应该做什么
Android camera: Threads? Which should do what
我正在尝试弄清楚哪些线程应该在 Android 中执行哪些操作。
我在官方文档中发现的唯一一件事是 camera.open()
应该放在它自己的线程中。
怎么样:
camera.startPreview()
camera.stopPreview()
camera.release()
它没有说明他们需要哪个线程。它们必须在主线程(ui 线程)上 运行 吗?还是我可以自由选择?
我为什么要弄清楚这个? camera.startPreview()
当主线程上的 运行 导致我的应用程序短时间 jitter/lag 时,这严重影响了我的应用程序,因为它被放在 viewPager 中,我不希望让相机始终预览(这不会导致延迟,但会占用系统资源)。
有什么想法吗?
Camera 的文档指出 class 不是线程安全的,应该 不 一次从多个线程调用(我想,除非您正在执行自己的同步)。
它表示回调将传递给调用 open
的线程
来自参考文献(强调我的):
This class is not thread-safe, and is meant for use from one event thread. Most long-running operations (preview, focus, photo capture, etc) happen asynchronously and invoke callbacks as necessary. Callbacks will be invoked on the event thread open(int) was called from. This class's methods must never be called from multiple threads at once.
来自open(int)
方法参考:
Callbacks from other methods are delivered to the event loop of the thread which called open(). If this thread has no event loop, then callbacks are delivered to the main application event loop. If there is no main application event loop, callbacks are not delivered.
Caution: On some devices, this method may take a long time to complete. It is best to call this method from a worker thread (possibly using AsyncTask) to avoid blocking the main application UI thread.
它需要的线程就是您用来调用open(int)
的线程。
所以要回答你的问题,是的,你可以相对自由地选择,但你必须保持一致。
我正在尝试弄清楚哪些线程应该在 Android 中执行哪些操作。
我在官方文档中发现的唯一一件事是 camera.open()
应该放在它自己的线程中。
怎么样:
camera.startPreview()
camera.stopPreview()
camera.release()
它没有说明他们需要哪个线程。它们必须在主线程(ui 线程)上 运行 吗?还是我可以自由选择?
我为什么要弄清楚这个? camera.startPreview()
当主线程上的 运行 导致我的应用程序短时间 jitter/lag 时,这严重影响了我的应用程序,因为它被放在 viewPager 中,我不希望让相机始终预览(这不会导致延迟,但会占用系统资源)。
有什么想法吗?
Camera 的文档指出 class 不是线程安全的,应该 不 一次从多个线程调用(我想,除非您正在执行自己的同步)。
它表示回调将传递给调用 open
来自参考文献(强调我的):
This class is not thread-safe, and is meant for use from one event thread. Most long-running operations (preview, focus, photo capture, etc) happen asynchronously and invoke callbacks as necessary. Callbacks will be invoked on the event thread open(int) was called from. This class's methods must never be called from multiple threads at once.
来自open(int)
方法参考:
Callbacks from other methods are delivered to the event loop of the thread which called open(). If this thread has no event loop, then callbacks are delivered to the main application event loop. If there is no main application event loop, callbacks are not delivered.
Caution: On some devices, this method may take a long time to complete. It is best to call this method from a worker thread (possibly using AsyncTask) to avoid blocking the main application UI thread.
它需要的线程就是您用来调用open(int)
的线程。
所以要回答你的问题,是的,你可以相对自由地选择,但你必须保持一致。