mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
PixelRatio.pixel()
Summary: This implements #5073. It adds a static method `PixelRatio.pixel()` which returns the smallest drawable line width, primarily for use in styles. It also updates the example apps to use the new function. Closes https://github.com/facebook/react-native/pull/5076 Reviewed By: svcscm Differential Revision: D2799849 Pulled By: nicklockwood fb-gh-sync-id: b83a77790601fe882affbf65531114e7c5cf4bdf
This commit is contained in:
committed by
facebook-github-bot-5
parent
78c6e416ae
commit
cd89016ee7
@@ -16,20 +16,6 @@ var Dimensions = require('Dimensions');
|
||||
/**
|
||||
* PixelRatio class gives access to the device pixel density.
|
||||
*
|
||||
* There are a few use cases for using PixelRatio:
|
||||
*
|
||||
* ### Displaying a line that's as thin as the device permits
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* ```
|
||||
* style={{ borderWidth: 1 / PixelRatio.get() }}
|
||||
* ```
|
||||
*
|
||||
* ### Fetching a correctly sized image
|
||||
*
|
||||
* You should get a higher resolution image if you are on a high pixel density
|
||||
@@ -91,6 +77,17 @@ class PixelRatio {
|
||||
return Math.round(layoutSize * PixelRatio.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Rounds a layout size (dp) to the nearest layout size that corresponds to
|
||||
* an integer number of pixels. For example, on a device with a PixelRatio
|
||||
* of 3, `PixelRatio.roundToNearestPixel(8.4) = 8.33`, which corresponds to
|
||||
* exactly (8.33 * 3) = 25 pixels.
|
||||
*/
|
||||
static roundToNearestPixel(layoutSize: number): number {
|
||||
var ratio = PixelRatio.get();
|
||||
return Math.round(layoutSize * ratio) / ratio;
|
||||
}
|
||||
|
||||
// No-op for iOS, but used on the web. Should not be documented.
|
||||
static startDetecting() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user