微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)的线程.

所以要回答你的问题,是的,你可以相对自由地选择,但你必须保持一致.

原文地址:https://www.jb51.cc/android/129639.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