Skip to content

Commit

Permalink
[Transition] Update MaterialContainerTransform to use getLocationInWi…
Browse files Browse the repository at this point in the history
…ndow() instead of getLocationOnScreen() when calculating bounds

Fixes issue where start / end view jump during transition in landscape mode when there is an artificial inset due to the camera hole / other display cutouts

PiperOrigin-RevId: 631421213
  • Loading branch information
dsn5ft committed May 7, 2024
1 parent 41eb087 commit 0429d44
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static com.google.android.material.transition.TransitionUtils.defaultIfNull;
import static com.google.android.material.transition.TransitionUtils.findAncestorById;
import static com.google.android.material.transition.TransitionUtils.findDescendantOrAncestorById;
import static com.google.android.material.transition.TransitionUtils.getLocationOnScreen;
import static com.google.android.material.transition.TransitionUtils.getLocationInWindow;
import static com.google.android.material.transition.TransitionUtils.getRelativeBounds;
import static com.google.android.material.transition.TransitionUtils.lerp;
import static com.google.android.material.transition.TransitionUtils.transform;
Expand Down Expand Up @@ -828,7 +828,7 @@ private static void captureValues(

if (ViewCompat.isLaidOut(view) || view.getWidth() != 0 || view.getHeight() != 0) {
// Capture location in screen co-ordinates
RectF bounds = view.getParent() == null ? getRelativeBounds(view) : getLocationOnScreen(view);
RectF bounds = view.getParent() == null ? getRelativeBounds(view) : getLocationInWindow(view);
transitionValues.values.put(PROP_BOUNDS, bounds);
transitionValues.values.put(
PROP_SHAPE_APPEARANCE,
Expand Down Expand Up @@ -919,7 +919,7 @@ public Animator createAnimator(
}

// Calculate drawable bounds and offset start/end bounds as needed
RectF drawingViewBounds = getLocationOnScreen(drawingView);
RectF drawingViewBounds = getLocationInWindow(drawingView);
float offsetX = -drawingViewBounds.left;
float offsetY = -drawingViewBounds.top;
RectF drawableBounds = calculateDrawableBounds(drawingView, boundingView, offsetX, offsetY);
Expand Down Expand Up @@ -1023,7 +1023,7 @@ private static float getElevationOrDefault(float elevation, View view) {
private static RectF calculateDrawableBounds(
View drawingView, @Nullable View boundingView, float offsetX, float offsetY) {
if (boundingView != null) {
RectF drawableBounds = getLocationOnScreen(boundingView);
RectF drawableBounds = getLocationInWindow(boundingView);
drawableBounds.offset(offsetX, offsetY);
return drawableBounds;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,9 @@ static Rect getRelativeBoundsRect(View view) {
return new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
}

static RectF getLocationOnScreen(View view) {
static RectF getLocationInWindow(View view) {
int[] location = new int[2];
view.getLocationOnScreen(location);
view.getLocationInWindow(location);
int left = location[0];
int top = location[1];
int right = left + view.getWidth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static com.google.android.material.transition.platform.TransitionUtils.defaultIfNull;
import static com.google.android.material.transition.platform.TransitionUtils.findAncestorById;
import static com.google.android.material.transition.platform.TransitionUtils.findDescendantOrAncestorById;
import static com.google.android.material.transition.platform.TransitionUtils.getLocationOnScreen;
import static com.google.android.material.transition.platform.TransitionUtils.getLocationInWindow;
import static com.google.android.material.transition.platform.TransitionUtils.getRelativeBounds;
import static com.google.android.material.transition.platform.TransitionUtils.lerp;
import static com.google.android.material.transition.platform.TransitionUtils.transform;
Expand Down Expand Up @@ -833,7 +833,7 @@ private static void captureValues(

if (ViewCompat.isLaidOut(view) || view.getWidth() != 0 || view.getHeight() != 0) {
// Capture location in screen co-ordinates
RectF bounds = view.getParent() == null ? getRelativeBounds(view) : getLocationOnScreen(view);
RectF bounds = view.getParent() == null ? getRelativeBounds(view) : getLocationInWindow(view);
transitionValues.values.put(PROP_BOUNDS, bounds);
transitionValues.values.put(
PROP_SHAPE_APPEARANCE,
Expand Down Expand Up @@ -924,7 +924,7 @@ public Animator createAnimator(
}

// Calculate drawable bounds and offset start/end bounds as needed
RectF drawingViewBounds = getLocationOnScreen(drawingView);
RectF drawingViewBounds = getLocationInWindow(drawingView);
float offsetX = -drawingViewBounds.left;
float offsetY = -drawingViewBounds.top;
RectF drawableBounds = calculateDrawableBounds(drawingView, boundingView, offsetX, offsetY);
Expand Down Expand Up @@ -1028,7 +1028,7 @@ private static float getElevationOrDefault(float elevation, View view) {
private static RectF calculateDrawableBounds(
View drawingView, @Nullable View boundingView, float offsetX, float offsetY) {
if (boundingView != null) {
RectF drawableBounds = getLocationOnScreen(boundingView);
RectF drawableBounds = getLocationInWindow(boundingView);
drawableBounds.offset(offsetX, offsetY);
return drawableBounds;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ static Rect getRelativeBoundsRect(View view) {
return new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
}

static RectF getLocationOnScreen(View view) {
static RectF getLocationInWindow(View view) {
int[] location = new int[2];
view.getLocationOnScreen(location);
view.getLocationInWindow(location);
int left = location[0];
int top = location[1];
int right = left + view.getWidth();
Expand Down

0 comments on commit 0429d44

Please sign in to comment.