java – Android相机:主题?哪个应该做什么

我试图弄清楚哪些线程应该在 Android中做什么.

我在官方文档中发现的唯一一件事是camera.open()应该放在自己的线程中.

关于什么:

> camera.startPreview()
> camera.stopPreview()
> camera.release()

它没有说明他们需要哪个线程.它们必须在主线程(ui线程)上运行吗?还是我可以自由选择?

为什么我要弄清楚这一点? camera.startPreview()在主线程上运行导致我的应用程序在短时间内抖动/滞后,这严重影响了我的应用程序,因为它放在viewPager中,我不希望让相机始终预览(这不会导致延迟,但会占用系统资源).

有任何想法吗?

解决方法

Camera文档声明该类不是线程安全的,不应该同时从多个线程调用(我想,除非您正在执行自己的同步).

它表示回调将被传递给调用打开的线程

从参考(强调我的):

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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...