|
@@ -5,10 +5,12 @@ import android.content.Context;
|
5
|
5
|
import android.graphics.Canvas;
|
6
|
6
|
import android.graphics.Color;
|
7
|
7
|
import android.graphics.Paint;
|
|
8
|
+import android.support.annotation.CheckResult;
|
8
|
9
|
import android.support.annotation.FloatRange;
|
9
|
10
|
import android.support.v4.widget.TextViewCompat;
|
10
|
11
|
import android.support.v7.widget.TintTypedArray;
|
11
|
12
|
import android.text.TextPaint;
|
|
13
|
+import android.text.TextUtils;
|
12
|
14
|
import android.view.animation.DecelerateInterpolator;
|
13
|
15
|
import android.view.animation.Interpolator;
|
14
|
16
|
import android.widget.FrameLayout;
|
|
@@ -24,8 +26,10 @@ public class CollapsingTextView extends FrameLayout {
|
24
|
26
|
|
25
|
27
|
private final int collapsedHeight;
|
26
|
28
|
private TextView dummy;
|
27
|
|
- private String text;
|
28
|
|
- private Paint paint;
|
|
29
|
+ private CharSequence textToDraw;
|
|
30
|
+ private CharSequence expendedTextToDraw;
|
|
31
|
+ private String originalText;
|
|
32
|
+ private TextPaint paint;
|
29
|
33
|
private float initialY = -1;
|
30
|
34
|
private float currentY;
|
31
|
35
|
private float collapseY;
|
|
@@ -53,6 +57,8 @@ public class CollapsingTextView extends FrameLayout {
|
53
|
57
|
a.recycle();
|
54
|
58
|
|
55
|
59
|
dummy = new TextView(context);
|
|
60
|
+ dummy.setSingleLine();
|
|
61
|
+ dummy.setEllipsize(TextUtils.TruncateAt.END);
|
56
|
62
|
TextViewCompat.setTextAppearance(dummy, titleTextAppearance);
|
57
|
63
|
collapsedTextSize = dummy.getTextSize();
|
58
|
64
|
expendedTextSize = collapsedTextSize * TEXT_SCALE_FACTOR;
|
|
@@ -63,12 +69,12 @@ public class CollapsingTextView extends FrameLayout {
|
63
|
69
|
|
64
|
70
|
private void createTextPaint() {
|
65
|
71
|
paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
|
66
|
|
- paint.setColor(Color.GREEN);
|
|
72
|
+ paint.setColor(Color.WHITE);
|
67
|
73
|
paint.setTextSize(expendedTextSize);
|
68
|
74
|
}
|
69
|
75
|
|
70
|
76
|
public void setText(String text) {
|
71
|
|
- this.text = text;
|
|
77
|
+ this.originalText = text;
|
72
|
78
|
dummy.setText(text);
|
73
|
79
|
}
|
74
|
80
|
|
|
@@ -97,16 +103,28 @@ public class CollapsingTextView extends FrameLayout {
|
97
|
103
|
protected void onDraw(Canvas canvas) {
|
98
|
104
|
super.onDraw(canvas);
|
99
|
105
|
if (initialY == -1) {
|
100
|
|
- calculateTextPosition();
|
|
106
|
+ calculateTextPosition(canvas);
|
101
|
107
|
}
|
102
|
108
|
paint.setTextSize(linearInterpolation(expendedTextSize, collapsedTextSize, collapseFraction));
|
103
|
|
- canvas.drawText(text, 0, currentY, paint);
|
|
109
|
+ calculateTextToDraw();
|
|
110
|
+ canvas.drawText(textToDraw, 0, textToDraw.length(), 0, currentY, paint);
|
104
|
111
|
}
|
105
|
112
|
|
106
|
|
- private void calculateTextPosition() {
|
|
113
|
+ private void calculateTextToDraw() {
|
|
114
|
+ if (currentY == collapseY) {
|
|
115
|
+ textToDraw = calculateText();
|
|
116
|
+ } else {
|
|
117
|
+ if (expendedTextToDraw == null) {
|
|
118
|
+ expendedTextToDraw = calculateText();
|
|
119
|
+ }
|
|
120
|
+ textToDraw = expendedTextToDraw;
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ private void calculateTextPosition(Canvas canvas) {
|
107
|
125
|
final int[] positionOnScreen = new int[2];
|
108
|
126
|
getLocationInWindow(positionOnScreen);
|
109
|
|
- currentY = initialY = positionOnScreen[1] + getMeasuredHeight() - dummy.getMeasuredHeight();
|
|
127
|
+ currentY = initialY = positionOnScreen[1] + canvas.getHeight() - dummy.getMeasuredHeight();
|
110
|
128
|
float bottomMargin = ViewUtils.convertDpToPixel(10);
|
111
|
129
|
collapseY = positionOnScreen[1] + bottomMargin;
|
112
|
130
|
}
|
|
@@ -115,4 +133,10 @@ public class CollapsingTextView extends FrameLayout {
|
115
|
133
|
fraction = scaleInterpolator.getInterpolation(fraction);
|
116
|
134
|
return from + (fraction * (to - from));
|
117
|
135
|
}
|
|
136
|
+
|
|
137
|
+ @CheckResult
|
|
138
|
+ private String calculateText() {
|
|
139
|
+ return (String) TextUtils.ellipsize(originalText, paint,
|
|
140
|
+ getWidth(), TextUtils.TruncateAt.END);
|
|
141
|
+ }
|
118
|
142
|
}
|