Browse Source

Ellipsize text

Guy Carmeli 8 years ago
parent
commit
e10ea83b78

+ 32
- 8
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTextView.java View File

5
 import android.graphics.Canvas;
5
 import android.graphics.Canvas;
6
 import android.graphics.Color;
6
 import android.graphics.Color;
7
 import android.graphics.Paint;
7
 import android.graphics.Paint;
8
+import android.support.annotation.CheckResult;
8
 import android.support.annotation.FloatRange;
9
 import android.support.annotation.FloatRange;
9
 import android.support.v4.widget.TextViewCompat;
10
 import android.support.v4.widget.TextViewCompat;
10
 import android.support.v7.widget.TintTypedArray;
11
 import android.support.v7.widget.TintTypedArray;
11
 import android.text.TextPaint;
12
 import android.text.TextPaint;
13
+import android.text.TextUtils;
12
 import android.view.animation.DecelerateInterpolator;
14
 import android.view.animation.DecelerateInterpolator;
13
 import android.view.animation.Interpolator;
15
 import android.view.animation.Interpolator;
14
 import android.widget.FrameLayout;
16
 import android.widget.FrameLayout;
24
 
26
 
25
     private final int collapsedHeight;
27
     private final int collapsedHeight;
26
     private TextView dummy;
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
     private float initialY = -1;
33
     private float initialY = -1;
30
     private float currentY;
34
     private float currentY;
31
     private float collapseY;
35
     private float collapseY;
53
         a.recycle();
57
         a.recycle();
54
 
58
 
55
         dummy = new TextView(context);
59
         dummy = new TextView(context);
60
+        dummy.setSingleLine();
61
+        dummy.setEllipsize(TextUtils.TruncateAt.END);
56
         TextViewCompat.setTextAppearance(dummy, titleTextAppearance);
62
         TextViewCompat.setTextAppearance(dummy, titleTextAppearance);
57
         collapsedTextSize = dummy.getTextSize();
63
         collapsedTextSize = dummy.getTextSize();
58
         expendedTextSize = collapsedTextSize * TEXT_SCALE_FACTOR;
64
         expendedTextSize = collapsedTextSize * TEXT_SCALE_FACTOR;
63
 
69
 
64
     private void createTextPaint() {
70
     private void createTextPaint() {
65
         paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
71
         paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
66
-        paint.setColor(Color.GREEN);
72
+        paint.setColor(Color.WHITE);
67
         paint.setTextSize(expendedTextSize);
73
         paint.setTextSize(expendedTextSize);
68
     }
74
     }
69
 
75
 
70
     public void setText(String text) {
76
     public void setText(String text) {
71
-        this.text = text;
77
+        this.originalText = text;
72
         dummy.setText(text);
78
         dummy.setText(text);
73
     }
79
     }
74
 
80
 
97
     protected void onDraw(Canvas canvas) {
103
     protected void onDraw(Canvas canvas) {
98
         super.onDraw(canvas);
104
         super.onDraw(canvas);
99
         if (initialY == -1) {
105
         if (initialY == -1) {
100
-            calculateTextPosition();
106
+            calculateTextPosition(canvas);
101
         }
107
         }
102
         paint.setTextSize(linearInterpolation(expendedTextSize, collapsedTextSize, collapseFraction));
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
         final int[] positionOnScreen = new int[2];
125
         final int[] positionOnScreen = new int[2];
108
         getLocationInWindow(positionOnScreen);
126
         getLocationInWindow(positionOnScreen);
109
-        currentY = initialY = positionOnScreen[1] + getMeasuredHeight() - dummy.getMeasuredHeight();
127
+        currentY = initialY = positionOnScreen[1] + canvas.getHeight() - dummy.getMeasuredHeight();
110
         float bottomMargin = ViewUtils.convertDpToPixel(10);
128
         float bottomMargin = ViewUtils.convertDpToPixel(10);
111
         collapseY = positionOnScreen[1] + bottomMargin;
129
         collapseY = positionOnScreen[1] + bottomMargin;
112
     }
130
     }
115
         fraction = scaleInterpolator.getInterpolation(fraction);
133
         fraction = scaleInterpolator.getInterpolation(fraction);
116
         return from + (fraction * (to - from));
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
 }