mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
8bd3edec88
Reviewed By: aaronabramov Differential Revision: D33367752 fbshipit-source-id: 4ce94d184485e5ee0a62cf67ad2d3ba16e285c8f
59 lines
1.8 KiB
Java
59 lines
1.8 KiB
Java
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
package com.facebook.react.uimanager;
|
|
|
|
import android.util.DisplayMetrics;
|
|
import android.util.TypedValue;
|
|
|
|
/** Android dp to pixel manipulation */
|
|
public class PixelUtil {
|
|
|
|
/** Convert from DIP to PX */
|
|
public static float toPixelFromDIP(float value) {
|
|
return TypedValue.applyDimension(
|
|
TypedValue.COMPLEX_UNIT_DIP, value, DisplayMetricsHolder.getWindowDisplayMetrics());
|
|
}
|
|
|
|
/** Convert from DIP to PX */
|
|
public static float toPixelFromDIP(double value) {
|
|
return toPixelFromDIP((float) value);
|
|
}
|
|
|
|
/** Convert from SP to PX */
|
|
public static float toPixelFromSP(float value) {
|
|
return toPixelFromSP(value, Float.NaN);
|
|
}
|
|
|
|
/** Convert from SP to PX */
|
|
public static float toPixelFromSP(float value, float maxFontScale) {
|
|
DisplayMetrics displayMetrics = DisplayMetricsHolder.getWindowDisplayMetrics();
|
|
float scaledDensity = displayMetrics.scaledDensity;
|
|
float currentFontScale = scaledDensity / displayMetrics.density;
|
|
if (maxFontScale >= 1 && maxFontScale < currentFontScale) {
|
|
scaledDensity = displayMetrics.density * maxFontScale;
|
|
}
|
|
|
|
return value * scaledDensity;
|
|
}
|
|
|
|
/** Convert from SP to PX */
|
|
public static float toPixelFromSP(double value) {
|
|
return toPixelFromSP((float) value);
|
|
}
|
|
|
|
/** Convert from PX to DP */
|
|
public static float toDIPFromPixel(float value) {
|
|
return value / DisplayMetricsHolder.getWindowDisplayMetrics().density;
|
|
}
|
|
|
|
/** @return {@link float} that represents the density of the display metrics for device screen. */
|
|
public static float getDisplayMetricDensity() {
|
|
return DisplayMetricsHolder.getWindowDisplayMetrics().density;
|
|
}
|
|
}
|