您在 React Native 中有 PixelRatio 和 Dimensions 用于此目的。除此之外,您还需要使用 RN 模块 react-native-pixel-perfect,这可以让您的应用在所有设备上保持完美像素,快速轻松
import {PixelRatio, Dimensions} from 'react-native';
import {create} from 'react-native-pixel-perfect';
let displayProps = {
width: PixelRatio.roundToNearestPixel(
Dimensions.get('window').width * PixelRatio.get(),
),
height: PixelRatio.roundToNearestPixel(
Dimensions.get('window').height * PixelRatio.get(),
),
};
let perfectSize = create(displayProps);
现在始终将您的像素大小传递给此方法,以获取基于设备的原始设备像素。
const styles = StyleSheet.create({
container: {
width: perfectSize(500),
height: perfectSize(300)
}
});
您的容器将正确适应设备。基于他们的屏幕分辨率。
如果您要支持的最小高度 x 宽度,但某些设备小于最小屏幕分辨率,并且您仍希望在这些设备上获得相同的结果。然后您可以在此功能上设置最小屏幕分辨率,如下所示。
let displayProps = {
width: PixelRatio.roundToNearestPixel(
Math.max(1920, Dimensions.get('window').width * PixelRatio.get()),
),
height: PixelRatio.roundToNearestPixel(
Math.max(1080, Dimensions.get('window').height * PixelRatio.get()),
),
};
所以在我的情况下,如果屏幕分辨率小于 1920x1080,比如说 720p 设备,那么这将有助于以 1920x1080 呈现 UI。