37
37
public class Candidate extends View {
38
38
39
39
/** 處理候選條選字事件 */
40
- public interface CandidateListener {
41
- void onPickCandidate (int index );
40
+ public interface EventListener {
41
+ void onCandidatePressed (int index );
42
42
}
43
43
44
44
private static final int MAX_CANDIDATE_COUNT = 30 ;
45
45
private static final int CANDIDATE_TOUCH_OFFSET = -12 ;
46
46
47
- private CandidateListener listener ;
47
+ private EventListener listener ;
48
48
private int highlightIndex ;
49
49
private Rime .RimeCandidate [] candidates ;
50
50
private int num_candidates ;
51
51
private int start_num = 0 ;
52
52
53
53
private Drawable candidateHighlight , candidateSeparator ;
54
- private final Paint paintCandidate ;
55
- private final Paint paintSymbol ;
56
- private final Paint paintComment ;
57
- private Typeface tfCandidate , tfSymbol , tfComment , tfHanB , tfLatin ;
58
- private int candidate_text_color , hilited_candidate_text_color ;
59
- private int comment_text_color , hilited_comment_text_color ;
60
- private int candidate_view_height , comment_height , candidate_spacing , candidate_padding ;
61
- private boolean show_comment = true , comment_on_top , candidate_use_cursor ;
54
+ private final Paint candidatePaint ;
55
+ private final Paint symbolPaint ;
56
+ private final Paint commentPaint ;
57
+ private Typeface candidateTypeface , symbolTypeface , commentTypeface , hanBTypeface , latinTypeface ;
58
+ private int candidateTextColor , hilitedCandidateTextColor ;
59
+ private int commentTextColor , hilitedCommentTextColor ;
60
+ private int candidateViewHeight , commentHeight , candidateSpacing , candidatePadding ;
61
+ private boolean shouldShowComment = true , isCommentOnTop , candidateUseCursor ;
62
62
63
63
private final Rect [] candidateRect = new Rect [MAX_CANDIDATE_COUNT + 2 ];
64
64
@@ -67,52 +67,52 @@ public void reset(Context context) {
67
67
candidateHighlight = new PaintDrawable (config .getColor ("hilited_candidate_back_color" ));
68
68
((PaintDrawable ) candidateHighlight ).setCornerRadius (config .getFloat ("layout/round_corner" ));
69
69
candidateSeparator = new PaintDrawable (config .getColor ("candidate_separator_color" ));
70
- candidate_spacing = config .getPixel ("candidate_spacing" );
71
- candidate_padding = config .getPixel ("candidate_padding" );
70
+ candidateSpacing = config .getPixel ("candidate_spacing" );
71
+ candidatePadding = config .getPixel ("candidate_padding" );
72
72
73
- candidate_text_color = config .getColor ("candidate_text_color" );
74
- comment_text_color = config .getColor ("comment_text_color" );
75
- hilited_candidate_text_color = config .getColor ("hilited_candidate_text_color" );
76
- hilited_comment_text_color = config .getColor ("hilited_comment_text_color" );
73
+ candidateTextColor = config .getColor ("candidate_text_color" );
74
+ commentTextColor = config .getColor ("comment_text_color" );
75
+ hilitedCandidateTextColor = config .getColor ("hilited_candidate_text_color" );
76
+ hilitedCommentTextColor = config .getColor ("hilited_comment_text_color" );
77
77
78
78
int candidate_text_size = config .getPixel ("candidate_text_size" );
79
79
int comment_text_size = config .getPixel ("comment_text_size" );
80
- candidate_view_height = config .getPixel ("candidate_view_height" );
81
- comment_height = config .getPixel ("comment_height" );
82
-
83
- tfCandidate = config .getFont ("candidate_font" );
84
- tfLatin = config .getFont ("latin_font" );
85
- tfHanB = config .getFont ("hanb_font" );
86
- tfComment = config .getFont ("comment_font" );
87
- tfSymbol = config .getFont ("symbol_font" );
88
-
89
- paintCandidate .setTextSize (candidate_text_size );
90
- paintCandidate .setTypeface (tfCandidate );
91
- paintSymbol .setTextSize (candidate_text_size );
92
- paintSymbol .setTypeface (tfSymbol );
93
- paintComment .setTextSize (comment_text_size );
94
- paintComment .setTypeface (tfComment );
95
-
96
- comment_on_top = config .getBoolean ("comment_on_top" );
97
- candidate_use_cursor = config .getBoolean ("candidate_use_cursor" );
80
+ candidateViewHeight = config .getPixel ("candidate_view_height" );
81
+ commentHeight = config .getPixel ("comment_height" );
82
+
83
+ candidateTypeface = config .getFont ("candidate_font" );
84
+ latinTypeface = config .getFont ("latin_font" );
85
+ hanBTypeface = config .getFont ("hanb_font" );
86
+ commentTypeface = config .getFont ("comment_font" );
87
+ symbolTypeface = config .getFont ("symbol_font" );
88
+
89
+ candidatePaint .setTextSize (candidate_text_size );
90
+ candidatePaint .setTypeface (candidateTypeface );
91
+ symbolPaint .setTextSize (candidate_text_size );
92
+ symbolPaint .setTypeface (symbolTypeface );
93
+ commentPaint .setTextSize (comment_text_size );
94
+ commentPaint .setTypeface (commentTypeface );
95
+
96
+ isCommentOnTop = config .getBoolean ("comment_on_top" );
97
+ candidateUseCursor = config .getBoolean ("candidate_use_cursor" );
98
98
invalidate ();
99
99
}
100
100
101
101
public void setShowComment (boolean value ) {
102
- show_comment = value ;
102
+ shouldShowComment = value ;
103
103
}
104
104
105
105
public Candidate (Context context , AttributeSet attrs ) {
106
106
super (context , attrs );
107
- paintCandidate = new Paint ();
108
- paintCandidate .setAntiAlias (true );
109
- paintCandidate .setStrokeWidth (0 );
110
- paintSymbol = new Paint ();
111
- paintSymbol .setAntiAlias (true );
112
- paintSymbol .setStrokeWidth (0 );
113
- paintComment = new Paint ();
114
- paintComment .setAntiAlias (true );
115
- paintComment .setStrokeWidth (0 );
107
+ candidatePaint = new Paint ();
108
+ candidatePaint .setAntiAlias (true );
109
+ candidatePaint .setStrokeWidth (0 );
110
+ symbolPaint = new Paint ();
111
+ symbolPaint .setAntiAlias (true );
112
+ symbolPaint .setStrokeWidth (0 );
113
+ commentPaint = new Paint ();
114
+ commentPaint .setAntiAlias (true );
115
+ commentPaint .setStrokeWidth (0 );
116
116
117
117
reset (context );
118
118
@@ -123,7 +123,7 @@ public static int getMaxCandidateCount() {
123
123
return MAX_CANDIDATE_COUNT ;
124
124
}
125
125
126
- public void setCandidateListener (CandidateListener listener ) {
126
+ public void setCandidateListener (EventListener listener ) {
127
127
this .listener = listener ;
128
128
}
129
129
@@ -152,7 +152,7 @@ private boolean pickHighlighted(int index) {
152
152
if ((highlightIndex != -1 ) && (listener != null )) {
153
153
if (index == -1 ) index = highlightIndex ;
154
154
if (index >= 0 ) index += start_num ;
155
- listener .onPickCandidate (index );
155
+ listener .onCandidatePressed (index );
156
156
return true ;
157
157
}
158
158
return false ;
@@ -175,7 +175,7 @@ private void removeHighlight() {
175
175
}
176
176
177
177
private boolean isHighlighted (int i ) {
178
- return candidate_use_cursor && i >= 0 && i == highlightIndex ;
178
+ return candidateUseCursor && i >= 0 && i == highlightIndex ;
179
179
}
180
180
181
181
private void drawHighlight (Canvas canvas ) {
@@ -186,8 +186,9 @@ private void drawHighlight(Canvas canvas) {
186
186
}
187
187
188
188
private Typeface getFont (int codepoint , Typeface font ) {
189
- if (tfHanB != Typeface .DEFAULT && Character .isSupplementaryCodePoint (codepoint )) return tfHanB ;
190
- if (tfLatin != Typeface .DEFAULT && codepoint < 0x2e80 ) return tfLatin ;
189
+ if (hanBTypeface != Typeface .DEFAULT && Character .isSupplementaryCodePoint (codepoint ))
190
+ return hanBTypeface ;
191
+ if (latinTypeface != Typeface .DEFAULT && codepoint < 0x2e80 ) return latinTypeface ;
191
192
return font ;
192
193
}
193
194
@@ -198,7 +199,8 @@ private void drawText(
198
199
if (length == 0 ) return ;
199
200
int points = s .codePointCount (0 , length );
200
201
float x = center - measureText (s , paint , font ) / 2 ;
201
- if (tfLatin != Typeface .DEFAULT || (tfHanB != Typeface .DEFAULT && length > points )) {
202
+ if (latinTypeface != Typeface .DEFAULT
203
+ || (hanBTypeface != Typeface .DEFAULT && length > points )) {
202
204
int offset = 0 ;
203
205
while (offset < length ) {
204
206
int codepoint = s .codePointAt (offset );
@@ -218,57 +220,58 @@ private void drawText(
218
220
private void drawCandidates (Canvas canvas ) {
219
221
if (candidates == null ) return ;
220
222
221
- float x ;
222
- float y ;
223
- int i = 0 ;
224
- float comment_x , comment_y ;
225
- float comment_width ;
226
- String candidate , comment ;
223
+ float candidateX ;
224
+
225
+ float commentX , commentY ;
226
+ float commentWidth ;
227
+ String candidate ;
227
228
228
- y = candidateRect [0 ].centerY () - (paintCandidate .ascent () + paintCandidate .descent ()) / 2 ;
229
- if (show_comment && comment_on_top ) y += comment_height / 2f ;
230
- comment_y = comment_height / 2f - (paintComment .ascent () + paintComment .descent ()) / 2 ;
231
- if (show_comment && !comment_on_top ) comment_y += candidateRect [0 ].bottom - comment_height ;
229
+ float candidateY =
230
+ candidateRect [0 ].centerY () - (candidatePaint .ascent () + candidatePaint .descent ()) / 2 ;
231
+ if (shouldShowComment && isCommentOnTop ) candidateY += commentHeight / 2f ;
232
+ commentY = commentHeight / 2f - (commentPaint .ascent () + commentPaint .descent ()) / 2 ;
233
+ if (shouldShowComment && !isCommentOnTop ) commentY += candidateRect [0 ].bottom - commentHeight ;
232
234
235
+ int i = 0 ;
233
236
while (i < num_candidates ) {
234
237
// Calculate a position where the text could be centered in the rectangle.
235
- x = candidateRect [i ].centerX ();
236
- if (show_comment ) {
237
- comment = getComment (i );
238
+ candidateX = candidateRect [i ].centerX ();
239
+ if (shouldShowComment ) {
240
+ final String comment = getComment (i );
238
241
if (!TextUtils .isEmpty (comment )) {
239
- comment_width = measureText (comment , paintComment , tfComment );
240
- if (comment_on_top ) {
241
- comment_x = candidateRect [i ].centerX ();
242
+ commentWidth = measureText (comment , commentPaint , commentTypeface );
243
+ if (isCommentOnTop ) {
244
+ commentX = candidateRect [i ].centerX ();
242
245
} else {
243
- x -= comment_width / 2 ;
244
- comment_x = candidateRect [i ].right - comment_width / 2 ;
246
+ candidateX -= commentWidth / 2 ;
247
+ commentX = candidateRect [i ].right - commentWidth / 2 ;
245
248
}
246
- paintComment .setColor (isHighlighted (i ) ? hilited_comment_text_color : comment_text_color );
247
- drawText (comment , canvas , paintComment , tfComment , comment_x , comment_y );
249
+ commentPaint .setColor (isHighlighted (i ) ? hilitedCommentTextColor : commentTextColor );
250
+ drawText (comment , canvas , commentPaint , commentTypeface , commentX , commentY );
248
251
}
249
252
}
250
- paintCandidate .setColor (
251
- isHighlighted (i ) ? hilited_candidate_text_color : candidate_text_color );
252
- drawText (getCandidate (i ), canvas , paintCandidate , tfCandidate , x , y );
253
+ candidatePaint .setColor (isHighlighted (i ) ? hilitedCandidateTextColor : candidateTextColor );
254
+ drawText (getCandidate (i ), canvas , candidatePaint , candidateTypeface , candidateX , candidateY );
253
255
// Draw the separator at the right edge of each candidate.
254
256
candidateSeparator .setBounds (
255
257
candidateRect [i ].right - candidateSeparator .getIntrinsicWidth (),
256
258
candidateRect [i ].top ,
257
- candidateRect [i ].right + candidate_spacing ,
259
+ candidateRect [i ].right + candidateSpacing ,
258
260
candidateRect [i ].bottom );
259
261
candidateSeparator .draw (canvas );
260
262
i ++;
261
263
}
262
264
for (int j = -4 ; j >= -5 ; j --) { // -4: left, -5: right
263
265
candidate = getCandidate (j );
264
266
if (candidate == null ) continue ;
265
- paintSymbol .setColor (isHighlighted (i ) ? hilited_comment_text_color : comment_text_color );
266
- x = candidateRect [i ].centerX () - measureText (candidate , paintSymbol , tfSymbol ) / 2 ;
267
- canvas .drawText (candidate , x , y , paintSymbol );
267
+ symbolPaint .setColor (isHighlighted (i ) ? hilitedCommentTextColor : commentTextColor );
268
+ candidateX =
269
+ candidateRect [i ].centerX () - measureText (candidate , symbolPaint , symbolTypeface ) / 2 ;
270
+ canvas .drawText (candidate , candidateX , candidateY , symbolPaint );
268
271
candidateSeparator .setBounds (
269
272
candidateRect [i ].right - candidateSeparator .getIntrinsicWidth (),
270
273
candidateRect [i ].top ,
271
- candidateRect [i ].right + candidate_spacing ,
274
+ candidateRect [i ].right + candidateSpacing ,
272
275
candidateRect [i ].bottom );
273
276
candidateSeparator .draw (canvas );
274
277
i ++;
@@ -291,18 +294,18 @@ private void updateCandidateWidth() {
291
294
final int bottom = getHeight ();
292
295
int i ;
293
296
int x = 0 ;
294
- if (Rime .hasLeft ()) x += getCandidateWidth (-4 ) + candidate_spacing ;
297
+ if (Rime .hasLeft ()) x += getCandidateWidth (-4 ) + candidateSpacing ;
295
298
getCandNum ();
296
299
for (i = 0 ; i < num_candidates ; i ++) {
297
300
candidateRect [i ] = new Rect (x , top , x += getCandidateWidth (i ), bottom );
298
- x += candidate_spacing ;
301
+ x += candidateSpacing ;
299
302
}
300
303
if (Rime .hasLeft ()) candidateRect [i ++] = new Rect (0 , top , (int ) getCandidateWidth (-4 ), bottom );
301
304
if (Rime .hasRight ()) candidateRect [i ++] = new Rect (x , top , x += getCandidateWidth (-5 ), bottom );
302
305
LayoutParams params = getLayoutParams ();
303
306
params .width = x ;
304
- params .height = candidate_view_height ;
305
- if (show_comment && comment_on_top ) params .height += comment_height ;
307
+ params .height = candidateViewHeight ;
308
+ if (shouldShowComment && isCommentOnTop ) params .height += commentHeight ;
306
309
setLayoutParams (params );
307
310
}
308
311
@@ -406,7 +409,8 @@ private float measureText(String s, Paint paint, Typeface font) {
406
409
int length = s .length ();
407
410
if (length == 0 ) return x ;
408
411
int points = s .codePointCount (0 , length );
409
- if (tfLatin != Typeface .DEFAULT || (tfHanB != Typeface .DEFAULT && length > points )) {
412
+ if (latinTypeface != Typeface .DEFAULT
413
+ || (hanBTypeface != Typeface .DEFAULT && length > points )) {
410
414
int offset = 0 ;
411
415
while (offset < length ) {
412
416
int codepoint = s .codePointAt (offset );
@@ -427,13 +431,13 @@ private float measureText(String s, Paint paint, Typeface font) {
427
431
private float getCandidateWidth (int i ) {
428
432
String s = getCandidate (i );
429
433
// float n = (s == null ? 0 : s.codePointCount(0, s.length()));
430
- float x = 2 * candidate_padding ;
431
- if (s != null ) x += measureText (s , paintCandidate , tfCandidate );
432
- if (i >= 0 && show_comment ) {
434
+ float x = 2 * candidatePadding ;
435
+ if (s != null ) x += measureText (s , candidatePaint , candidateTypeface );
436
+ if (i >= 0 && shouldShowComment ) {
433
437
String comment = getComment (i );
434
438
if (comment != null ) {
435
- float x2 = measureText (comment , paintComment , tfComment );
436
- if (comment_on_top ) {
439
+ float x2 = measureText (comment , commentPaint , commentTypeface );
440
+ if (isCommentOnTop ) {
437
441
if (x2 > x ) x = x2 ;
438
442
} // 提示在上方
439
443
else x += x2 ; // 提示在右方
0 commit comments