android - React Native - 方向锁定;我还能得到传感器的方向吗?
问题描述
我正在开发一个旨在以纵向模式运行的 React Native 应用程序。方向通过清单锁定:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustResize"
android:screenOrientation="portrait">
我需要添加拍摄我使用过的照片的功能react-native-camera
。
问题是,我无法在拍照时改变方向,因为它被锁定为纵向。
有没有办法允许在特定视图(即相机容器)中改变方向,以便相机可以检测到正确的方向?
我在想也许以下是可能的:
- 直接从传感器请求方向
- 没有锁定方向的新活动
解决方案
我遇到过同样的问题。我使用了 React-Native-Orientation:https ://github.com/yamill/react-native-orientation 。
您不应该在清单中锁定方向,而是可以使用此库将每个屏幕锁定为纵向模式。采用lockToPortrait()
对于您需要旋转的屏幕,只需使用 unlockAllOrientations 或lockToLandscape()
API 很棒,但问题是你需要为每个屏幕都做这件事。如果你有很多屏幕,这似乎有点矫枉过正。但效果很好
附加提示
在每个屏幕上都这样做会很忙而且很难管理。我所做的是将我的导航功能模块化为一个类。一世
import Orientation from 'react-native-orientation';
export const navigateToMainScreen=()=>{
determineOrientation("Main");
this.props.navigation.navigate("Main");
}
const determineOrientation=(screenName)=>{
// ur logic
if(true){
Orientation.lockToPortrait();
}
else{
Orientation.lockToLandscape();
}
}
推荐阅读
- c# - UIDocumentInteractionController 总是创建一个副本
- java - 如何使用 spring 注释将一个 POJO 对象注入另一个 POJO?
- vue.js - Vue嵌套路由不包含动态参数
- directx - 我可以在 2 个 D3D11Device 之间共享 ArraySize > 1 的纹理 2D 吗?
- regex - 同时对 /^Subject 和 /^From 进行正则表达式
- elasticsearch - 重新索引 Elasticsearch 将 unixtime 转换为日期
- powerbi - 计算两年之间金额的差异(差异)
- python - Bonobo ETL:如何在执行期间为所有节点提供全局上下文?
- c# - 如何解析div,因为我想要h3标签内的超链接
- javascript - net::ERR_CONNECTION_REFUSED 使用 websockets