Skip to content

Commit

Permalink
Fix android view dimensions
Browse files Browse the repository at this point in the history
Summary:
This diff fixes the Android View dimensions in VR

PixelUtil.toSPFromPixel and PixelUtil.getDisplayMetricDensity() are both using getScreenDisplayMetrics() to perform conversion of dimensions. This is not correct because we should take into consideration the density of the Context / Activity instead of the Screen. This problem didn't raise before in Fabric Android because it seems that android OS on phones usually share the scale between the screen and the Activity?

These two methods are only used in Fabric and they were introduced by D9583972 (5c0da01) and D9173758 (8b5e3fc)

As part of this diff I'm also deleting the method toSPFromPixel in favor of toDIPFromPixel because I noticed the usages of these methods are meant to use toDIPFromPixel()

changelog: [Internal] internal

Reviewed By: JoshuaGross

Differential Revision: D29864944

fbshipit-source-id: a0a093c120bde21a6cf9e1043a83c31e870d4368
  • Loading branch information
mdvacca authored and facebook-github-bot committed Jul 28, 2021
1 parent eb38543 commit 6d4fff2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public static float toPixelFromDIP(double value) {
return toPixelFromDIP((float) value);
}

/** Convert from PX to SP */
public static float toSPFromPixel(float value) {
return value / DisplayMetricsHolder.getScreenDisplayMetrics().scaledDensity;
}

/** Convert from SP to PX */
public static float toPixelFromSP(float value) {
return toPixelFromSP(value, Float.NaN);
Expand Down Expand Up @@ -58,6 +53,6 @@ public static float toDIPFromPixel(float value) {

/** @return {@link float} that represents the density of the display metrics for device screen. */
public static float getDisplayMetricDensity() {
return DisplayMetricsHolder.getScreenDisplayMetrics().density;
return DisplayMetricsHolder.getWindowDisplayMetrics().density;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -493,16 +493,16 @@ public static long measureText(

// The attachment array returns the positions of each of the attachments as
attachmentsPositions[attachmentPosition] =
PixelUtil.toSPFromPixel(placeholderTopPosition);
PixelUtil.toDIPFromPixel(placeholderTopPosition);
attachmentsPositions[attachmentPosition + 1] =
PixelUtil.toSPFromPixel(placeholderLeftPosition);
PixelUtil.toDIPFromPixel(placeholderLeftPosition);
attachmentIndex++;
}
}
}

float widthInSP = PixelUtil.toSPFromPixel(calculatedWidth);
float heightInSP = PixelUtil.toSPFromPixel(calculatedHeight);
float widthInSP = PixelUtil.toDIPFromPixel(calculatedWidth);
float heightInSP = PixelUtil.toDIPFromPixel(calculatedHeight);

if (ENABLE_MEASURE_LOGGING) {
FLog.e(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,16 +514,16 @@ public static long measureText(

// The attachment array returns the positions of each of the attachments as
attachmentsPositions[attachmentPosition] =
PixelUtil.toSPFromPixel(placeholderTopPosition);
PixelUtil.toDIPFromPixel(placeholderTopPosition);
attachmentsPositions[attachmentPosition + 1] =
PixelUtil.toSPFromPixel(placeholderLeftPosition);
PixelUtil.toDIPFromPixel(placeholderLeftPosition);
attachmentIndex++;
}
}
}

float widthInSP = PixelUtil.toSPFromPixel(calculatedWidth);
float heightInSP = PixelUtil.toSPFromPixel(calculatedHeight);
float widthInSP = PixelUtil.toDIPFromPixel(calculatedWidth);
float heightInSP = PixelUtil.toDIPFromPixel(calculatedHeight);

if (ENABLE_MEASURE_LOGGING) {
FLog.e(
Expand Down

0 comments on commit 6d4fff2

Please sign in to comment.