[ReactNative][MAdMan] helper for GraphQL-safe pixel size calculation

This commit is contained in:
Philipp von Weitershausen 2015-04-21 09:18:51 -07:00
parent a0db658982
commit 25ae5485da
1 changed files with 13 additions and 2 deletions

View File

@ -36,8 +36,8 @@ var Dimensions = require('Dimensions');
* *
* ``` * ```
* var image = getImage({ * var image = getImage({
* width: 200 * PixelRatio.get(), * width: PixelRatio.getPixelSizeForLayoutSize(200),
* height: 100 * PixelRatio.get() * height: PixelRatio.getPixelSizeForLayoutSize(100),
* }); * });
* <Image source={image} style={{width: 200, height: 100}} /> * <Image source={image} style={{width: 200, height: 100}} />
* ``` * ```
@ -52,10 +52,21 @@ class PixelRatio {
* - iPhone 6 * - iPhone 6
* - PixelRatio.get() === 3 * - PixelRatio.get() === 3
* - iPhone 6 plus * - iPhone 6 plus
* - PixelRatio.get() === 3.5
* - Nexus 6
*/ */
static get(): number { static get(): number {
return Dimensions.get('window').scale; return Dimensions.get('window').scale;
} }
/**
* Converts a layout size (dp) to pixel size (px).
*
* Guaranteed to return an integer number.
*/
static getPixelSizeForLayoutSize(layoutSize: number): number {
return Math.round(layoutSize * PixelRatio.get());
}
} }
// No-op for iOS, but used on the web. Should not be documented. // No-op for iOS, but used on the web. Should not be documented.