Can override key drawing methods of KeyboardView

Change-Id: Ie303df4f5e944ea868f5c28ba3bb72a57cc9a35f
main
Tadashi G. Takaoka 2011-12-18 02:58:13 +09:00
parent 605f730959
commit bc781e963f
1 changed files with 61 additions and 51 deletions

View File

@ -194,7 +194,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
} }
} }
private static class KeyDrawParams { /* package */ static class KeyDrawParams {
// XML attributes // XML attributes
public final int mKeyTextColor; public final int mKeyTextColor;
public final int mKeyTextInactivatedColor; public final int mKeyTextInactivatedColor;
@ -283,7 +283,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
} }
} }
protected static class KeyPreviewDrawParams { /* package */ static class KeyPreviewDrawParams {
// XML attributes. // XML attributes.
public final Drawable mPreviewBackground; public final Drawable mPreviewBackground;
public final Drawable mPreviewLeftBackground; public final Drawable mPreviewLeftBackground;
@ -474,7 +474,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
if (mKeyboard == null) return; if (mKeyboard == null) return;
final boolean isManualTemporaryUpperCase = mKeyboard.isManualTemporaryUpperCase();
final KeyDrawParams params = mKeyDrawParams; final KeyDrawParams params = mKeyDrawParams;
if (mInvalidatedKey != null && mInvalidatedKeyRect.contains(mDirtyRect)) { if (mInvalidatedKey != null && mInvalidatedKeyRect.contains(mDirtyRect)) {
// Draw a single key. // Draw a single key.
@ -482,8 +481,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
+ getPaddingLeft(); + getPaddingLeft();
final int keyDrawY = mInvalidatedKey.mY + getPaddingTop(); final int keyDrawY = mInvalidatedKey.mY + getPaddingTop();
canvas.translate(keyDrawX, keyDrawY); canvas.translate(keyDrawX, keyDrawY);
onBufferDrawKey(mInvalidatedKey, mKeyboard, canvas, mPaint, params, onDrawKey(mInvalidatedKey, canvas, mPaint, params);
isManualTemporaryUpperCase);
canvas.translate(-keyDrawX, -keyDrawY); canvas.translate(-keyDrawX, -keyDrawY);
} else { } else {
// Draw all keys. // Draw all keys.
@ -491,7 +489,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
final int keyDrawX = key.mX + key.mVisualInsetsLeft + getPaddingLeft(); final int keyDrawX = key.mX + key.mVisualInsetsLeft + getPaddingLeft();
final int keyDrawY = key.mY + getPaddingTop(); final int keyDrawY = key.mY + getPaddingTop();
canvas.translate(keyDrawX, keyDrawY); canvas.translate(keyDrawX, keyDrawY);
onBufferDrawKey(key, mKeyboard, canvas, mPaint, params, isManualTemporaryUpperCase); onDrawKey(key, canvas, mPaint, params);
canvas.translate(-keyDrawX, -keyDrawY); canvas.translate(-keyDrawX, -keyDrawY);
} }
} }
@ -514,38 +512,43 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
} }
} }
private static void onBufferDrawKey(final Key key, final Keyboard keyboard, final Canvas canvas, private void onDrawKey(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
Paint paint, KeyDrawParams params, boolean isManualTemporaryUpperCase) { if (key.isSpacer()) return;
final boolean debugShowAlign = LatinImeLogger.sVISUALDEBUG; onDrawKeyBackground(key, canvas, params);
// Draw key background. onDrawKeyTopVisuals(key, canvas, paint, params);
if (!key.isSpacer()) { }
final int bgWidth = key.mWidth - key.mVisualInsetsLeft - key.mVisualInsetsRight
+ params.mPadding.left + params.mPadding.right;
final int bgHeight = key.mHeight + params.mPadding.top + params.mPadding.bottom;
final int bgX = -params.mPadding.left;
final int bgY = -params.mPadding.top;
final int[] drawableState = key.getCurrentDrawableState();
final Drawable background = params.mKeyBackground;
background.setState(drawableState);
final Rect bounds = background.getBounds();
if (bgWidth != bounds.right || bgHeight != bounds.bottom) {
background.setBounds(0, 0, bgWidth, bgHeight);
}
canvas.translate(bgX, bgY);
background.draw(canvas);
if (debugShowAlign) {
drawRectangle(canvas, 0, 0, bgWidth, bgHeight, 0x80c00000, new Paint());
}
canvas.translate(-bgX, -bgY);
}
// Draw key top visuals. // Draw key background.
/* package */ void onDrawKeyBackground(Key key, Canvas canvas, KeyDrawParams params) {
final int bgWidth = key.mWidth - key.mVisualInsetsLeft - key.mVisualInsetsRight
+ params.mPadding.left + params.mPadding.right;
final int bgHeight = key.mHeight + params.mPadding.top + params.mPadding.bottom;
final int bgX = -params.mPadding.left;
final int bgY = -params.mPadding.top;
final int[] drawableState = key.getCurrentDrawableState();
final Drawable background = params.mKeyBackground;
background.setState(drawableState);
final Rect bounds = background.getBounds();
if (bgWidth != bounds.right || bgHeight != bounds.bottom) {
background.setBounds(0, 0, bgWidth, bgHeight);
}
canvas.translate(bgX, bgY);
background.draw(canvas);
if (LatinImeLogger.sVISUALDEBUG) {
drawRectangle(canvas, 0, 0, bgWidth, bgHeight, 0x80c00000, new Paint());
}
canvas.translate(-bgX, -bgY);
}
// Draw key top visuals.
/* package */ void onDrawKeyTopVisuals(Key key, Canvas canvas, Paint paint,
KeyDrawParams params) {
final int keyWidth = key.mWidth - key.mVisualInsetsLeft - key.mVisualInsetsRight; final int keyWidth = key.mWidth - key.mVisualInsetsLeft - key.mVisualInsetsRight;
final int keyHeight = key.mHeight; final int keyHeight = key.mHeight;
final float centerX = keyWidth * 0.5f; final float centerX = keyWidth * 0.5f;
final float centerY = keyHeight * 0.5f; final float centerY = keyHeight * 0.5f;
if (debugShowAlign) { if (LatinImeLogger.sVISUALDEBUG) {
drawRectangle(canvas, 0, 0, keyWidth, keyHeight, 0x800000c0, new Paint()); drawRectangle(canvas, 0, 0, keyWidth, keyHeight, 0x800000c0, new Paint());
} }
@ -554,7 +557,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
float positionX = centerX; float positionX = centerX;
if (key.mLabel != null) { if (key.mLabel != null) {
// Switch the character to uppercase if shift is pressed // Switch the character to uppercase if shift is pressed
final CharSequence label = keyboard.adjustLabelCase(key.mLabel); final CharSequence label = mKeyboard.adjustLabelCase(key.mLabel);
// For characters, use large font. For labels like "Done", use smaller font. // For characters, use large font. For labels like "Done", use smaller font.
paint.setTypeface(key.selectTypeface(params.mKeyTextStyle)); paint.setTypeface(key.selectTypeface(params.mKeyTextStyle));
final int labelSize = key.selectTextSize(params.mKeyLetterSize, final int labelSize = key.selectTextSize(params.mKeyLetterSize,
@ -597,7 +600,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
Math.min(1.0f, (keyWidth * MAX_LABEL_RATIO) / getLabelWidth(label, paint))); Math.min(1.0f, (keyWidth * MAX_LABEL_RATIO) / getLabelWidth(label, paint)));
} }
if (key.hasUppercaseLetter() && isManualTemporaryUpperCase) { if (key.hasUppercaseLetter() && mKeyboard.isManualTemporaryUpperCase()) {
paint.setColor(params.mKeyTextInactivatedColor); paint.setColor(params.mKeyTextInactivatedColor);
} else { } else {
paint.setColor(params.mKeyTextColor); paint.setColor(params.mKeyTextColor);
@ -627,7 +630,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
} }
} }
if (debugShowAlign) { if (LatinImeLogger.sVISUALDEBUG) {
final Paint line = new Paint(); final Paint line = new Paint();
drawHorizontalLine(canvas, baseline, keyWidth, 0xc0008000, line); drawHorizontalLine(canvas, baseline, keyWidth, 0xc0008000, line);
drawVerticalLine(canvas, positionX, keyHeight, 0xc0800080, line); drawVerticalLine(canvas, positionX, keyHeight, 0xc0800080, line);
@ -644,7 +647,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
hintSize = params.mKeyHintLabelSize; hintSize = params.mKeyHintLabelSize;
paint.setTypeface(Typeface.DEFAULT); paint.setTypeface(Typeface.DEFAULT);
} else if (key.hasUppercaseLetter()) { } else if (key.hasUppercaseLetter()) {
hintColor = isManualTemporaryUpperCase hintColor = mKeyboard.isManualTemporaryUpperCase()
? params.mKeyUppercaseLetterActivatedColor ? params.mKeyUppercaseLetterActivatedColor
: params.mKeyUppercaseLetterInactivatedColor; : params.mKeyUppercaseLetterInactivatedColor;
hintSize = params.mKeyUppercaseLetterSize; hintSize = params.mKeyUppercaseLetterSize;
@ -677,7 +680,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
} }
canvas.drawText(hint, 0, hint.length(), hintX, hintY, paint); canvas.drawText(hint, 0, hint.length(), hintX, hintY, paint);
if (debugShowAlign) { if (LatinImeLogger.sVISUALDEBUG) {
final Paint line = new Paint(); final Paint line = new Paint();
drawHorizontalLine(canvas, (int)hintY, keyWidth, 0xc0808000, line); drawHorizontalLine(canvas, (int)hintY, keyWidth, 0xc0808000, line);
drawVerticalLine(canvas, (int)hintX, keyHeight, 0xc0808000, line); drawVerticalLine(canvas, (int)hintX, keyHeight, 0xc0808000, line);
@ -702,29 +705,36 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
} }
drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight); drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight);
if (debugShowAlign) { if (LatinImeLogger.sVISUALDEBUG) {
final Paint line = new Paint(); final Paint line = new Paint();
drawVerticalLine(canvas, alignX, keyHeight, 0xc0800080, line); drawVerticalLine(canvas, alignX, keyHeight, 0xc0800080, line);
drawRectangle(canvas, iconX, iconY, iconWidth, iconHeight, 0x80c00000, line); drawRectangle(canvas, iconX, iconY, iconWidth, iconHeight, 0x80c00000, line);
} }
} }
// Draw popup hint "..." at the bottom right corner of the key.
if ((key.hasPopupHint() && key.mMoreKeys != null && key.mMoreKeys.length > 0) if ((key.hasPopupHint() && key.mMoreKeys != null && key.mMoreKeys.length > 0)
|| key.needsSpecialPopupHint()) { || key.needsSpecialPopupHint()) {
paint.setTextSize(params.mKeyHintLetterSize); drawKeyPopupHint(key, canvas, paint, params);
paint.setColor(params.mKeyHintLabelColor); }
paint.setTextAlign(Align.CENTER); }
final float hintX = keyWidth - params.mKeyHintLetterPadding
- getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2;
final float hintY = keyHeight - params.mKeyPopupHintLetterPadding;
canvas.drawText(POPUP_HINT_CHAR, hintX, hintY, paint);
if (debugShowAlign) { // Draw popup hint "..." at the bottom right corner of the key.
final Paint line = new Paint(); /* package */ void drawKeyPopupHint(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
drawHorizontalLine(canvas, (int)hintY, keyWidth, 0xc0808000, line); final int keyWidth = key.mWidth - key.mVisualInsetsLeft - key.mVisualInsetsRight;
drawVerticalLine(canvas, (int)hintX, keyHeight, 0xc0808000, line); final int keyHeight = key.mHeight;
}
paint.setTextSize(params.mKeyHintLetterSize);
paint.setColor(params.mKeyHintLabelColor);
paint.setTextAlign(Align.CENTER);
final float hintX = keyWidth - params.mKeyHintLetterPadding
- getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2;
final float hintY = keyHeight - params.mKeyPopupHintLetterPadding;
canvas.drawText(POPUP_HINT_CHAR, hintX, hintY, paint);
if (LatinImeLogger.sVISUALDEBUG) {
final Paint line = new Paint();
drawHorizontalLine(canvas, (int)hintY, keyWidth, 0xc0808000, line);
drawVerticalLine(canvas, (int)hintX, keyHeight, 0xc0808000, line);
} }
} }