Skip to content

Commit 0e41541

Browse files
dsn5fthunterstich
authored andcommitted
[Motion] Changed MaterialContainerTransform to skip transition instead of crash when no view bounds
Resolves #1377 PiperOrigin-RevId: 319977600 (cherry picked from commit 6b63c11)
1 parent a2a238a commit 0e41541

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

lib/java/com/google/android/material/transition/MaterialContainerTransform.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
import android.graphics.drawable.Drawable;
5252
import android.os.Build.VERSION;
5353
import android.os.Build.VERSION_CODES;
54+
import androidx.core.view.ViewCompat;
55+
import android.util.Log;
56+
import android.view.View;
57+
import android.view.ViewGroup;
5458
import androidx.annotation.ColorInt;
5559
import androidx.annotation.FloatRange;
5660
import androidx.annotation.IdRes;
@@ -59,9 +63,6 @@
5963
import androidx.annotation.Nullable;
6064
import androidx.annotation.RestrictTo;
6165
import androidx.annotation.StyleRes;
62-
import androidx.core.view.ViewCompat;
63-
import android.view.View;
64-
import android.view.ViewGroup;
6566
import androidx.transition.ArcMotion;
6667
import androidx.transition.PathMotion;
6768
import androidx.transition.Transition;
@@ -181,6 +182,7 @@ public final class MaterialContainerTransform extends Transition {
181182
@Retention(RetentionPolicy.SOURCE)
182183
public @interface FitMode {}
183184

185+
private static final String TAG = MaterialContainerTransform.class.getSimpleName();
184186
private static final String PROP_BOUNDS = "materialContainerTransition:bounds";
185187
private static final String PROP_SHAPE_APPEARANCE = "materialContainerTransition:shapeAppearance";
186188
private static final String[] TRANSITION_PROPS =
@@ -866,6 +868,22 @@ public Animator createAnimator(
866868
return null;
867869
}
868870

871+
RectF startBounds = (RectF) startValues.values.get(PROP_BOUNDS);
872+
ShapeAppearanceModel startShapeAppearanceModel =
873+
(ShapeAppearanceModel) startValues.values.get(PROP_SHAPE_APPEARANCE);
874+
if (startBounds == null || startShapeAppearanceModel == null) {
875+
Log.w(TAG, "Skipping due to null start bounds. Ensure start view is laid out and measured.");
876+
return null;
877+
}
878+
879+
RectF endBounds = (RectF) endValues.values.get(PROP_BOUNDS);
880+
ShapeAppearanceModel endShapeAppearanceModel =
881+
(ShapeAppearanceModel) endValues.values.get(PROP_SHAPE_APPEARANCE);
882+
if (endBounds == null || endShapeAppearanceModel == null) {
883+
Log.w(TAG, "Skipping due to null end bounds. Ensure end view is laid out and measured.");
884+
return null;
885+
}
886+
869887
final View startView = startValues.view;
870888
final View endView = endValues.view;
871889
final View drawingView;
@@ -879,21 +897,6 @@ public Animator createAnimator(
879897
boundingView = null;
880898
}
881899

882-
RectF startBounds = (RectF) startValues.values.get(PROP_BOUNDS);
883-
if (startBounds == null) {
884-
throw new IllegalStateException(
885-
"Start view bounds must not be null, make sure the start view is laid out and measured.");
886-
}
887-
RectF endBounds = (RectF) endValues.values.get(PROP_BOUNDS);
888-
if (endBounds == null) {
889-
throw new IllegalStateException(
890-
"End view bounds must not be null, make sure the end view is laid out and measured");
891-
}
892-
ShapeAppearanceModel startShapeAppearanceModel =
893-
(ShapeAppearanceModel) startValues.values.get(PROP_SHAPE_APPEARANCE);
894-
ShapeAppearanceModel endShapeAppearanceModel =
895-
(ShapeAppearanceModel) endValues.values.get(PROP_SHAPE_APPEARANCE);
896-
897900
// Calculate drawable bounds and offset start/end bounds as needed
898901
RectF drawingViewBounds = getLocationOnScreen(drawingView);
899902
float offsetX = -drawingViewBounds.left;

lib/java/com/google/android/material/transition/platform/MaterialContainerTransform.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
import android.graphics.drawable.Drawable;
5656
import android.os.Build.VERSION;
5757
import android.os.Build.VERSION_CODES;
58+
import androidx.core.view.ViewCompat;
59+
import android.util.Log;
60+
import android.view.View;
61+
import android.view.ViewGroup;
5862
import androidx.annotation.ColorInt;
5963
import androidx.annotation.FloatRange;
6064
import androidx.annotation.IdRes;
@@ -63,9 +67,6 @@
6367
import androidx.annotation.Nullable;
6468
import androidx.annotation.RestrictTo;
6569
import androidx.annotation.StyleRes;
66-
import androidx.core.view.ViewCompat;
67-
import android.view.View;
68-
import android.view.ViewGroup;
6970
import android.transition.ArcMotion;
7071
import android.transition.PathMotion;
7172
import android.transition.Transition;
@@ -186,6 +187,7 @@ public final class MaterialContainerTransform extends Transition {
186187
@Retention(RetentionPolicy.SOURCE)
187188
public @interface FitMode {}
188189

190+
private static final String TAG = MaterialContainerTransform.class.getSimpleName();
189191
private static final String PROP_BOUNDS = "materialContainerTransition:bounds";
190192
private static final String PROP_SHAPE_APPEARANCE = "materialContainerTransition:shapeAppearance";
191193
private static final String[] TRANSITION_PROPS =
@@ -871,6 +873,22 @@ public Animator createAnimator(
871873
return null;
872874
}
873875

876+
RectF startBounds = (RectF) startValues.values.get(PROP_BOUNDS);
877+
ShapeAppearanceModel startShapeAppearanceModel =
878+
(ShapeAppearanceModel) startValues.values.get(PROP_SHAPE_APPEARANCE);
879+
if (startBounds == null || startShapeAppearanceModel == null) {
880+
Log.w(TAG, "Skipping due to null start bounds. Ensure start view is laid out and measured.");
881+
return null;
882+
}
883+
884+
RectF endBounds = (RectF) endValues.values.get(PROP_BOUNDS);
885+
ShapeAppearanceModel endShapeAppearanceModel =
886+
(ShapeAppearanceModel) endValues.values.get(PROP_SHAPE_APPEARANCE);
887+
if (endBounds == null || endShapeAppearanceModel == null) {
888+
Log.w(TAG, "Skipping due to null end bounds. Ensure end view is laid out and measured.");
889+
return null;
890+
}
891+
874892
final View startView = startValues.view;
875893
final View endView = endValues.view;
876894
final View drawingView;
@@ -884,21 +902,6 @@ public Animator createAnimator(
884902
boundingView = null;
885903
}
886904

887-
RectF startBounds = (RectF) startValues.values.get(PROP_BOUNDS);
888-
if (startBounds == null) {
889-
throw new IllegalStateException(
890-
"Start view bounds must not be null, make sure the start view is laid out and measured.");
891-
}
892-
RectF endBounds = (RectF) endValues.values.get(PROP_BOUNDS);
893-
if (endBounds == null) {
894-
throw new IllegalStateException(
895-
"End view bounds must not be null, make sure the end view is laid out and measured");
896-
}
897-
ShapeAppearanceModel startShapeAppearanceModel =
898-
(ShapeAppearanceModel) startValues.values.get(PROP_SHAPE_APPEARANCE);
899-
ShapeAppearanceModel endShapeAppearanceModel =
900-
(ShapeAppearanceModel) endValues.values.get(PROP_SHAPE_APPEARANCE);
901-
902905
// Calculate drawable bounds and offset start/end bounds as needed
903906
RectF drawingViewBounds = getLocationOnScreen(drawingView);
904907
float offsetX = -drawingViewBounds.left;

0 commit comments

Comments
 (0)