react-native app 屏幕适配方案(按照设计稿像素大小写就行)
import React, { Component,PropTypes } from 'react';
import { Dimensions,PixelRatio,Platform,StatusBar,View } from 'react-native';
let props = {};
export default class Resolution {
static get(useFixWidth = true) {
return useFixWidth ? {
...props.fw
} : {
...props.fh
}
}
static setDesignSize(dwidth = 750, dheight = 1336, dim = "window") {
let designSize = {
width: dwidth,
height: dheight
};
let navHeight = Platform.OS === 'android' ? StatusBar.currentHeight : 0;
let pxRatio = PixelRatio.get(dim);
let {
width,
height
} = Dimensions.get(dim);
if (dim != "screen") height -= navHeight;
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
};
console.log("winSize", JSON.stringify({
width,
height
}))
console.log("winPixelSize", JSON.stringify({
width: w,
height: h
}))
console.log("pxRatio", pxRatio)
console.log("fixWidth", JSON.stringify(props.fw))
console.log("fixHeight", JSON.stringify(props.fh))
}
static FixWidthView = (p) => {
let {
width,
height,
scale,
navHeight
} = props.fw;
return ( <
View {
...p
}
style = {
[p.style, {
marginTop: navHeight,
width: width,
height: height,
transform: [{
translateX: -width * .5
},
{
translateY: -height * .5
},
{
scale: scale
},
{
translateX: width * .5
},
{
translateY: height * .5
}
]
}]
}
/>
);
};
static FixHeightView = (p) => {
let {
width,
height,
scale,
navHeight
} = props.fh;
return ( <
View {
...p
}
style = {
[p.style, {
marginTop: navHeight,
width: width,
height: height,
transform: [{
translateX: -width * .5
},
{
translateY: -height * .5
},
{
scale: scale
},
{
translateX: width * .5
},
{
translateY: height * .5
}
]
}]
}
/>
);
};
};
//init
Resolution.setDesignSize();
最新文章
- JavaScript学习总结(三)——this、原型、javascript面向对象
- java基础-多线程执行
- Mac OS 电脑播放 iPhone音乐
- Linux phpbb论坛的安装(中文版)
- 网页中的超链接<;a>;标签
- java webservice的多种实现方法汇总
- Android批量插入数据到SQLite数据库
- UVA 507 - Jill Rides Again 动态规划
- MYSQL设计优化
- android之wifi开发
- HTML5 3D翻书效果(双面效应)
- User Browsing Model简介
- sys.argv向脚本中传递参数
- 520. Detect Capital
- html中几种常见长度单位介绍
- Unity 用ml-agents机器学习造个游戏AI吧(1)(Windows环境配置)
- 吴恩达机器学习笔记53-高斯分布的算法(Algorithm of Gaussian Distribution)
- [物理学与PDEs]第1章第7节 媒质中的 Maxwell 方程组 7.2 媒质交界面上的条件
- MySQL没有备份怎么恢复被drop的表(利用undrop-for-innodb)
- [Python设计模式] 第28章 男人和女人——访问者模式
热门文章
- 用go写爬虫服务并发请求,限制并发数
- abstract class
- 一致性 hash 算法( consistent hashing )及java实现
- FPGA浮点数定点数的处理
- [LC] 300. Longest Increasing Subsequence
- 微信小游戏排行榜页滚动查看排行榜(canvas指定区域溢出滚动,懒渲染)
- <;SCOI2009>;粉刷匠の思路
- (二)一个很好用的自动生成工具——mybatis generator
- java中的URLEncoder和URLDecoder类;中文在地址栏中的处理
- 以后的IT路还很长(1)