Add keyLabelAlign attribute to Keyboard XML

Bug: 3216592
Change-Id: I88f8055199f2cd0525faf9374114fbebba53b951
main
Tadashi G. Takaoka 2010-11-20 08:03:24 -08:00
parent 45911256fd
commit cd39f2adee
11 changed files with 115 additions and 46 deletions

View File

@ -106,6 +106,14 @@
<attr name="keyOutputText" format="string" />
<!-- The label to display on the key. -->
<attr name="keyLabel" format="string" />
<!-- The key label option -->
<attr name="keyLabelOption">
<!-- This should be aligned with BaseKeyboardView.KEY_LABEL_OPTION_* -->
<flag name="alignLeft" value="1" />
<flag name="alignRight" value="2" />
<flag name="alignBottom" value="8" />
<flag name="fontItalic" value="16" />
</attr>
<!-- The unicode that this key generates in manual temporary upper case mode. -->
<attr name="manualTemporaryUpperCaseCode" format="integer" />
<!-- The icon to display on the key instead of the label. -->
@ -132,14 +140,14 @@
</declare-styleable>
<declare-styleable name="BaseKeyboard_Case">
<!-- This should be matched with KeyboardSwitcher.MODE_* -->
<!-- This should be aligned with KeyboardSwitcher.MODE_* -->
<attr name="mode">
<flag name="text" value="0" />
<flag name="url" value="1" />
<flag name="email" value="2" />
<flag name="im" value="3" />
<flag name="web" value="4" />
<flag name="phone" value="5" />
<enum name="text" value="0" />
<enum name="url" value="1" />
<enum name="email" value="2" />
<enum name="im" value="3" />
<enum name="web" value="4" />
<enum name="phone" value="5" />
</attr>
<attr name="settingsKey" format="string" />
<attr name="voiceKey" format="string" />

View File

@ -226,6 +226,12 @@
<string name="label_alpha_key">ABC</string>
<!-- Label for ALT modifier key. Must be short to fit on key! -->
<string name="label_alt_key">ALT</string>
<!-- Label for Backspace modifier key. Must be short to fit on key! -->
<string name="label_backspace_key">Backspace</string>
<!-- Label for Shift modifier key. Must be short to fit on key! -->
<string name="label_shift_key">Shift</string>
<!-- Label for Return key. Must be short to fit on key! -->
<string name="label_return_key">Return</string>
<!-- Label for Shift modifier key of symbol keyboard. Must be short to fit on key! -->
<string name="label_more_key">More</string>
<!-- Label for "Tab" key. Must be short to fit on key! [CHAR LIMIT=4]-->

View File

@ -30,6 +30,7 @@
<Key
latin:codes="@integer/key_tab"
latin:keyLabel="@string/label_tab_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="12.1%p"
latin:isModifier="true"
latin:keyEdgeFlags="left" />
@ -61,8 +62,8 @@
latin:horizontalGap="4.0%p" />
<Key
latin:codes="@integer/key_delete"
latin:keyIcon="@drawable/sym_keyboard_delete"
latin:iconPreview="@drawable/sym_keyboard_feedback_delete"
latin:keyLabel="@string/label_backspace_key"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="15.0%p"
latin:isModifier="true"
latin:isRepeatable="true"
@ -72,6 +73,7 @@
<Key
latin:codes="@integer/key_symbol"
latin:keyLabel="@string/label_more_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="12.1%p"
latin:isModifier="true"
latin:isSticky="true"
@ -104,8 +106,8 @@
latin:horizontalGap="4.0%p" />
<Key
latin:codes="@integer/key_return"
latin:keyIcon="@drawable/sym_keyboard_return"
latin:iconPreview="@drawable/sym_keyboard_feedback_return"
latin:keyLabel="@string/label_return_key"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="15.0%p"
latin:isModifier="true"
latin:keyEdgeFlags="right" />

View File

