Skip to content

Commit 2a2792d

Browse files
committed
[Motion] Remove context parameter from MaterialContainerTransform.
PiperOrigin-RevId: 304389543
1 parent 52cf8d9 commit 2a2792d

File tree

5 files changed

+27
-24
lines changed

5 files changed

+27
-24
lines changed

catalog/java/io/material/catalog/transition/TransitionContainerTransformDemoFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ private void configureTransitions(Fragment fragment) {
146146
}
147147

148148
private MaterialContainerTransform buildContainerTransform(boolean entering) {
149-
MaterialContainerTransform transform = new MaterialContainerTransform(requireContext());
149+
MaterialContainerTransform transform = new MaterialContainerTransform();
150150
transform.setDrawingViewId(entering ? R.id.end_root : R.id.start_root);
151151
configurationHelper.configure(transform, entering);
152152
return transform;

catalog/java/io/material/catalog/transition/TransitionContainerTransformEndDemoActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public int getDemoTitleResId() {
6666
}
6767

6868
private MaterialContainerTransform buildContainerTransform(boolean entering) {
69-
MaterialContainerTransform transform = new MaterialContainerTransform(this);
69+
MaterialContainerTransform transform = new MaterialContainerTransform();
7070
transform.addTarget(android.R.id.content);
7171
configurationHelper.configure(transform, entering);
7272
return transform;

catalog/java/io/material/catalog/transition/TransitionContainerTransformViewDemoFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private void showStartView() {
121121

122122
@NonNull
123123
private MaterialContainerTransform buildContainerTransform(boolean entering) {
124-
MaterialContainerTransform transform = new MaterialContainerTransform(requireContext());
124+
MaterialContainerTransform transform = new MaterialContainerTransform();
125125
transform.setScrimColor(Color.TRANSPARENT);
126126
configurationHelper.configure(transform, entering);
127127
return transform;

catalog/java/io/material/catalog/transition/hero/TransitionMusicAlbumDemoFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle bundle) {
118118
}
119119

120120
private void setUpTransitions() {
121-
MaterialContainerTransform transform = new MaterialContainerTransform(requireContext());
121+
MaterialContainerTransform transform = new MaterialContainerTransform();
122122
setSharedElementEnterTransition(transform);
123123
}
124124

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public class MaterialContainerTransform extends Transition {
223223
@IdRes private int startViewId = View.NO_ID;
224224
@IdRes private int endViewId = View.NO_ID;
225225
@ColorInt private int containerColor = Color.TRANSPARENT;
226-
@ColorInt private int scrimColor;
226+
@ColorInt private int scrimColor = -1;
227227
@TransitionDirection private int transitionDirection = TRANSITION_DIRECTION_AUTO;
228228
@FadeMode private int fadeMode = FADE_MODE_IN;
229229
@FitMode private int fitMode = FIT_MODE_AUTO;
@@ -236,9 +236,8 @@ public class MaterialContainerTransform extends Transition {
236236
@Nullable private ProgressThresholds scaleMaskProgressThresholds;
237237
@Nullable private ProgressThresholds shapeMaskProgressThresholds;
238238

239-
public MaterialContainerTransform(@NonNull Context context) {
239+
public MaterialContainerTransform() {
240240
setInterpolator(AnimationUtils.FAST_OUT_SLOW_IN_INTERPOLATOR);
241-
scrimColor = getDefaultScrimColor(context);
242241
}
243242

244243
/** Get the id of the View which will be used as the start shared element container. */
@@ -368,8 +367,8 @@ public int getDrawingViewId() {
368367
/**
369368
* Set the id of the View whose overlay this transition will be added to.
370369
*
371-
* <p>If {@param drawingViewId} is the same as the end View's id, MaterialContainerTransform
372-
* will add the transition's drawable to the {@param drawingViewId}'s parent instead.
370+
* <p>If {@param drawingViewId} is the same as the end View's id, MaterialContainerTransform will
371+
* add the transition's drawable to the {@param drawingViewId}'s parent instead.
373372
*/
374373
public void setDrawingViewId(@IdRes int drawingViewId) {
375374
this.drawingViewId = drawingViewId;
@@ -400,6 +399,9 @@ public void setContainerColor(@ColorInt int containerColor) {
400399
/**
401400
* Get the color to be drawn under the morphing container but within the bounds of the {@link
402401
* #getDrawingViewId()}.
402+
*
403+
* <p>If this is not set, -1 will be returned, meaning the default, R.attr.scrimBackground, will
404+
* be as the scrim color.
403405
*/
404406
@ColorInt
405407
public int getScrimColor() {
@@ -410,14 +412,14 @@ public int getScrimColor() {
410412
* Set the color to be drawn under the morphing container but within the bounds of the {@link
411413
* #getDrawingViewId()}.
412414
*
413-
* <p>By default this is set to your theme's {@link R.attr#scrimBackground}. Drawing a scrim
414-
* is primarily useful for transforming from a partial-screen View (eg. Card in a grid) to a full
415+
* <p>By default this is set to your theme's {@link R.attr#scrimBackground}. Drawing a scrim is
416+
* primarily useful for transforming from a partial-screen View (eg. Card in a grid) to a full
415417
* screen. The scrim will gradually fade in and cover the content being transformed over by the
416418
* morphing container.
417419
*
418420
* <p>Manually setting a scrim color can be useful when transitioning between two Views in a
419421
* layout, where the ending View does not cover any outgoing content (eg. a FAB to a bottom
420-
* toolbar). For scenarios such as these, set the scrim color to transparent.
422+
* toolbar). For scenarios such as these, set the scrim color to transparent (0).
421423
*/
422424
public void setScrimColor(@ColorInt int scrimColor) {
423425
this.scrimColor = scrimColor;
@@ -447,18 +449,14 @@ public void setTransitionDirection(@TransitionDirection int transitionDirection)
447449
this.transitionDirection = transitionDirection;
448450
}
449451

450-
/**
451-
* The fade mode to be used to swap the content of the start View with that of the end
452-
* View.
453-
*/
452+
/** The fade mode to be used to swap the content of the start View with that of the end View. */
454453
@FadeMode
455454
public int getFadeMode() {
456455
return fadeMode;
457456
}
458457

459458
/**
460-
* Set the fade mode to be used to swap the content of the start View with that of the end
461-
* View.
459+
* Set the fade mode to be used to swap the content of the start View with that of the end View.
462460
*
463461
* <p>By default, the fade mode is set to {@link #FADE_MODE_IN}.
464462
*
@@ -740,7 +738,7 @@ public Animator createAnimator(
740738
endBounds,
741739
endShapeAppearanceModel,
742740
containerColor,
743-
scrimColor,
741+
getScrimColorOrDefault(startView.getContext()),
744742
entering,
745743
FadeModeEvaluators.get(fadeMode, entering),
746744
FitModeEvaluators.get(fitMode, entering, startBounds, endBounds),
@@ -793,9 +791,15 @@ public void onAnimationEnd(Animator animation) {
793791
}
794792

795793
@ColorInt
796-
private static int getDefaultScrimColor(Context context) {
797-
return MaterialColors.getColor(
798-
context, R.attr.scrimBackground, ContextCompat.getColor(context, R.color.mtrl_scrim_color));
794+
private int getScrimColorOrDefault(Context context) {
795+
if (scrimColor == -1) {
796+
return MaterialColors.getColor(
797+
context,
798+
R.attr.scrimBackground,
799+
ContextCompat.getColor(context, R.color.mtrl_scrim_color));
800+
}
801+
802+
return scrimColor;
799803
}
800804

801805
private static RectF calculateDrawableBounds(
@@ -1075,8 +1079,7 @@ private void updateProgress(float progress) {
10751079
currentStartBounds,
10761080
currentStartBoundsMasked,
10771081
currentEndBoundsMasked,
1078-
progressThresholds.shapeMask
1079-
);
1082+
progressThresholds.shapeMask);
10801083

10811084
// Cross-fade images of the start/end states over range of `progress`
10821085
float fadeStartFraction = checkNotNull(progressThresholds.fade.start);

0 commit comments

Comments
 (0)