StyledPlayerView: Fix calculations to account for margins

Also make some related naming improvements.

PiperOrigin-RevId: 347631916
This commit is contained in:
olly 2020-12-15 17:42:24 +00:00 committed by Christos Tsilopoulos
parent 761bd091c3
commit 7ac19ff406
3 changed files with 42 additions and 18 deletions

View file

@ -24,6 +24,7 @@ import android.content.res.Resources;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.LinearInterpolator;
import androidx.annotation.Nullable;
@ -477,9 +478,9 @@ import java.util.List;
int oldRight,
int oldBottom) {
boolean shouldBeMinimalMode = shouldBeMinimalMode();
if (isMinimalMode != shouldBeMinimalMode) {
isMinimalMode = shouldBeMinimalMode;
boolean useMinimalMode = useMinimalMode();
if (isMinimalMode != useMinimalMode) {
isMinimalMode = useMinimalMode;
v.post(this::updateLayoutForSizeChange);
}
boolean widthChanged = (right - left) != (oldRight - oldLeft);
@ -564,7 +565,7 @@ import java.util.List;
}
}
private boolean shouldBeMinimalMode() {
private boolean useMinimalMode() {
int width =
styledPlayerControlView.getWidth()
- styledPlayerControlView.getPaddingLeft()
@ -573,11 +574,14 @@ import java.util.List;
styledPlayerControlView.getHeight()
- styledPlayerControlView.getPaddingBottom()
- styledPlayerControlView.getPaddingTop();
int defaultModeWidth =
Math.max(getWidth(centerControls), getWidth(timeView) + getWidth(overflowShowButton));
int defaultModeHeight = getHeight(centerControls) + getHeight(timeBar) + getHeight(bottomBar);
int defaultModeMinimumWidth =
Math.max(
getWidthWithMargins(centerControls),
getWidthWithMargins(timeView) + getWidthWithMargins(overflowShowButton));
int defaultModeMinimumHeight =
getHeightWithMargins(centerControls) + 2 * getHeightWithMargins(bottomBar);
return (width <= defaultModeWidth || height <= defaultModeHeight);
return width <= defaultModeMinimumWidth || height <= defaultModeMinimumHeight;
}
private void updateLayoutForSizeChange() {
@ -644,7 +648,7 @@ import java.util.List;
styledPlayerControlView.getWidth()
- styledPlayerControlView.getPaddingLeft()
- styledPlayerControlView.getPaddingRight();
int bottomBarWidth = getWidth(timeView);
int bottomBarWidth = getWidthWithMargins(timeView);
for (int i = 0; i < basicControls.getChildCount(); ++i) {
bottomBarWidth += basicControls.getChildAt(i).getWidth();
}
@ -707,11 +711,29 @@ import java.util.List;
}
}
private static int getWidth(@Nullable View v) {
return (v != null ? v.getWidth() : 0);
private static int getWidthWithMargins(@Nullable View v) {
if (v == null) {
return 0;
}
int width = v.getWidth();
LayoutParams layoutParams = v.getLayoutParams();
if (layoutParams instanceof MarginLayoutParams) {
MarginLayoutParams marginLayoutParams = (MarginLayoutParams) layoutParams;
width += marginLayoutParams.leftMargin + marginLayoutParams.rightMargin;
}
return width;
}
private static int getHeight(@Nullable View v) {
return (v != null ? v.getHeight() : 0);
private static int getHeightWithMargins(@Nullable View v) {
if (v == null) {
return 0;
}
int height = v.getHeight();
LayoutParams layoutParams = v.getLayoutParams();
if (layoutParams instanceof MarginLayoutParams) {
MarginLayoutParams marginLayoutParams = (MarginLayoutParams) layoutParams;
height += marginLayoutParams.topMargin + marginLayoutParams.bottomMargin;
}
return height;
}
}

View file

@ -24,6 +24,7 @@
<FrameLayout android:id="@+id/exo_bottom_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/exo_styled_bottom_bar_height"
android:layout_marginTop="@dimen/exo_styled_bottom_bar_margin_top"
android:layout_gravity="bottom"
android:background="@color/exo_bottom_bar_background"
android:layoutDirection="ltr">
@ -31,10 +32,10 @@
<LinearLayout android:id="@+id/exo_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/exo_styled_time_padding"
android:paddingEnd="@dimen/exo_styled_time_padding"
android:paddingLeft="@dimen/exo_styled_time_padding"
android:paddingRight="@dimen/exo_styled_time_padding"
android:paddingStart="@dimen/exo_styled_bottom_bar_time_padding"
android:paddingEnd="@dimen/exo_styled_bottom_bar_time_padding"
android:paddingLeft="@dimen/exo_styled_bottom_bar_time_padding"
android:paddingRight="@dimen/exo_styled_bottom_bar_time_padding"
android:layout_gravity="center_vertical|start"
android:layoutDirection="ltr">

View file

@ -42,7 +42,8 @@
<dimen name="exo_styled_progress_touch_target_height">48dp</dimen>
<dimen name="exo_styled_progress_margin_bottom">52dp</dimen>
<dimen name="exo_styled_bottom_bar_height">60dp</dimen>
<dimen name="exo_styled_time_padding">10dp</dimen>
<dimen name="exo_styled_bottom_bar_time_padding">10dp</dimen>
<dimen name="exo_styled_bottom_bar_margin_top">10dp</dimen>
<dimen name="exo_styled_minimal_controls_margin_bottom">4dp</dimen>
<dimen name="exo_error_message_height">32dp</dimen>