我对本机反应有点陌生。我想让我的UI对所有类型的设备都具有响应能力,就像在Web开发中使用“ SCSS”时,我们曾经使用vw使字体具有响应能力。
但是问题是我们不能在react-native中使用Viewport。
现在,我的设计在“ dp”中有字体。我在寻找响应式解决方案。
因此他们回答说我们可以使用PixelRatio
来获得“ dpi”比率。
我们可以使用此公式计算像素
px = dp * PixelRatio
。
但是随着dpi值的增加,字体大小会变得太大或太小。
我希望不使用任何库即可使UI响应。请给我建议一些方法。 将“ dp”转换为自适应UI像素。
谢谢。
答案 0 :(得分:0)
答案 1 :(得分:0)
希望这会有所帮助!
信用:Github repo
// packages
import { Dimensions, PixelRatio } from 'react-native';
// Retrieve initial screen's width
let screenWidth = Dimensions.get('window').width;
// Retrieve initial screen's height
let screenHeight = Dimensions.get('window').height;
/**
* Converts provided width percentage to independent pixel (dp).
* @param {string} widthPercent The percentage of screen's width that UI element should cover
* along with the percentage symbol (%).
* @return {number} The calculated dp depending on current device's screen width.
*/
const widthPercentageToDP = widthPercent => {
// Parse string percentage input and convert it to number.
const elemWidth = parseFloat(widthPercent);
// Use PixelRatio.roundToNearestPixel method in order to round the layout
// size (dp) to the nearest one that correspons to an integer number of pixels.
return PixelRatio.roundToNearestPixel(screenWidth * elemWidth / 100);
};
/**
* Converts provided height percentage to independent pixel (dp).
* @param {string} heightPercent The percentage of screen's height that UI element should cover
* along with the percentage symbol (%).
* @return {number} The calculated dp depending on current device's screen height.
*/
const heightPercentageToDP = heightPercent => {
// Parse string percentage input and convert it to number.
const elemHeight = parseFloat(heightPercent);
// Use PixelRatio.roundToNearestPixel method in order to round the layout
// size (dp) to the nearest one that correspons to an integer number of pixels.
return PixelRatio.roundToNearestPixel(screenHeight * elemHeight / 100);
};
/**
* Event listener function that detects orientation change (every time it occurs) and triggers
* screen rerendering. It does that, by changing the state of the screen where the function is
* called. State changing occurs for a new state variable with the name 'orientation' that will
* always hold the current value of the orientation after the 1st orientation change.
* Invoke it inside the screen's constructor or in componentDidMount lifecycle method.
* @param {object} that Screen's class component this variable. The function needs it to
* invoke setState method and trigger screen rerender (this.setState()).
*/
const listenOrientationChange = that => {
Dimensions.addEventListener('change', newDimensions => {
// Retrieve and save new dimensions
screenWidth = newDimensions.window.width;
screenHeight = newDimensions.window.height;
// Trigger screen's rerender with a state update of the orientation variable
that.setState({
orientation: screenWidth < screenHeight ? 'portrait' : 'landscape'
});
});
};
/**
* Wrapper function that removes orientation change listener and should be invoked in
* componentWillUnmount lifecycle method of every class component (UI screen) that
* listenOrientationChange function has been invoked. This should be done in order to
* avoid adding new listeners every time the same component is re-mounted.
*/
const removeOrientationListener = () => {
Dimensions.removeEventListener('change', () => {});
};
export {
widthPercentageToDP,
heightPercentageToDP,
listenOrientationChange,
removeOrientationListener
};
//In your component
import { widthPercentageToDP as wp, heightPercentageToDP as hp } from '../resposive-helper';
//Styles
shadowOffset: { width: wp(1), height: hp(1) },
borderRadius: wp(50),
paddingTop: hp(1.5),
paddingBottom: hp(1.5),
paddingLeft: wp(2.5),
paddingRight: wp(2.5),