diff options
author | 2011-08-02 21:29:24 -0700 | |
---|---|---|
committer | 2011-08-03 01:53:30 -0700 | |
commit | 62d4a96497951d4de784156f86626efdd55813fe (patch) | |
tree | 175295325e23029fb5961ab745ef2051a89e6df4 /java/src | |
parent | b320134f0104c7ddf6b6677850b034ccb85755a9 (diff) | |
download | latinime-62d4a96497951d4de784156f86626efdd55813fe.tar.gz latinime-62d4a96497951d4de784156f86626efdd55813fe.tar.xz latinime-62d4a96497951d4de784156f86626efdd55813fe.zip |
Support label with icon key
Bug: 5108339
Change-Id: I7cd4258a95a7e25b8b1114b43ffcca5b77c5c1e3
Diffstat (limited to 'java/src')
3 files changed, 76 insertions, 30 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/Key.java b/java/src/com/android/inputmethod/keyboard/Key.java index 57a354ecf..4cc0bba6a 100644 --- a/java/src/com/android/inputmethod/keyboard/Key.java +++ b/java/src/com/android/inputmethod/keyboard/Key.java @@ -51,10 +51,10 @@ public class Key { /** Hint label to display on the key in conjunction with the label */ public final CharSequence mHintLabel; /** Option of the label */ - public final int mLabelOption; - public static final int LABEL_OPTION_ALIGN_LEFT = 0x01; - public static final int LABEL_OPTION_ALIGN_RIGHT = 0x02; - public static final int LABEL_OPTION_ALIGN_LEFT_OF_CENTER = 0x08; + private final int mLabelOption; + private static final int LABEL_OPTION_ALIGN_LEFT = 0x01; + private static final int LABEL_OPTION_ALIGN_RIGHT = 0x02; + private static final int LABEL_OPTION_ALIGN_LEFT_OF_CENTER = 0x08; private static final int LABEL_OPTION_LARGE_LETTER = 0x10; private static final int LABEL_OPTION_FONT_NORMAL = 0x20; private static final int LABEL_OPTION_FONT_MONO_SPACE = 0x40; @@ -63,6 +63,8 @@ public class Key { private static final int LABEL_OPTION_HAS_POPUP_HINT = 0x200; private static final int LABEL_OPTION_HAS_UPPERCASE_LETTER = 0x400; private static final int LABEL_OPTION_HAS_HINT_LABEL = 0x800; + private static final int LABEL_OPTION_WITH_ICON_LEFT = 0x1000; + private static final int LABEL_OPTION_WITH_ICON_RIGHT = 0x2000; /** Icon to display instead of a label. Icon takes precedence over a label */ private Drawable mIcon; @@ -384,6 +386,18 @@ public class Key { } } + public boolean isAlignLeft() { + return (mLabelOption & LABEL_OPTION_ALIGN_LEFT) != 0; + } + + public boolean isAlignRight() { + return (mLabelOption & LABEL_OPTION_ALIGN_RIGHT) != 0; + } + + public boolean isAlignLeftOfCenter() { + return (mLabelOption & LABEL_OPTION_ALIGN_LEFT_OF_CENTER) != 0; + } + public boolean hasPopupHint() { return (mLabelOption & LABEL_OPTION_HAS_POPUP_HINT) != 0; } @@ -396,6 +410,14 @@ public class Key { return (mLabelOption & LABEL_OPTION_HAS_HINT_LABEL) != 0; } + public boolean hasLabelWithIconLeft() { + return (mLabelOption & LABEL_OPTION_WITH_ICON_LEFT) != 0; + } + + public boolean hasLabelWithIconRight() { + return (mLabelOption & LABEL_OPTION_WITH_ICON_RIGHT) != 0; + } + public Drawable getIcon() { return mIcon; } diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index d1fd7e3ca..aab58b52c 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -505,6 +505,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { } // Draw key label. + final Drawable icon = key.getIcon(); float positionX = centerX; if (key.mLabel != null) { // Switch the character to uppercase if shift is pressed @@ -521,16 +522,25 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { final float baseline = centerY + labelCharHeight / 2; // Horizontal label text alignment - if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_LEFT) != 0) { + float labelWidth = 0; + if (key.isAlignLeft()) { positionX = (int)params.mKeyLabelHorizontalPadding; paint.setTextAlign(Align.LEFT); - } else if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_RIGHT) != 0) { + } else if (key.isAlignRight()) { positionX = keyWidth - (int)params.mKeyLabelHorizontalPadding; paint.setTextAlign(Align.RIGHT); - } else if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_LEFT_OF_CENTER) != 0) { + } else if (key.isAlignLeftOfCenter()) { // TODO: Parameterise this? positionX = centerX - labelCharWidth * 7 / 4; paint.setTextAlign(Align.LEFT); + } else if (key.hasLabelWithIconLeft() && icon != null) { + labelWidth = getLabelWidth(label, paint) + icon.getIntrinsicWidth(); + positionX = centerX + labelWidth / 2; + paint.setTextAlign(Align.RIGHT); + } else if (key.hasLabelWithIconRight() && icon != null) { + labelWidth = getLabelWidth(label, paint) + icon.getIntrinsicWidth(); + positionX = centerX - labelWidth / 2; + paint.setTextAlign(Align.LEFT); } else { positionX = centerX; paint.setTextAlign(Align.CENTER); @@ -552,6 +562,19 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { // Turn off drop shadow paint.setShadowLayer(0, 0, 0, 0); + if (icon != null) { + final int iconWidth = icon.getIntrinsicWidth(); + final int iconHeight = icon.getIntrinsicHeight(); + final int iconY = (keyHeight - iconHeight) / 2; + if (key.hasLabelWithIconLeft()) { + final int iconX = (int)(centerX - labelWidth / 2); + drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight); + } else if (key.hasLabelWithIconRight()) { + final int iconX = (int)(centerX + labelWidth / 2 - iconWidth); + drawIcon(canvas, icon, iconX, iconY, iconWidth, iconHeight); + } + } + if (debugShowAlign) { final Paint line = new Paint(); drawHorizontalLine(canvas, baseline, keyWidth, 0xc0008000, line); @@ -605,16 +628,15 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { } // Draw key icon. - final Drawable icon = key.getIcon(); if (key.mLabel == null && icon != null) { final int iconWidth = icon.getIntrinsicWidth(); final int iconHeight = icon.getIntrinsicHeight(); final int iconX, alignX; final int iconY = (keyHeight - iconHeight) / 2; - if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_LEFT) != 0) { + if (key.isAlignLeft()) { iconX = (int)params.mKeyLabelHorizontalPadding; alignX = iconX; - } else if ((key.mLabelOption & Key.LABEL_OPTION_ALIGN_RIGHT) != 0) { + } else if (key.isAlignRight()) { iconX = keyWidth - (int)params.mKeyLabelHorizontalPadding - iconWidth; alignX = iconX + iconWidth; } else { // Align center @@ -694,6 +716,11 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { return width; } + private static float getLabelWidth(CharSequence label, Paint paint) { + paint.getTextBounds(label.toString(), 0, label.length(), sTextBounds); + return sTextBounds.width(); + } + private static void drawIcon(Canvas canvas, Drawable icon, int x, int y, int width, int height) { canvas.translate(x, y); diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java index ed4608bf6..2d8b7bf11 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardIconsSet.java @@ -30,23 +30,22 @@ public class KeyboardIconsSet { // This should be aligned with Keyboard.keyIcon enum. private static final int ICON_SHIFT_KEY = 1; - private static final int ICON_TO_SYMBOL_KEY = 2; - private static final int ICON_TO_SYMBOL_KEY_WITH_SHORTCUT = 3; - private static final int ICON_DELETE_KEY = 4; - private static final int ICON_SETTINGS_KEY = 5; // This is also represented as "@icon/5" in xml. - private static final int ICON_SHORTCUT_KEY = 6; - private static final int ICON_SPACE_KEY = 7; - private static final int ICON_RETURN_KEY = 8; - private static final int ICON_SEARCH_KEY = 9; - private static final int ICON_TAB_KEY = 10; + private static final int ICON_DELETE_KEY = 2; + private static final int ICON_SETTINGS_KEY = 3; // This is also represented as "@icon/3" in XML. + private static final int ICON_SPACE_KEY = 4; + private static final int ICON_RETURN_KEY = 5; + private static final int ICON_SEARCH_KEY = 6; + private static final int ICON_TAB_KEY = 7; // This is also represented as "@icon/7" in XML. + private static final int ICON_SHORTCUT_KEY = 8; + private static final int ICON_SHORTCUT_FOR_LABEL = 9; // This should be aligned with Keyboard.keyIconShifted enum. - private static final int ICON_SHIFTED_SHIFT_KEY = 11; + private static final int ICON_SHIFTED_SHIFT_KEY = 10; // This should be aligned with Keyboard.keyIconPreview enum. - private static final int ICON_PREVIEW_TAB_KEY = 12; - private static final int ICON_PREVIEW_SETTINGS_KEY = 13; - private static final int ICON_PREVIEW_SHORTCUT_KEY = 14; + private static final int ICON_PREVIEW_TAB_KEY = 11; + private static final int ICON_PREVIEW_SETTINGS_KEY = 12; + private static final int ICON_PREVIEW_SHORTCUT_KEY = 13; - private static final int ICON_LAST = 14; + private static final int ICON_LAST = 13; private final Drawable mIcons[] = new Drawable[ICON_LAST + 1]; @@ -54,16 +53,10 @@ public class KeyboardIconsSet { switch (attrIndex) { case R.styleable.Keyboard_iconShiftKey: return ICON_SHIFT_KEY; - case R.styleable.Keyboard_iconToSymbolKey: - return ICON_TO_SYMBOL_KEY; - case R.styleable.Keyboard_iconToSymbolKeyWithShortcut: - return ICON_TO_SYMBOL_KEY_WITH_SHORTCUT; case R.styleable.Keyboard_iconDeleteKey: return ICON_DELETE_KEY; case R.styleable.Keyboard_iconSettingsKey: return ICON_SETTINGS_KEY; - case R.styleable.Keyboard_iconShortcutKey: - return ICON_SHORTCUT_KEY; case R.styleable.Keyboard_iconSpaceKey: return ICON_SPACE_KEY; case R.styleable.Keyboard_iconReturnKey: @@ -72,6 +65,10 @@ public class KeyboardIconsSet { return ICON_SEARCH_KEY; case R.styleable.Keyboard_iconTabKey: return ICON_TAB_KEY; + case R.styleable.Keyboard_iconShortcutKey: + return ICON_SHORTCUT_KEY; + case R.styleable.Keyboard_iconShortcutForLabel: + return ICON_SHORTCUT_FOR_LABEL; case R.styleable.Keyboard_iconShiftedShiftKey: return ICON_SHIFTED_SHIFT_KEY; case R.styleable.Keyboard_iconPreviewTabKey: |