Skip to content

Commit

Permalink
[CollapsingToolbarLayout] Fix issue with multiline title text flicker…
Browse files Browse the repository at this point in the history
…ing when titleCollapseMode=fade

PiperOrigin-RevId: 506622125
  • Loading branch information
dsn5ft committed Feb 2, 2023
1 parent 26697c3 commit bdb6f5d
Showing 1 changed file with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -883,25 +883,33 @@ private boolean shouldDrawMultiline() {

private void drawMultilineTransition(@NonNull Canvas canvas, float currentExpandedX, float y) {
int originalAlpha = textPaint.getAlpha();
// position expanded text appropriately
// position text appropriately
canvas.translate(currentExpandedX, y);
// Expanded text
textPaint.setAlpha((int) (expandedTextBlend * originalAlpha));
// Workaround for API 31(+). Paint applies an inverse alpha of Paint object on the shadow layer
// when collapsing mode is scale and shadow color is opaque. The workaround is to set the shadow
// not opaque. Then Paint will respect to the color's alpha. Applying the shadow color for
// expanded text.
if (VERSION.SDK_INT >= VERSION_CODES.S) {
textPaint.setShadowLayer(
currentShadowRadius,
currentShadowDx,
currentShadowDy,
MaterialColors.compositeARGBWithAlpha(currentShadowColor, textPaint.getAlpha()));

if (!fadeModeEnabled) {
// Expanded text (when not in fade mode, because in fade mode at this point the expanded text
// has been fully faded out, so there's no need to try to draw it again)
textPaint.setAlpha((int) (expandedTextBlend * originalAlpha));
// Workaround for API 31(+). Paint applies an inverse alpha of Paint object on the shadow
// layer when collapsing mode is scale and shadow color is opaque. The workaround is to set
// the shadow not opaque. Then Paint will respect to the color's alpha. Applying the shadow
// color for expanded text.
if (VERSION.SDK_INT >= VERSION_CODES.S) {
textPaint.setShadowLayer(
currentShadowRadius,
currentShadowDx,
currentShadowDy,
MaterialColors.compositeARGBWithAlpha(currentShadowColor, textPaint.getAlpha()));
}
textLayout.draw(canvas);
}
textLayout.draw(canvas);

// Collapsed text
textPaint.setAlpha((int) (collapsedTextBlend * originalAlpha));
if (!fadeModeEnabled) {
// Only change the collapsed text alpha when not in fade mode, because when in fade mode it
// will be precalculated based on the current fraction in calculateOffsets()
textPaint.setAlpha((int) (collapsedTextBlend * originalAlpha));
}
// Workaround for API 31(+). Applying the shadow color for collapsed text.
if (VERSION.SDK_INT >= VERSION_CODES.S) {
textPaint.setShadowLayer(
Expand Down

0 comments on commit bdb6f5d

Please sign in to comment.