Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 3b1fee1

Browse files
committed
Merge branch 'master' of github.com:opacapp/multiline-collapsingtoolbar
2 parents 5dd5e24 + 3b53ead commit 3b1fee1

File tree

2 files changed

+84
-11
lines changed

2 files changed

+84
-11
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ allprojects {
1313
repositories {
1414
jcenter()
1515
}
16-
}
16+
}

multiline-collapsingtoolbar/src/main/java/net/opacapp/multilinecollapsingtoolbar/CollapsingTextHelper.java

+83-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (C) 2015 The Android Open Source Project
3-
* Modified 2015 by Johan v. Forstner
3+
* Modified 2015 by Johan v. Forstner (modifications are marked with comments)
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -28,8 +28,10 @@
2828
import android.support.v4.text.TextDirectionHeuristicsCompat;
2929
import android.support.v4.view.GravityCompat;
3030
import android.support.v4.view.ViewCompat;
31+
// BEGIN MODIFICATION: Added imports
3132
import android.text.Layout;
3233
import android.text.StaticLayout;
34+
// END MODIFICATION
3335
import android.text.TextPaint;
3436
import android.text.TextUtils;
3537
import android.view.Gravity;
@@ -52,8 +54,10 @@ final class CollapsingTextHelper {
5254
}
5355

