Skip to content

Commit

Permalink
Fix display metric used for scrollview snapping
Browse files Browse the repository at this point in the history
Summary:
Similarly to D29864944 (6d4fff2) we want to use `getWindowDisplayMetrics` instead of `getScreenDisplayMetrics`.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D33916223

fbshipit-source-id: cae07b1f0c1498745f28d0b9f860edcc55bde5ed
  • Loading branch information
javache authored and facebook-github-bot committed Feb 1, 2022
1 parent 10199b1 commit 79975d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
Expand Up @@ -8,13 +8,11 @@
package com.facebook.react.views.scroll;

import android.graphics.Color;
import android.util.DisplayMetrics;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.PointerEvents;
import com.facebook.react.uimanager.ReactClippingViewGroupHelper;
Expand Down Expand Up @@ -99,8 +97,8 @@ public void setDisableIntervalMomentum(
@ReactProp(name = "snapToInterval")
public void setSnapToInterval(ReactHorizontalScrollView view, float snapToInterval) {
// snapToInterval needs to be exposed as a float because of the Javascript interface.
DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics();
view.setSnapInterval((int) (snapToInterval * screenDisplayMetrics.density));
float density = PixelUtil.getDisplayMetricDensity();
view.setSnapInterval((int) (snapToInterval * density));
}

@ReactProp(name = "snapToAlignment")
Expand All @@ -116,10 +114,10 @@ public void setSnapToOffsets(
return;
}

DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics();
float density = PixelUtil.getDisplayMetricDensity();
List<Integer> offsets = new ArrayList<Integer>();
for (int i = 0; i < snapToOffsets.size(); i++) {
offsets.add((int) (snapToOffsets.getDouble(i) * screenDisplayMetrics.density));
offsets.add((int) (snapToOffsets.getDouble(i) * density));
}
view.setSnapOffsets(offsets);
}
Expand Down
Expand Up @@ -8,7 +8,6 @@
package com.facebook.react.views.scroll;

import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
Expand All @@ -17,7 +16,6 @@
import com.facebook.react.bridge.RetryableMountingLayerException;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import com.facebook.react.uimanager.PixelUtil;
import com.facebook.react.uimanager.PointerEvents;
import com.facebook.react.uimanager.ReactClippingViewGroupHelper;
Expand Down Expand Up @@ -98,8 +96,8 @@ public void setDisableIntervalMomentum(ReactScrollView view, boolean disbaleInte
@ReactProp(name = "snapToInterval")
public void setSnapToInterval(ReactScrollView view, float snapToInterval) {
// snapToInterval needs to be exposed as a float because of the Javascript interface.
DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics();
view.setSnapInterval((int) (snapToInterval * screenDisplayMetrics.density));
float density = PixelUtil.getDisplayMetricDensity();
view.setSnapInterval((int) (snapToInterval * density));
}

@ReactProp(name = "snapToOffsets")
Expand All @@ -109,10 +107,10 @@ public void setSnapToOffsets(ReactScrollView view, @Nullable ReadableArray snapT
return;
}

DisplayMetrics screenDisplayMetrics = DisplayMetricsHolder.getScreenDisplayMetrics();
float density = PixelUtil.getDisplayMetricDensity();
List<Integer> offsets = new ArrayList<Integer>();
for (int i = 0; i < snapToOffsets.size(); i++) {
offsets.add((int) (snapToOffsets.getDouble(i) * screenDisplayMetrics.density));
offsets.add((int) (snapToOffsets.getDouble(i) * density));
}
view.setSnapOffsets(offsets);
}
Expand Down

0 comments on commit 79975d1

Please sign in to comment.