35
35
import com .osfans .trime .setup .Config ;
36
36
import com .osfans .trime .util .GraphicUtils ;
37
37
38
+ import java .util .ArrayList ;
39
+
38
40
import timber .log .Timber ;
39
41
40
42
// 这是滑动键盘顶部的view,展示了键盘布局的多个标签。
@@ -45,7 +47,7 @@ public class TabView extends View {
45
47
private static final int CANDIDATE_TOUCH_OFFSET = -12 ;
46
48
47
49
private int highlightIndex ;
48
- private TabTag [] tabTags ;
50
+ private ArrayList < TabTag > tabTags ;
49
51
private final GraphicUtils graphicUtils ;
50
52
51
53
private PaintDrawable candidateHighlight ;
@@ -57,8 +59,7 @@ public class TabView extends View {
57
59
private final boolean shouldShowComment = true ;
58
60
private boolean isCommentOnTop ;
59
61
private boolean shouldCandidateUseCursor ;
60
-
61
- private final Rect [] tabGeometries = new Rect [MAX_CANDIDATE_COUNT + 2 ];
62
+ // private final Rect[] tabGeometries = new Rect[MAX_CANDIDATE_COUNT + 2];
62
63
63
64
public void reset (Context context ) {
64
65
Config config = Config .get (context );
@@ -107,65 +108,58 @@ private boolean isHighlighted(int i) {
107
108
return shouldCandidateUseCursor && i >= 0 && i == highlightIndex ;
108
109
}
109
110
110
- private void drawHighlight (Canvas canvas ) {
111
- if (isHighlighted (highlightIndex )) {
112
- candidateHighlight .setBounds (tabGeometries [highlightIndex ]);
113
- candidateHighlight .draw (canvas );
114
- }
115
- }
116
-
117
111
public int getHightlightLeft () {
118
- return tabGeometries [ highlightIndex ] .left ;
112
+ return tabTags . get ( highlightIndex ). geometry .left ;
119
113
}
120
114
121
115
public int getHightlightRight () {
122
- return tabGeometries [ highlightIndex ] .right ;
116
+ return tabTags . get ( highlightIndex ). geometry .right ;
123
117
}
124
118
125
- private void drawCandidates (Canvas canvas ) {
119
+ @ Override
120
+ protected void onDraw (Canvas canvas ) {
121
+ if (canvas == null ) return ;
126
122
if (tabTags == null ) return ;
123
+ super .onDraw (canvas );
127
124
128
- float y = tabGeometries [0 ].centerY () - (candidatePaint .ascent () + candidatePaint .descent ()) / 2 ;
129
- if (shouldShowComment && isCommentOnTop ) y += (float ) commentHeight / 2 ;
130
-
131
- int i = 0 ;
132
- while (i < tabTags .length ) {
125
+ // Draw highlight background
126
+ if (isHighlighted (highlightIndex )) {
127
+ candidateHighlight .setBounds (tabTags .get (highlightIndex ).geometry );
128
+ candidateHighlight .draw (canvas );
129
+ }
130
+ // Draw tab text
131
+ float tabY = (shouldShowComment && isCommentOnTop )
132
+ ? tabTags .get (0 ).geometry .centerY () - (candidatePaint .ascent () + candidatePaint .descent ()) / 2.0f
133
+ + commentHeight / 2.0f
134
+ : tabTags .get (0 ).geometry .centerY () - (candidatePaint .ascent () + candidatePaint .descent ()) / 2.0f ;
135
+
136
+ for (TabTag computedTab : tabTags ) {
137
+ int i = tabTags .indexOf (computedTab );
133
138
// Calculate a position where the text could be centered in the rectangle.
134
- float x = tabGeometries [ i ] .centerX ();
139
+ float tabX = computedTab . geometry .centerX ();
135
140
136
141
candidatePaint .setColor (
137
- isHighlighted (i ) ? hilitedCandidateTextColor : candidateTextColor );
138
- graphicUtils .drawText (canvas , getTabText ( i ), x , y , candidatePaint , candidateFont );
142
+ isHighlighted (i ) ? hilitedCandidateTextColor : candidateTextColor );
143
+ graphicUtils .drawText (canvas , computedTab . text , tabX , tabY , candidatePaint , candidateFont );
139
144
// Draw the separator at the right edge of each candidate.
140
145
canvas .drawRect (
141
- tabGeometries [ i ] .right - candidateSpacing ,
142
- tabGeometries [ i ] .top ,
143
- tabGeometries [ i ] .right + candidateSpacing ,
144
- tabGeometries [ i ] .bottom ,
146
+ computedTab . geometry .right - candidateSpacing ,
147
+ computedTab . geometry .top ,
148
+ computedTab . geometry .right + candidateSpacing ,
149
+ computedTab . geometry .bottom ,
145
150
separatorPaint
146
151
);
147
- i ++;
148
- }
149
- }
150
-
151
- @ Override
152
- protected void onDraw (Canvas canvas ) {
153
- if (canvas == null ) {
154
- return ;
155
152
}
156
- super .onDraw (canvas );
157
-
158
- drawHighlight (canvas );
159
- drawCandidates (canvas );
160
153
}
161
154
162
155
public void updateCandidateWidth () {
163
156
tabTags = TabManager .get ().getTabCandidates ();
164
157
highlightIndex = TabManager .get ().getSelected ();
165
158
166
159
int x = 0 ;
167
- for (int i = 0 ; i < tabTags .length ; i ++) {
168
- tabGeometries [i ] = new Rect (x , 0 , x += getTabWidth (i ), getHeight ());
160
+ for (TabTag computedTab : tabTags ) {
161
+ int i = tabTags .indexOf (computedTab );
162
+ computedTab .geometry = new Rect (x , 0 , x += getTabWidth (i ), getHeight ());
169
163
x += candidateSpacing ;
170
164
}
171
165
LayoutParams params = getLayoutParams ();
@@ -219,7 +213,6 @@ public boolean onTouchEvent(@NonNull MotionEvent me) {
219
213
case EXIT :
220
214
Trime .getService ().selectLiquidKeyboard (-1 );
221
215
break ;
222
-
223
216
// TODO liquidKeyboard中除返回按钮外,其他按键均未实装
224
217
case DEL_LEFT :
225
218
case DEL_RIGHT :
@@ -232,7 +225,7 @@ public boolean onTouchEvent(@NonNull MotionEvent me) {
232
225
invalidate ();
233
226
Trime .getService ().selectLiquidKeyboard (i );
234
227
}
235
- Timber .d ("index=" + i + " length=" + tabTags .length );
228
+ Timber .d ("index=" + i + " length=" + tabTags .size () );
236
229
}
237
230
break ;
238
231
}
@@ -247,28 +240,21 @@ public boolean onTouchEvent(@NonNull MotionEvent me) {
247
240
* @return {@code >=0}: 觸摸點 (x, y) 處候選項序號,從0開始編號; {@code -1}: 觸摸點 (x, y) 處無候選項;
248
241
*/
249
242
private int getTabIndex (int x , int y ) {
250
- Rect r = new Rect ();
251
-
252
- int j = 0 ;
253
- for (int i = 0 ; i < tabTags .length ; i ++) {
254
- // Enlarge the rectangle to be more responsive to user clicks.
255
- r .set (tabGeometries [j ++]);
256
- r .inset (0 , CANDIDATE_TOUCH_OFFSET );
257
- if (r .contains (x , y )) {
258
- // Returns -1 if there is no candidate in the hitting rectangle.
259
- return (i < tabTags .length ) ? i : -1 ;
243
+ //Rect r = new Rect();
244
+ int retIndex = -1 ; // Returns -1 if there is no tab in the hitting rectangle.
245
+ for (TabTag computedTab : tabTags ) {
246
+ /* Enlarge the rectangle to be more responsive to user clicks.
247
+ // r.set(tabGeometries[j++]);
248
+ //r.inset(0, CANDIDATE_TOUCH_OFFSET); */
249
+ if (computedTab .geometry .contains (x , y )) {
250
+ retIndex = tabTags .indexOf (computedTab );
260
251
}
261
252
}
262
- return -1 ;
263
- }
264
-
265
- private String getTabText (int i ) {
266
- if (tabTags != null && i >= 0 ) return tabTags [i ].text ;
267
- return "-1" ;
253
+ return retIndex ;
268
254
}
269
255
270
256
private float getTabWidth (int i ) {
271
- String s = getTabText (i );
257
+ String s = tabTags . get (i ). text ;
272
258
return s != null ? 2 * candidatePadding + graphicUtils .measureText (candidatePaint , s , candidateFont ) : 2 * candidatePadding ;
273
259
}
274
260
}
0 commit comments