我在官方文档中发现的唯一一件事是camera.open()应该放在自己的线程中.
关于什么:
> camera.startPreview()
> camera.stopPreview()
> camera.release()
它没有说明他们需要哪个线程.它们必须在主线程(ui线程)上运行吗?还是我可以自由选择?
为什么我要弄清楚这一点? camera.startPreview()在主线程上运行导致我的应用程序在短时间内抖动/滞后,这严重影响了我的应用程序,因为它放在viewPager中,我不希望让相机始终预览(这不会导致延迟,但会占用系统资源).
有任何想法吗?
解决方法
它表示回调将被传递给调用打开的线程
从参考(强调我的):
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)的线程.
所以要回答你的问题,是的,你可以相对自由地选择,但你必须保持一致.