2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* @providesModule PixelRatio
|
2015-03-24 00:09:14 +00:00
|
|
|
* @flow
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Dimensions = require('Dimensions');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PixelRatio class gives access to the device pixel density.
|
|
|
|
*
|
|
|
|
* There are a few use cases for using PixelRatio:
|
|
|
|
*
|
2015-03-16 16:37:45 +00:00
|
|
|
* ### Displaying a line that's as thin as the device permits
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-07-27 16:25:19 +00:00
|
|
|
* A width of 1 is actually pretty thick on devices with high pixel density
|
|
|
|
* (such as iPhone 4+ and many Android devices), we can make one that's
|
|
|
|
* thinner using a width of `1 / PixelRatio.get()`.
|
|
|
|
* It's a technique that works on all the devices independent of their
|
|
|
|
* pixel density.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-03-16 16:37:45 +00:00
|
|
|
* ```
|
|
|
|
* style={{ borderWidth: 1 / PixelRatio.get() }}
|
|
|
|
* ```
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2015-03-16 16:37:45 +00:00
|
|
|
* ### Fetching a correctly sized image
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* You should get a higher resolution image if you are on a high pixel density
|
|
|
|
* device. A good rule of thumb is to multiply the size of the image you display
|
|
|
|
* by the pixel ratio.
|
|
|
|
*
|
2015-03-16 16:37:45 +00:00
|
|
|
* ```
|
|
|
|
* var image = getImage({
|
2015-04-21 16:18:51 +00:00
|
|
|
* width: PixelRatio.getPixelSizeForLayoutSize(200),
|
|
|
|
* height: PixelRatio.getPixelSizeForLayoutSize(100),
|
2015-03-16 16:37:45 +00:00
|
|
|
* });
|
|
|
|
* <Image source={image} style={{width: 200, height: 100}} />
|
|
|
|
* ```
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
|
|
|
class PixelRatio {
|
2015-03-16 16:37:45 +00:00
|
|
|
/**
|
|
|
|
* Returns the device pixel density. Some examples:
|
|
|
|
*
|
2015-07-27 16:25:19 +00:00
|
|
|
* - PixelRatio.get() === 1
|
|
|
|
* - mdpi Android devices (160 dpi)
|
|
|
|
* - PixelRatio.get() === 1.5
|
|
|
|
* - hdpi Android devices (240 dpi)
|
2015-03-16 16:37:45 +00:00
|
|
|
* - PixelRatio.get() === 2
|
|
|
|
* - iPhone 4, 4S
|
|
|
|
* - iPhone 5, 5c, 5s
|
|
|
|
* - iPhone 6
|
2015-07-27 16:25:19 +00:00
|
|
|
* - xhdpi Android devices (320 dpi)
|
2015-03-16 16:37:45 +00:00
|
|
|
* - PixelRatio.get() === 3
|
|
|
|
* - iPhone 6 plus
|
2015-07-27 16:25:19 +00:00
|
|
|
* - xxhdpi Android devices (480 dpi)
|
2015-04-21 16:18:51 +00:00
|
|
|
* - PixelRatio.get() === 3.5
|
|
|
|
* - Nexus 6
|
2015-03-16 16:37:45 +00:00
|
|
|
*/
|
2015-03-24 00:09:14 +00:00
|
|
|
static get(): number {
|
2015-02-20 04:10:52 +00:00
|
|
|
return Dimensions.get('window').scale;
|
|
|
|
}
|
2015-04-21 16:18:51 +00:00
|
|
|
|
2015-06-01 17:19:25 +00:00
|
|
|
/**
|
|
|
|
* Returns the scaling factor for font sizes. This is the ratio that is used to calculate the
|
|
|
|
* absolute font size, so any elements that heavily depend on that should use this to do
|
|
|
|
* calculations.
|
|
|
|
*
|
|
|
|
* If a font scale is not set, this returns the device pixel ratio.
|
|
|
|
*
|
|
|
|
* Currently this is only implemented on Android and reflects the user preference set in
|
|
|
|
* Settings > Display > Font size, on iOS it will always return the default pixel ratio.
|
2015-07-27 16:25:19 +00:00
|
|
|
* @platform android
|
2015-06-01 17:19:25 +00:00
|
|
|
*/
|
|
|
|
static getFontScale(): number {
|
|
|
|
return Dimensions.get('window').fontScale || PixelRatio.get();
|
|
|
|
}
|
|
|
|
|
2015-04-21 16:18:51 +00:00
|
|
|
/**
|
|
|
|
* 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());
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-05-12 21:22:22 +00:00
|
|
|
// No-op for iOS, but used on the web. Should not be documented.
|
|
|
|
static startDetecting() {}
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
module.exports = PixelRatio;
|