@ -30,6 +30,7 @@
<Key
latin:codes="@integer/key_tab"
latin:keyLabel="@string/label_tab_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="12.1%p"
latin:isModifier="true"
latin:keyEdgeFlags="left" />
@ -64,8 +65,8 @@
latin:horizontalGap="4.0%p" />
<Key
latin:codes="@integer/key_delete"
latin:keyIcon="@drawable/sym_keyboard_delete"
latin:iconPreview="@drawable/sym_keyboard_feedback_delete"
latin:keyLabel="@string/label_backspace_key"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="15.0%p"
latin:isModifier="true"
latin:isRepeatable="true"
@ -75,6 +76,7 @@
<Key
latin:codes="@integer/key_symbol"
latin:keyLabel="@string/label_more_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="12.1%p"
latin:isModifier="true"
latin:isSticky="true"
@ -110,8 +112,8 @@
latin:horizontalGap="4.0%p" />
<Key
latin:codes="@integer/key_return"
latin:keyIcon="@drawable/sym_keyboard_return"
latin:iconPreview="@drawable/sym_keyboard_feedback_return"
latin:keyLabel="@string/label_return_key"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="15.0%p"
latin:isModifier="true"
latin:keyEdgeFlags="right" />

View File

@ -25,7 +25,8 @@
<Row>
<Key
latin:codes="@integer/key_tab"
latin:keyLabel="Tab"
latin:keyLabel="@string/label_tab_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="7.0%p"
latin:isModifier="true"
latin:keyEdgeFlags="left" />

View File

@ -25,6 +25,7 @@
<Key
latin:codes="@integer/key_symbol"
latin:keyLabel="@string/label_symbol_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="9.7%p"
latin:isModifier="true"
latin:keyEdgeFlags="left" />
@ -75,8 +76,8 @@
</switch>
<Key
latin:codes="@integer/key_return"
latin:keyIcon="@drawable/sym_keyboard_return"
latin:iconPreview="@drawable/sym_keyboard_feedback_return"
latin:keyLabel="@string/label_return_key"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="8.3%p"
latin:isModifier="true"
latin:keyEdgeFlags="right" />

View File

@ -24,9 +24,9 @@
<Row>
<Key
latin:codes="@integer/key_shift"
latin:keyIcon="@drawable/sym_keyboard_shift"
latin:iconPreview="@drawable/sym_keyboard_feedback_shift"
latin:keyLabel="@string/label_shift_key"
latin:keyWidth="13.1%p"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:isModifier="true"
latin:isSticky="true"
latin:keyEdgeFlags="left" />
@ -80,8 +80,8 @@
</switch>
<Key
latin:codes="@integer/key_shift"
latin:keyIcon="@drawable/sym_keyboard_shift"
latin:iconPreview="@drawable/sym_keyboard_feedback_shift"
latin:keyLabel="@string/label_shift_key"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="13.1%p"
latin:isModifier="true"
latin:isSticky="true"

View File

@ -29,7 +29,8 @@
<Row>
<Key
latin:codes="@integer/key_tab"
latin:keyLabel="Tab"
latin:keyLabel="@string/label_tab_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="7.0%p"
latin:isModifier="true"
latin:keyEdgeFlags="left" />
@ -80,6 +81,7 @@
<Key
latin:codes="@integer/key_symbol"
latin:keyLabel="@string/label_alpha_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="9.7%p"
latin:isModifier="true"
latin:keyEdgeFlags="left" />
@ -119,8 +121,8 @@
latin:popupCharacters="]}&gt;" />
<Key
latin:codes="@integer/key_return"
latin:keyIcon="@drawable/sym_keyboard_return"
latin:iconPreview="@drawable/sym_keyboard_feedback_return"
latin:keyLabel="@string/label_return_key"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="8.3%p"
latin:isModifier="true"
latin:keyEdgeFlags="right" />
@ -129,7 +131,7 @@
<Key
latin:codes="@integer/key_shift"
latin:keyLabel="@string/label_more_key"
latin:iconPreview="@drawable/sym_keyboard_feedback_shift"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="13.1%p"
latin:isModifier="true"
latin:isSticky="true"
@ -162,6 +164,7 @@
latin:codes="@integer/key_shift"
latin:keyLabel="@string/label_more_key"
latin:iconPreview="@drawable/sym_keyboard_feedback_shift"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="13.1%p"
latin:isModifier="true"
latin:isSticky="true"

