Skip to content

Commit 949b057

Browse files
pekingmepaulfthomas
authored andcommitted
[FAB] Improved readability.
PiperOrigin-RevId: 735467033
1 parent db7fe20 commit 949b057

File tree

6 files changed

+27
-63
lines changed

6 files changed

+27
-63
lines changed

lib/java/com/google/android/material/floatingactionbutton/FloatingActionButton.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,34 +1046,34 @@ public void setInternalAutoHideListener(OnVisibilityChangedListener listener) {
10461046

10471047
// dereference of possibly-null reference lp
10481048
@SuppressWarnings("nullness:dereference.of.nullable")
1049-
private boolean shouldUpdateVisibility(
1049+
private boolean ignoreUpdateVisibility(
10501050
@NonNull View dependency, @NonNull FloatingActionButton child) {
10511051
final CoordinatorLayout.LayoutParams lp =
10521052
(CoordinatorLayout.LayoutParams) child.getLayoutParams();
10531053
if (!autoHideEnabled) {
1054-
return false;
1054+
return true;
10551055
}
10561056

10571057
if (lp.getAnchorId() != dependency.getId()) {
10581058
// The anchor ID doesn't match the dependency, so we won't automatically
10591059
// show/hide the FAB
1060-
return false;
1060+
return true;
10611061
}
10621062

10631063
//noinspection RedundantIfStatement
10641064
if (child.getUserSetVisibility() != VISIBLE) {
10651065
// The view isn't set to be visible so skip changing its visibility
1066-
return false;
1066+
return true;
10671067
}
10681068

1069-
return true;
1069+
return false;
10701070
}
10711071

10721072
private boolean updateFabVisibilityForAppBarLayout(
10731073
CoordinatorLayout parent,
10741074
@NonNull AppBarLayout appBarLayout,
10751075
@NonNull FloatingActionButton child) {
1076-
if (!shouldUpdateVisibility(appBarLayout, child)) {
1076+
if (ignoreUpdateVisibility(appBarLayout, child)) {
10771077
return false;
10781078
}
10791079

@@ -1099,7 +1099,7 @@ private boolean updateFabVisibilityForAppBarLayout(
10991099
@SuppressWarnings("nullness:dereference.of.nullable")
11001100
private boolean updateFabVisibilityForBottomSheet(
11011101
@NonNull View bottomSheet, @NonNull FloatingActionButton child) {
1102-
if (!shouldUpdateVisibility(bottomSheet, child)) {
1102+
if (ignoreUpdateVisibility(bottomSheet, child)) {
11031103
return false;
11041104
}
11051105
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
@@ -1164,7 +1164,7 @@ private void offsetIfNeeded(
11641164
@NonNull CoordinatorLayout parent, @NonNull FloatingActionButton fab) {
11651165
final Rect padding = fab.shadowPadding;
11661166

1167-
if (padding != null && padding.centerX() > 0 && padding.centerY() > 0) {
1167+
if (padding.centerX() > 0 && padding.centerY() > 0) {
11681168
final CoordinatorLayout.LayoutParams lp =
11691169
(CoordinatorLayout.LayoutParams) fab.getLayoutParams();
11701170

lib/java/com/google/android/material/floatingactionbutton/FloatingActionButtonImpl.java

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import android.animation.TimeInterpolator;
3232
import android.animation.TypeEvaluator;
3333
import android.animation.ValueAnimator;
34-
import android.animation.ValueAnimator.AnimatorUpdateListener;
3534
import android.content.Context;
3635
import android.content.res.ColorStateList;
3736
import android.graphics.Matrix;
@@ -675,20 +674,16 @@ private AnimatorSet createDefaultAnimator(
675674
final float startImageMatrixScale = imageMatrixScale;
676675
final Matrix matrix = new Matrix(tmpMatrix);
677676
animator.addUpdateListener(
678-
new AnimatorUpdateListener() {
679-
@Override
680-
public void onAnimationUpdate(ValueAnimator animation) {
681-
float progress = (float) animation.getAnimatedValue();
682-
// Animate the opacity over the first 20% of the animation
683-
view.setAlpha(AnimationUtils.lerp(startAlpha, targetOpacity, 0F, 0.2F, progress));
684-
view.setScaleX(AnimationUtils.lerp(startScaleX, targetScale, progress));
685-
view.setScaleY(AnimationUtils.lerp(startScaleY, targetScale, progress));
686-
imageMatrixScale =
687-
AnimationUtils.lerp(startImageMatrixScale, targetIconScale, progress);
688-
calculateImageMatrixFromScale(
689-
AnimationUtils.lerp(startImageMatrixScale, targetIconScale, progress), matrix);
690-
view.setImageMatrix(matrix);
691-
}
677+
animation -> {
678+
float progress = (float) animation.getAnimatedValue();
679+
// Animate the opacity over the first 20% of the animation
680+
view.setAlpha(AnimationUtils.lerp(startAlpha, targetOpacity, 0F, 0.2F, progress));
681+
view.setScaleX(AnimationUtils.lerp(startScaleX, targetScale, progress));
682+
view.setScaleY(AnimationUtils.lerp(startScaleY, targetScale, progress));
683+
imageMatrixScale = AnimationUtils.lerp(startImageMatrixScale, targetIconScale, progress);
684+
calculateImageMatrixFromScale(
685+
AnimationUtils.lerp(startImageMatrixScale, targetIconScale, progress), matrix);
686+
view.setImageMatrix(matrix);
692687
});
693688
animators.add(animator);
694689
AnimatorSetCompat.playTogether(set, animators);
@@ -876,37 +871,6 @@ private Animator createElevationAnimator(float elevation, float translationZ) {
876871
return set;
877872
}
878873

879-
private abstract class ShadowAnimatorImpl extends AnimatorListenerAdapter
880-
implements ValueAnimator.AnimatorUpdateListener {
881-
882-
private boolean validValues;
883-
private float shadowSizeStart;
884-
private float shadowSizeEnd;
885-
886-
@Override
887-
public void onAnimationUpdate(@NonNull ValueAnimator animator) {
888-
if (!validValues) {
889-
shadowSizeStart = shapeDrawable == null ? 0 : shapeDrawable.getElevation();
890-
shadowSizeEnd = getTargetShadowSize();
891-
validValues = true;
892-
}
893-
894-
updateShapeElevation(
895-
(int)
896-
(shadowSizeStart
897-
+ ((shadowSizeEnd - shadowSizeStart) * animator.getAnimatedFraction())));
898-
}
899-
900-
@Override
901-
public void onAnimationEnd(Animator animator) {
902-
updateShapeElevation((int) shadowSizeEnd);
903-
validValues = false;
904-
}
905-
906-
/** Returns the shadow size we want to animate to. */
907-
protected abstract float getTargetShadowSize();
908-
}
909-
910874
private boolean shouldAnimateVisibilityChange() {
911875
return view.isLaidOut() && !view.isInEditMode();
912876
}

lib/java/com/google/android/material/transformation/FabTransformationBehavior.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ private void createExpansionAnimation(
374374

375375
float revealCenterX = calculateRevealCenterX(dependency, child, spec.positioning);
376376
float revealCenterY = calculateRevealCenterY(dependency, child, spec.positioning);
377-
((FloatingActionButton) dependency).getContentRect(tmpRect);
377+
((FloatingActionButton) dependency).getMeasuredContentRect(tmpRect);
378378
float dependencyRadius = tmpRect.width() / 2f;
379379

380380
Animator animator;

tests/javatests/com/google/android/material/appbar/AppBarWithDodgingTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public void testLeftDodge() throws Throwable {
4343

4444
final Rect fabRect = new Rect();
4545
final Rect fab2Rect = new Rect();
46-
fab.getContentRect(fabRect);
47-
fab2.getContentRect(fab2Rect);
46+
fab.getMeasuredContentRect(fabRect);
47+
fab2.getMeasuredContentRect(fab2Rect);
4848

4949
// Our second FAB is configured to "dodge" the first one - to be displayed to the
5050
// right of it
@@ -72,8 +72,8 @@ public void testRightDodge() throws Throwable {
7272

7373
final Rect fabRect = new Rect();
7474
final Rect fab2Rect = new Rect();
75-
fab.getContentRect(fabRect);
76-
fab2.getContentRect(fab2Rect);
75+
fab.getMeasuredContentRect(fabRect);
76+
fab2.getMeasuredContentRect(fab2Rect);
7777

7878
// Our second FAB is configured to "dodge" the first one - to be displayed to the
7979
// left of it

tests/javatests/com/google/android/material/floatingactionbutton/FloatingActionButtonTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public void testClickableTouchAndDragOffView() {
236236
final int[] xy = new int[2];
237237
fab.getLocationOnScreen(xy);
238238
final Rect rect = new Rect();
239-
fab.getContentRect(rect);
239+
fab.getMeasuredContentRect(rect);
240240

241241
return new float[] {xy[0] + rect.centerX(), xy[1] + rect.centerY()};
242242
},

tests/javatests/com/google/android/material/testutils/TestUtilsMatchers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public boolean matchesSafely(final View view) {
330330
// Since the FAB background is round, and may contain the shadow, we'll look at
331331
// just the center half rect of the content area
332332
final Rect area = new Rect();
333-
fab.getContentRect(area);
333+
fab.getMeasuredContentRect(area);
334334

335335
final int rectHeightQuarter = area.height() / 4;
336336
final int rectWidthQuarter = area.width() / 4;
@@ -404,7 +404,7 @@ public boolean matchesSafely(final View view) {
404404

405405
final FloatingActionButton fab = (FloatingActionButton) view;
406406
final Rect area = new Rect();
407-
fab.getContentRect(area);
407+
fab.getMeasuredContentRect(area);
408408

409409
if (area.height() != size) {
410410
failedCheckDescription =
@@ -439,7 +439,7 @@ public boolean matchesSafely(final View view) {
439439
final ViewGroup parent = (ViewGroup) view.getParent();
440440

441441
final Rect area = new Rect();
442-
fab.getContentRect(area);
442+
fab.getMeasuredContentRect(area);
443443

444444
final int absGravity =
445445
GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(view));

0 commit comments

Comments
 (0)