5456
private final View mView;
57+
5558
private boolean mDrawTitle;
5659
private float mExpandedFraction;
60+
5761
private final Rect mExpandedBounds;
5862
private final Rect mCollapsedBounds;
5963
private final RectF mCurrentBounds;
@@ -63,6 +67,7 @@ final class CollapsingTextHelper {
6367
private float mCollapsedTextSize = 15;
6468
private int mExpandedTextColor;
6569
private int mCollapsedTextColor;
70+
6671
private float mExpandedDrawY;
6772
private float mCollapsedDrawY;
6873
private float mExpandedDrawX;
@@ -72,42 +77,50 @@ final class CollapsingTextHelper {
7277
private Typeface mCollapsedTypeface;
7378
private Typeface mExpandedTypeface;
7479
private Typeface mCurrentTypeface;
80+
7581
private CharSequence mText;
7682
private CharSequence mTextToDraw;
77-
private CharSequence mTextToDrawCollapsed;
7883
private boolean mIsRtl;
84+
7985
private boolean mUseTexture;
80-
private Bitmap mCollapsedTitleTexture;
8186
private Bitmap mExpandedTitleTexture;
8287
private Paint mTexturePaint;
88+
// MODIFICATION: Removed now unused fields mTextureAscent and mTextureDescent
89+
8390
private float mScale;
8491
private float mCurrentTextSize;
92+
8593
private boolean mBoundsChanged;
94+
8695
private final TextPaint mTextPaint;
96+
8797
private Interpolator mPositionInterpolator;
8898
private Interpolator mTextSizeInterpolator;
89-
private Interpolator mTextBlendInterpolator;
99+
90100
private float mCollapsedShadowRadius, mCollapsedShadowDx, mCollapsedShadowDy;
91101
private int mCollapsedShadowColor;
102+
92103
private float mExpandedShadowRadius, mExpandedShadowDx, mExpandedShadowDy;
93104
private int mExpandedShadowColor;
105+
106+
// BEGIN MODIFICATION: Added fields
107+
private CharSequence mTextToDrawCollapsed;
108+
private Bitmap mCollapsedTitleTexture;
94109
private StaticLayout mTextLayout;
95110
private float mTextBlend;
111+
// END MODIFICATION
96112

97113
public CollapsingTextHelper(View view) {
98114
mView = view;
115+
99116
mTextPaint = new TextPaint();
100117
mTextPaint.setAntiAlias(true);
118+
101119
mCollapsedBounds = new Rect();
102120
mExpandedBounds = new Rect();
103121
mCurrentBounds = new RectF();
104122
}
105123

106-
void setTextBlendInterpolator(Interpolator interpolator) {
107-
mTextBlendInterpolator = interpolator;
108-
recalculate();
109-
}
110-
111124
void setTextSizeInterpolator(Interpolator interpolator) {
112125
mTextSizeInterpolator = interpolator;
113126
recalculate();
@@ -204,9 +217,11 @@ void setCollapsedTextAppearance(int resId) {
204217
mCollapsedShadowDy = a.getFloat(R.styleable.TextAppearance_android_shadowDy, 0);
205218
mCollapsedShadowRadius = a.getFloat(R.styleable.TextAppearance_android_shadowRadius, 0);
206219
a.recycle();
220+
207221
if (Build.VERSION.SDK_INT >= 16) {
208222
mCollapsedTypeface = readFontFamilyTypeface(resId);
209223
}
224+
210225
recalculate();
211226
}
212227

@@ -225,9 +240,11 @@ void setExpandedTextAppearance(int resId) {
225240
mExpandedShadowDy = a.getFloat(R.styleable.TextAppearance_android_shadowDy, 0);
226241
mExpandedShadowRadius = a.getFloat(R.styleable.TextAppearance_android_shadowRadius, 0);
227242
a.recycle();
243+
228244
if (Build.VERSION.SDK_INT >= 16) {
229245
mExpandedTypeface = readFontFamilyTypeface(resId);
230246
}
247+
231248
recalculate();
232249
}
233250

@@ -281,6 +298,7 @@ Typeface getExpandedTypeface() {
281298
*/
282299
void setExpansionFraction(float fraction) {
283300
fraction = MathUtils.constrain(fraction, 0f, 1f);
301+
284302
if (fraction != mExpandedFraction) {
285303
mExpandedFraction = fraction;
286304
calculateCurrentOffsets();
@@ -311,7 +329,11 @@ private void calculateOffsets(final float fraction) {
311329
mPositionInterpolator);
312330
setInterpolatedTextSize(lerp(mExpandedTextSize, mCollapsedTextSize,
313331
fraction, mTextSizeInterpolator));
314-
setTextBlend(lerp(0, 1, fraction, mTextBlendInterpolator));
332+
333+
// BEGIN MODIFICATION: set text blending
334+
setTextBlend(lerp(0, 1, fraction, null));
335+
// END MODIFICATION
336+
315337
if (mCollapsedTextColor != mExpandedTextColor) {
316338
// If the collapsed and expanded text colors are different, blend them based on the
317339
// fraction
@@ -331,11 +353,18 @@ private void calculateBaseOffsets() {
331353
final float currentTextSize = mCurrentTextSize;
332354
// We then calculate the collapsed text size, using the same logic
333355
calculateUsingTextSize(mCollapsedTextSize);
356+
357+
// BEGIN MODIFICATION: set mTextToDrawCollapsed and calculate width using mTextLayout
334358
mTextToDrawCollapsed = mTextToDraw;
335359
float width = mTextLayout != null ? mTextLayout.getWidth() : 0;
360+
// END MODIFICATION
361+
336362
final int collapsedAbsGravity = GravityCompat.getAbsoluteGravity(mCollapsedTextGravity,
337363
mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
364+
365+
// BEGIN MODIFICATION: calculate height and Y position using mTextLayout
338366
float textHeight = mTextLayout != null ? mTextLayout.getHeight() : 0;
367+
339368
switch (collapsedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
340369
case Gravity.BOTTOM:
341370
mCollapsedDrawY = mCollapsedBounds.bottom - textHeight;
@@ -349,6 +378,8 @@ private void calculateBaseOffsets() {
349378
mCollapsedDrawY = mCollapsedBounds.centerY() - textOffset;
350379
break;
351380
}
381+
// END MODIFICATION
382+
352383
switch (collapsedAbsGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
353384
case Gravity.CENTER_HORIZONTAL:
354385
mCollapsedDrawX = mCollapsedBounds.centerX() - (width / 2);
@@ -361,10 +392,17 @@ private void calculateBaseOffsets() {
361392
mCollapsedDrawX = mCollapsedBounds.left;
362393
break;
363394
}
395+
364396
calculateUsingTextSize(mExpandedTextSize);
397+
398+
// BEGIN MODIFICATION: calculate width using mTextLayout
365399
width = mTextLayout != null ? mTextLayout.getWidth() : 0;
400+
// END MODIFICATION
401+
366402
final int expandedAbsGravity = GravityCompat.getAbsoluteGravity(mExpandedTextGravity,
367403
mIsRtl ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR);
404+
405+
// BEGIN MODIFICATION: calculate height and Y position using mTextLayout
368406
textHeight = mTextLayout != null ? mTextLayout.getHeight() : 0;
369407
switch (expandedAbsGravity & Gravity.VERTICAL_GRAVITY_MASK) {
370408
case Gravity.BOTTOM:
@@ -379,6 +417,8 @@ private void calculateBaseOffsets() {
379417
mExpandedDrawY = mExpandedBounds.centerY() - textOffset;
380418
break;
381419
}
420+
// END MODIFICATION
421+
382422
switch (expandedAbsGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
383423
case Gravity.CENTER_HORIZONTAL:
384424
mExpandedDrawX = mExpandedBounds.centerX() - (width / 2);
@@ -391,6 +431,7 @@ private void calculateBaseOffsets() {
391431
mExpandedDrawX = mExpandedBounds.left;
392432
break;
393433
}
434+
394435
// The bounds have changed so we need to clear the texture
395436
clearTexture();
396437
// Now reset the text size back to the original
@@ -410,18 +451,25 @@ private void interpolateBounds(float fraction) {
410451

411452
public void draw(Canvas canvas) {
412453
final int saveCount = canvas.save();
454+
413455
if (mTextToDraw != null && mDrawTitle) {
414456
float x = mCurrentDrawX;
415457
float y = mCurrentDrawY;
458+
416459
final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;
417460
final float ascent;
461+
// MODIFICATION: removed now unused "descent" variable declaration
462+
418463
// Update the TextPaint to the current text size
419464
mTextPaint.setTextSize(mCurrentTextSize);
465+
466+
// BEGIN MODIFICATION: new drawing code
420467
if (drawTexture) {
421468
ascent = 0;
422469
} else {
423470
ascent = mTextPaint.ascent() * mScale;
424471
}
472+
425473
if (DEBUG_DRAW) {
426474
// Just a debug tool, which drawn a Magneta rect in the text bounds
427475
canvas.drawRect(mCurrentBounds.left, y, mCurrentBounds.right,
@@ -445,10 +493,12 @@ public void draw(Canvas canvas) {
445493
canvas.drawText(mTextToDrawCollapsed, 0, mTextToDrawCollapsed.length(), 0,
446494
-ascent / mScale, mTextPaint);
447495
}
496+
// END MODIFICATION
448497
}
449498
canvas.restoreToCount(saveCount);
450499
}
451500

501+
// BEGIN MODIFICATION: new getCrossfadeAlpha function
452502
/**
453503
* Calculate alpha values for "square crossfade" between two images with transparency see
454504
* http://javagraphics.blogspot.de/2008/06/crossfades-what-is-and-isnt-possible.html
@@ -464,6 +514,7 @@ private boolean calculateIsRtl(CharSequence text) {
464514
? TextDirectionHeuristicsCompat.FIRSTSTRONG_RTL
465515
: TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR).isRtl(text, 0, text.length());
466516
}
517+
// END MODIFICATION
467518

468519
private void setInterpolatedTextSize(float textSize) {
469520
calculateUsingTextSize(textSize);
@@ -472,22 +523,28 @@ private void setInterpolatedTextSize(float textSize) {
472523
if (mUseTexture) {
473524
// Make sure we have an expanded texture if needed
474525
ensureExpandedTexture();
526+
// BEGIN MODIFICATION: added collapsed texture
475527
ensureCollapsedTexture();
476528
}
477529
ViewCompat.postInvalidateOnAnimation(mView);
530+
// END MODIFICATION
478531
}
479532

533+
// BEGIN MODIFICATION: new setTextBlend method
480534
private void setTextBlend(float blend) {
481535
mTextBlend = blend;
482536
ViewCompat.postInvalidateOnAnimation(mView);
483537
}
538+
// END MODIFICATION
484539

485540
private void calculateUsingTextSize(final float textSize) {
486541
if (mText == null) return;
487542
final float availableWidth;
488543
final float newTextSize;
489544
boolean updateDrawText = false;
545+
// BEGIN MODIFICATION: Add maxLines variable
490546
int maxLines;
547+
// END MODIFICATION
491548
if (isClose(textSize, mCollapsedTextSize)) {
492549
availableWidth = mCollapsedBounds.width();
493550
newTextSize = mCollapsedTextSize;
@@ -496,7 +553,9 @@ private void calculateUsingTextSize(final float textSize) {
496553
mCurrentTypeface = mCollapsedTypeface;
497554
updateDrawText = true;
498555
}
556+
// BEGIN MODIFICATION: Set maxLines variable
499557
maxLines = 1;
558+
// END MODIFICATION
500559
} else {
501560
availableWidth = mExpandedBounds.width();
502561
newTextSize = mExpandedTextSize;
@@ -511,7 +570,9 @@ private void calculateUsingTextSize(final float textSize) {
511570
// Else, we'll scale down from the expanded text size
512571
mScale = textSize / mExpandedTextSize;
513572
}
573+
// BEGIN MODIFICATION: Set maxLines variable
514574
maxLines = 3;
575+
// END MODIFICATION
515576
}
516577
if (availableWidth > 0) {
517578
updateDrawText = (mCurrentTextSize != newTextSize) || mBoundsChanged || updateDrawText;
@@ -522,6 +583,7 @@ private void calculateUsingTextSize(final float textSize) {
522583
mTextPaint.setTextSize(mCurrentTextSize);
523584
mTextPaint.setTypeface(mCurrentTypeface);
524585

586+
// BEGIN MODIFICATION: Text layout creation and text truncation
525587
StaticLayout layout = new StaticLayout(mText, mTextPaint, (int) availableWidth,
526588
Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
527589
CharSequence truncatedText;
@@ -553,6 +615,7 @@ private void calculateUsingTextSize(final float textSize) {
553615
}
554616
mTextLayout = new StaticLayout(mTextToDraw, mTextPaint, (int) availableWidth,
555617
Layout.Alignment.ALIGN_NORMAL, 1, 0, false);
618+
// END MODIFICATION
556619
}
557620
}
558621

@@ -562,20 +625,29 @@ private void ensureExpandedTexture() {
562625
return;
563626
}
564627
calculateOffsets(0f);
628+
629+
// BEGIN MODIFICATION: Calculate width and height using mTextLayout and remove
630+
// mTextureAscent and mTextureDescent assignment
565631
final int w = mTextLayout.getWidth();
566632
final int h = mTextLayout.getHeight();
633+
// END MODIFICATION
634+
567635
if (w <= 0 && h <= 0) {
568636
return; // If the width or height are 0, return
569637
}
570638
mExpandedTitleTexture = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
639+
640+
// BEGIN MODIFICATION: Draw text using mTextLayout
571641
Canvas c = new Canvas(mExpandedTitleTexture);
572642
mTextLayout.draw(c);
643+
// END MODIFICATION
573644
if (mTexturePaint == null) {
574645
// Make sure we have a paint
575646
mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
576647
}
577648
}
578649

650+
// BEGIN MODIFICATION: new ensureCollapsedTexture method
579651
private void ensureCollapsedTexture() {
580652
if (mCollapsedTitleTexture != null || mCollapsedBounds.isEmpty()
581653
|| TextUtils.isEmpty(mTextToDraw)) {
@@ -596,6 +668,7 @@ private void ensureCollapsedTexture() {
596668
mTexturePaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
597669
}
598670
}
671+
// END MODIFICATION
599672

600673
public void recalculate() {
601674
if (mView.getHeight() > 0 && mView.getWidth() > 0) {

0 commit comments

Comments
 (0)