Merge "Update the collapsing toolbar layout" into sc-dev

This commit is contained in:
TreeHugger Robot
2021-06-02 07:59:22 +00:00
committed by Android (Google) Code Review
2 changed files with 14 additions and 33 deletions

View File

@@ -16,10 +16,12 @@
--> -->
<resources> <resources>
<!-- Collapsing toolbar layout dimensions --> <!-- Collapsing toolbar layout dimensions -->
<dimen name="toolbar_one_line_height">226dp</dimen> <dimen name="toolbar_one_line_height">216dp</dimen>
<dimen name="toolbar_two_lines_height">270dp</dimen> <dimen name="toolbar_two_lines_height">260dp</dimen>
<dimen name="toolbar_three_lines_height">314dp</dimen> <dimen name="toolbar_three_lines_height">304dp</dimen>
<dimen name="scrim_visible_height_trigger">174dp</dimen> <dimen name="scrim_visible_height_trigger">174dp</dimen>
<dimen name="scrim_visible_height_trigger_two_lines">218dp</dimen>
<dimen name="scrim_visible_height_trigger_three_lines">262dp</dimen>
<dimen name="expanded_title_margin_start">24dp</dimen> <dimen name="expanded_title_margin_start">24dp</dimen>
<dimen name="expanded_title_margin_end">24dp</dimen> <dimen name="expanded_title_margin_end">24dp</dimen>
</resources> </resources>

View File

@@ -26,12 +26,9 @@ import androidx.annotation.Nullable;
import com.google.android.material.appbar.CollapsingToolbarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/** /**
* A customized version of CollapsingToolbarLayout that can apply different font size based on * A customized version of CollapsingToolbarLayout that can apply different font size based on the
* the line count of its title. * line count of its title.
*/ */
public class AdjustableToolbarLayout extends CollapsingToolbarLayout { public class AdjustableToolbarLayout extends CollapsingToolbarLayout {
@@ -59,43 +56,25 @@ public class AdjustableToolbarLayout extends CollapsingToolbarLayout {
public void onLayoutChange(View v, int left, int top, int right, int bottom, public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) { int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this); v.removeOnLayoutChangeListener(this);
final int count = getLineCountWithReflection(); final int count = getLineCount();
if (count > TOOLBAR_MAX_LINE_NUMBER) { if (count > TOOLBAR_MAX_LINE_NUMBER) {
final ViewGroup.LayoutParams lp = getLayoutParams(); final ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = getResources() lp.height = getResources()
.getDimensionPixelSize(R.dimen.toolbar_three_lines_height); .getDimensionPixelSize(R.dimen.toolbar_three_lines_height);
setScrimVisibleHeightTrigger(
getResources().getDimensionPixelSize(
R.dimen.scrim_visible_height_trigger_three_lines));
setLayoutParams(lp); setLayoutParams(lp);
} else if (count == TOOLBAR_MAX_LINE_NUMBER) { } else if (count == TOOLBAR_MAX_LINE_NUMBER) {
final ViewGroup.LayoutParams lp = getLayoutParams(); final ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = getResources() lp.height = getResources()
.getDimensionPixelSize(R.dimen.toolbar_two_lines_height); .getDimensionPixelSize(R.dimen.toolbar_two_lines_height);
setScrimVisibleHeightTrigger(
getResources().getDimensionPixelSize(
R.dimen.scrim_visible_height_trigger_two_lines));
setLayoutParams(lp); setLayoutParams(lp);
} }
} }
}); });
} }
/**
* Returns a number of title line count for CollapsingToolbarLayout so that facilitates the
* determination to apply what kind of font size. Since the title of CollapsingToolbarLayout is
* drawn in a canvas and the text process is wrapped in a CollapsingTextHelper, the way we used
* here is to get the line count from the CollapsingTextHelper via Java Reflection.
*/
private int getLineCountWithReflection() {
try {
final Field textHelperField =
this.getClass().getSuperclass().getDeclaredField("collapsingTextHelper");
textHelperField.setAccessible(true);
final Object textHelperObj = textHelperField.get(this);
final Field layoutField = textHelperObj.getClass().getDeclaredField("textLayout");
layoutField.setAccessible(true);
final Object layoutObj = layoutField.get(textHelperObj);
final Method method = layoutObj.getClass().getDeclaredMethod("getLineCount");
return (int) method.invoke(layoutObj);
} catch (Exception e) {
return 0;
}
}
} }