View File

@ -29,7 +29,8 @@
<Row>
<Key
latin:codes="@integer/key_tab"
latin:keyLabel="Tab"
latin:keyLabel="@string/label_tab_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="7.0%p"
latin:isModifier="true"
latin:keyEdgeFlags="left" />
@ -70,6 +71,7 @@
<Key
latin:codes="@integer/key_symbol"
latin:keyLabel="@string/label_alpha_key"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="9.7%p"
latin:isModifier="true"
latin:keyEdgeFlags="left" />
@ -99,8 +101,8 @@
latin:keyLabel="]" />
<Key
latin:codes="@integer/key_return"
latin:keyIcon="@drawable/sym_keyboard_return"
latin:iconPreview="@drawable/sym_keyboard_feedback_return"
latin:keyLabel="@string/label_return_key"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="8.3%p"
latin:isModifier="true"
latin:keyEdgeFlags="right" />
@ -109,7 +111,7 @@
<Key
latin:codes="@integer/key_shift"
latin:keyLabel="@string/label_more_key"
latin:iconPreview="@drawable/sym_keyboard_feedback_shift"
latin:keyLabelOption="fontItalic|alignLeft|alignBottom"
latin:keyWidth="13.1%p"
latin:isModifier="true"
latin:isSticky="true"
@ -143,7 +145,7 @@
<Key
latin:codes="@integer/key_shift"
latin:keyLabel="@string/label_more_key"
latin:iconPreview="@drawable/sym_keyboard_feedback_shift"
latin:keyLabelOption="fontItalic|alignRight|alignBottom"
latin:keyWidth="13.1%p"
latin:isModifier="true"
latin:isSticky="true"

View File

