RN屏幕的适配问题

import React, {Component, PropTypes} from 'react';
import {
    Dimensions,
    PixelRatio,
    Platform,
    StatusBar,
    View
} from 'react-native';
//React Native中使用的尺寸单位是dp(一种基于屏幕密度的抽象单位。在每英寸160点的显示器上,1dp = 1px),而设计师使用的是px,
let props = {};
export default class Resolution {
    static get(useFixWidth = true){
        return useFixWidth?{...props.fw}:{...props.fh}
    }

    static setDesignSize(dwidth=1080,dheight=1920,dim="window"){
        //获取设计图宽高
        let designSize = {width:dwidth,height:dheight};

        let navHeight = Platform.OS === 'android' ? StatusBar.currentHeight : 64;
        let pxRatio = PixelRatio.get(dim);
        let {width,height} = Dimensions.get(dim);
        if(dim != "screen")height-=navHeight;
        //将一个布局尺寸(dp)转换为像素尺寸(px)。
        let w = PixelRatio.getPixelSizeForLayoutSize(width);
        let h = PixelRatio.getPixelSizeForLayoutSize(height);

        let fw_design_scale = designSize.width/w;
        fw_width = designSize.width;
        fw_height = h*fw_design_scale;
        fw_scale = 1/pxRatio/fw_design_scale;

        let fh_design_scale = designSize.height/h;
        fh_width = w*fh_design_scale;
        fh_height = designSize.height;
        fh_scale = 1/pxRatio/fh_design_scale;

        props.fw = {width:fw_width,height:fw_height,scale:fw_scale,navHeight};
        props.fh = {width:fh_width,height:fh_height,scale:fh_scale,navHeight};
    }

    static FixWidthView = (p) => {
        let {width,height,scale,navHeight} = props.fw;
        return (
            <View {...p} style={{
                                            marginTop:navHeight,
                                            width:width,
                                            height:height,
                                            backgroundColor: 'transparent',
                                            transform:[{translateX:-width*.5},
                                                        {translateY:-height*.5},
                                                        {scale:scale},
                                                        {translateX:width*.5},
                                                        {translateY:height*.5}]
                                        }}>
            </View>
        );
    };

    static FixHeightView = (p) => {
        let {width,height,scale,navHeight} = props.fh;
        return (
            <View {...p} style={{
                                            marginTop:navHeight,
                                            width:width,
                                            height:height,
                                            backgroundColor: 'transparent',
                                            transform:[{translateX:-width*.5},
                                                        {translateY:-height*.5},
                                                        {scale:scale},
                                                        {translateX:width*.5},
                                                        {translateY:height*.5}]
                                        }}>
                {p.children}
            </View>
        );
    };
};
//init
Resolution.setDesignSize(1080,1920);

在改一改Navigator的样式就可以了、使用的时候改下设计图尺寸
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值