Fix fablayout measurement

This commit is contained in:
tehcneko committed 2021-05-21 11:10:24 +08:00
1 parent 7b5f82a41b
commit 87efa242d8
2 files changed
+42 -15

No files matched your search

@@ -1236,6 +1236,7 @@ public final class GalleryListScene extends BaseScene
delay = 0L;
} else {
delay = ANIMATE_TIME;
mFabLayout.setExpanded(false, true);
fab.animate().scaleX(0.0f).scaleY(0.0f).setListener(mActionFabAnimatorListener)
.setDuration(ANIMATE_TIME).setStartDelay(0L)
.setInterpolator(AnimationUtils.SLOW_FAST_INTERPOLATOR).start();
@@ -30,7 +30,6 @@ import androidx.annotation.NonNull;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.hippo.ehviewer.R;
import com.hippo.yorozuya.AnimationUtils;
import com.hippo.yorozuya.AssertUtils;
import com.hippo.yorozuya.SimpleAnimatorListener;
public class FabLayout extends ViewGroup implements View.OnClickListener {
@@ -120,27 +119,54 @@ public class FabLayout extends ViewGroup implements View.OnClickListener {
}
}
private int getChildMeasureSpec(int parentMeasureSpec) {
int parentMode = MeasureSpec.getMode(parentMeasureSpec);
int parentSize = MeasureSpec.getSize(parentMeasureSpec);
int childMode;
int childSize;
switch (parentMode) {
default:
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
childMode = MeasureSpec.AT_MOST;
childSize = parentSize;
break;
case MeasureSpec.UNSPECIFIED:
childMode = MeasureSpec.UNSPECIFIED;
childSize = parentSize;
}
return MeasureSpec.makeMeasureSpec(childSize, childMode);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
AssertUtils.assertEquals("Measure mode must be MeasureSpec.EXACTLY",
MeasureSpec.getMode(widthMeasureSpec), MeasureSpec.EXACTLY);
AssertUtils.assertEquals("Measure mode must be MeasureSpec.EXACTLY",
MeasureSpec.getMode(heightMeasureSpec), MeasureSpec.EXACTLY);
int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec);
int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec);
measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);
int maxWidth = 0;
int maxHeight = 0;
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if (child.getVisibility() == View.GONE) {
continue;
}
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
maxWidth = Math.max(maxWidth, child.getMeasuredWidth());
maxHeight = maxHeight + child.getMeasuredHeight();
}
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
width - getPaddingLeft() - getPaddingRight(), MeasureSpec.AT_MOST);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
height - getPaddingTop() - getPaddingBottom(), MeasureSpec.AT_MOST);
measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);
maxWidth += getPaddingLeft() + getPaddingRight();
maxHeight += getPaddingTop() + getPaddingBottom();
maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
setMeasuredDimension(width, height);
setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
resolveSizeAndState(maxHeight, heightMeasureSpec, 0));
}
// For pre-L, FloatActionButton use padding to show shadow, so its position looks wrong.
// We use it default size to make it position right
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int centerX = 0;