zoukankan      html  css  js  c++  java
  • ReactNative: 使用像素密度类PixelRatio进行适配

    一、简介

    现在设备显示屏的清晰度越来越高,尤其是iOS移动设备上的高清适配,Retina显示屏。在开发中,为了保证图片在不同的设备上显示的效果保持一致,往往需要准备多套图片,比如iOS开发中的@1x,@2x,@3x图,这是一件比较繁琐的事。在RN中,针对这个情况提供了一种新的解决方案,由于RN中的pt单位是统一的,所以通过像素密度来获取图片在不同设备上需要的真正大小。PixelRatio类提供的都是静态的方法,直接类名调用即可。PixelRatio类提供了一些默认的像素密度如下:

    像素密度             适用设备
    PixelRatio.get() === 1 mdpi Android devices (160 dpi) PixelRatio.get() === 1.5 hdpi Android devices (240 dpi) PixelRatio.get() === 2 iPhone 4, 4S, 5, 5c, 5s, 6, xhdpi Android devices (320 dpi) PixelRatio.get() === 3 iPhone 6 plus、xxhdpi Android devices (480 dpi) PixelRatio.get() === 3.5 Nexus 6

    二、API

    //获取像素密度
    static get(): number {
      return Dimensions.get('window').scale;
    }
    
    //获取字体比例,目前仅支持安卓,iOS默认还是使用像素密度
    static getFontScale(): number {
      return Dimensions.get('window').fontScale || PixelRatio.get();
    }
    
    //获取一个布局元素的真实像素大小,返回值是一个四舍五入的整型
    static getPixelSizeForLayoutSize(layoutSize: number): number {
      return Math.round(layoutSize * PixelRatio.get());
    }
    
    //将布局尺寸舍入到与整数像素数相对应的最接近的布局尺寸
    //例如:on a device with a PixelRatioof 3,PixelRatio.roundToNearestPixel(8.4) = 8.33,exactly (8.33 * 3) = 25 pixels
    static roundToNearestPixel(layoutSize: number): number {
      var ratio = PixelRatio.get();
      return Math.round(layoutSize * ratio) / ratio;
    }
    
    //仅支持web,开始检测
    static startDetecting() {}

    三、使用

    /**
     * Sample React Native App
     * https://github.com/facebook/react-native
     * @flow
     */
    
    import React, { Component } from 'react';
    
    import {
        AppRegistry,
        StyleSheet,
        View,
        PixelRatio,
        Image
    } from 'react-native';
    
    
    export default class ReactNativeDemo extends Component {
    
        render() {
            return (
                <View style={styles.flex}>
                    <View style={styles.view1}>
                        <Image style={styles.image1} source={{uri:'car1.png'}}/>
                    </View>
                    <View style={styles.view2}>
                        <Image style={styles.image2} source={{uri:'car1.png'}}/>
                    </View>
                </View>
            );
        }
    }
    
    const styles = StyleSheet.create({
        flex: {
            flex: 1,
            alignItems: 'center'
        },
        view1: {
            marginTop: 50,
            200,
            height:200,
            borderColor:'red',
            borderWidth: 5,                   ///未适配的线宽
            borderRadius: 4,
            alignItems: 'center',
            justifyContent: 'center'
        },
        view2: {
            marginTop: 30,
            200,
            height:200,
            borderColor:'red',
            borderWidth: 5/PixelRatio.get(),  ///适配后的线宽
            borderRadius: 4,
            alignItems: 'center',
            justifyContent: 'center'
        },
        image1: {
             50,                       ///未适配的图宽(假设图片的原始宽度)
            height: 60                       ///未适配的图高(假设图片的原始高度)
        },
        image2: {
             PixelRatio.getPixelSizeForLayoutSize(50),      ///适配后的图宽
            height: PixelRatio.getPixelSizeForLayoutSize(60)      ///适配后的图高
        }
    });
    
    AppRegistry.registerComponent('ReactNativeDemo', () => ReactNativeDemo);

    可以发现:采用当前设备的像素密度适配后,边框线宽和图片的尺寸都变的精细了。 

     

  • 相关阅读:
    用node.js从零开始去写一个简单的爬虫
    如何在手机上查看测试vue-cli构建的项目
    【干货】听说你想成为一名6的飞起的黑客,这些资料怎么能少
    用node.js从零开始去写一个简单的爬虫
    如何在手机上查看测试vue-cli构建的项目
    一个好用的在线微信二维码设计网站
    ES6-----学习系列十五(Iterator)
    js学习总结----js中常用的四种输出方式
    js学习总结----javascript引入到页面的方式和细节
    ES6-----学习系列十四(promise)
  • 原文地址:https://www.cnblogs.com/XYQ-208910/p/12040155.html
Copyright © 2011-2022 走看看