@ -183,6 +183,8 @@ public class BaseKeyboard {
/** Label to display */
public CharSequence label;
/** Option of the label */
public int labelOption;
/** Icon to display instead of a label. Icon takes precedence over a label */
public Drawable icon;
@ -331,6 +333,7 @@ public class BaseKeyboard {
setDefaultBounds(manualTemporaryUpperCaseHintIcon);
label = a.getText(R.styleable.BaseKeyboard_Key_keyLabel);
labelOption = a.getInt(R.styleable.BaseKeyboard_Key_keyLabelOption, 0);
manualTemporaryUpperCaseCode = a.getInt(
R.styleable.BaseKeyboard_Key_manualTemporaryUpperCaseCode, 0);
text = a.getText(R.styleable.BaseKeyboard_Key_keyOutputText);

View File

@ -249,9 +249,20 @@ public class BaseKeyboardView extends View implements PointerTracker.UIProxy {
private final Rect mClipRegion = new Rect(0, 0, 0, 0);
// This map caches key label text height in pixel as value and key label text size as map key.
private final HashMap<Integer, Integer> mTextHeightCache = new HashMap<Integer, Integer>();
// Distance from horizontal center of the key, proportional to key label text height.
private final float KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR = 0.55f;
private final String KEY_LABEL_HEIGHT_REFERENCE_CHAR = "H";
// This map caches key label text width in pixel as value and key label text size as map key.
private final HashMap<Integer, Integer> mTextWidthCache = new HashMap<Integer, Integer>();
// Distance from horizontal center of the key, proportional to key label text height and width.
private final float KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR_CENTER = 0.55f;
private final float KEY_LABEL_VERTICAL_PADDING_FACTOR = 1.60f;
private final float KEY_LABEL_HORIZONTAL_PADDING_FACTOR = 0.80f;
private final String KEY_LABEL_REFERENCE_CHAR = "H";
private final int KEY_LABEL_OPTION_ALIGN_LEFT = 1;
private final int KEY_LABEL_OPTION_ALIGN_RIGHT = 2;
private final int KEY_LABEL_OPTION_ALIGN_BOTTOM = 8;
private final int KEY_LABEL_OPTION_FONT_ITALIC = 16;
// TODO: Currently we don't have san-serif italic type face. This is actually san-serif
// non-italic type face.
private final Typeface TYPEFACE_ITALIC = Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC);
private final UIHandler mHandler = new UIHandler();
@ -808,12 +819,17 @@ public class BaseKeyboardView extends View implements PointerTracker.UIProxy {
keyBackground.draw(canvas);
boolean drawHintIcon = true;
// Draw key label
if (label != null) {
// For characters, use large font. For labels like "Done", use small font.
final int labelSize;
if (label.length() > 1 && key.codes.length < 2) {
labelSize = mLabelTextSize;
paint.setTypeface(Typeface.DEFAULT_BOLD);
if ((key.labelOption & KEY_LABEL_OPTION_FONT_ITALIC) != 0) {
paint.setTypeface(TYPEFACE_ITALIC);
} else {
paint.setTypeface(Typeface.DEFAULT_BOLD);
}
} else {
labelSize = mKeyTextSize;
paint.setTypeface(mKeyTextStyle);
@ -821,26 +837,51 @@ public class BaseKeyboardView extends View implements PointerTracker.UIProxy {
paint.setTextSize(labelSize);
Integer labelHeightValue = mTextHeightCache.get(labelSize);
final int labelHeight;
final int labelCharHeight;
final int labelCharWidth;
if (labelHeightValue != null) {
labelHeight = labelHeightValue;
labelCharHeight = labelHeightValue;
labelCharWidth = mTextWidthCache.get(labelSize);
} else {
Rect textBounds = new Rect();
paint.getTextBounds(KEY_LABEL_HEIGHT_REFERENCE_CHAR, 0, 1, textBounds);
labelHeight = textBounds.height();
mTextHeightCache.put(labelSize, labelHeight);
paint.getTextBounds(KEY_LABEL_REFERENCE_CHAR, 0, 1, textBounds);
labelCharHeight = textBounds.height();
labelCharWidth = textBounds.width();
mTextHeightCache.put(labelSize, labelCharHeight);
mTextWidthCache.put(labelSize, labelCharWidth);
}
// Draw a drop shadow for the text
// Vertical label text alignment.
final float baseline;
if ((key.labelOption & KEY_LABEL_OPTION_ALIGN_BOTTOM) != 0) {
baseline = key.height -
+ labelCharHeight * KEY_LABEL_VERTICAL_PADDING_FACTOR;
} else { // Align center
final float centerY = (key.height + padding.top - padding.bottom) / 2;
baseline = centerY
+ labelCharHeight * KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR_CENTER;
}
// Horizontal label text alignment
final int positionX;
if ((key.labelOption & KEY_LABEL_OPTION_ALIGN_LEFT) != 0) {
positionX = (int)(
labelCharWidth * KEY_LABEL_HORIZONTAL_PADDING_FACTOR + padding.left);
paint.setTextAlign(Align.LEFT);
} else if ((key.labelOption & KEY_LABEL_OPTION_ALIGN_RIGHT) != 0) {
positionX = (int)(key.width
- labelCharWidth * KEY_LABEL_HORIZONTAL_PADDING_FACTOR - padding.right);
paint.setTextAlign(Align.RIGHT);
} else {
positionX = (key.width + padding.left - padding.right) / 2;
paint.setTextAlign(Align.CENTER);
}
// Set a drop shadow for the text
paint.setShadowLayer(mShadowRadius, 0, 0, mShadowColor);
final int centerX = (key.width + padding.left - padding.right) / 2;
final int centerY = (key.height + padding.top - padding.bottom) / 2;
final float baseline = centerY
+ labelHeight * KEY_LABEL_VERTICAL_ADJUSTMENT_FACTOR;
canvas.drawText(label, centerX, baseline, paint);
canvas.drawText(label, positionX, baseline, paint);
// Turn off drop shadow
paint.setShadowLayer(0, 0, 0, 0);
}
// Draw key icon
if (key.label == null && key.icon != null) {
int drawableWidth = key.icon.getIntrinsicWidth();
int drawableHeight = key.icon.getIntrinsicHeight();