From ba49920e8046ca592d6c914b2e3f7394bf9c281c Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 7 Aug 2014 14:12:01 +0900 Subject: [PATCH] Add Key label off center attribute This CL adds two Key attributes to control how much a label should be off centered. - keyLabelOffCenterRatio - keyHintLabelOffCenterRatio Bug: 14419121 Change-Id: I26650eefdd375f0eeca8a1ca6034a8339d2a3182 --- java/res/values/attrs.xml | 10 ++++++- java/res/values/themes-common.xml | 2 ++ java/res/xml/key_styles_number.xml | 2 +- .../com/android/inputmethod/keyboard/Key.java | 6 ++-- .../inputmethod/keyboard/KeyboardView.java | 13 ++++----- .../keyboard/internal/KeyDrawParams.java | 28 +++++++++++++++++-- .../internal/KeyVisualAttributes.java | 8 ++++++ 7 files changed, 53 insertions(+), 16 deletions(-) diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml index 37ba3963b..91cd01723 100644 --- a/java/res/values/attrs.xml +++ b/java/res/values/attrs.xml @@ -293,7 +293,7 @@ - + @@ -368,6 +368,14 @@ + + + + diff --git a/java/res/values/themes-common.xml b/java/res/values/themes-common.xml index b139110ca..02e671b5d 100644 --- a/java/res/values/themes-common.xml +++ b/java/res/values/themes-common.xml @@ -38,6 +38,8 @@ @fraction/config_key_hint_letter_ratio @fraction/config_key_hint_label_ratio @fraction/config_key_shifted_letter_hint_ratio + -175% + 200% normal @dimen/config_key_hint_letter_padding @dimen/config_key_popup_hint_letter_padding diff --git a/java/res/xml/key_styles_number.xml b/java/res/xml/key_styles_number.xml index 3038097d8..14b202874 100644 --- a/java/res/xml/key_styles_number.xml +++ b/java/res/xml/key_styles_number.xml @@ -39,7 +39,7 @@ latin:parentStyle="numKeyBaseStyle" /> { /** Flags of the label */ private final int mLabelFlags; private static final int LABEL_FLAGS_ALIGN_ICON_TO_BOTTOM = 0x04; - private static final int LABEL_FLAGS_ALIGN_LEFT_OF_CENTER = 0x08; + private static final int LABEL_FLAGS_ALIGN_LABEL_OFF_CENTER = 0x08; // Font typeface specification. private static final int LABEL_FLAGS_FONT_MASK = 0x30; private static final int LABEL_FLAGS_FONT_NORMAL = 0x10; @@ -648,8 +648,8 @@ public class Key implements Comparable { return (mLabelFlags & LABEL_FLAGS_ALIGN_ICON_TO_BOTTOM) != 0; } - public final boolean isAlignLeftOfCenter() { - return (mLabelFlags & LABEL_FLAGS_ALIGN_LEFT_OF_CENTER) != 0; + public final boolean isAlignLabelOffCenter() { + return (mLabelFlags & LABEL_FLAGS_ALIGN_LABEL_OFF_CENTER) != 0; } public final boolean hasPopupHint() { diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index 4a791f325..72b254756 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -369,9 +369,9 @@ public class KeyboardView extends View { final float baseline = centerY + labelCharHeight / 2.0f; // Horizontal label text alignment - if (key.isAlignLeftOfCenter()) { - // TODO: Parameterise this? - positionX = centerX - labelCharWidth * 7.0f / 4.0f; + if (key.isAlignLabelOffCenter()) { + // The label is placed off center of the key. Used mainly on "phone number" layout. + positionX = centerX + params.mLabelOffCenterRatio * labelCharWidth; paint.setTextAlign(Align.LEFT); } else { positionX = centerX; @@ -418,15 +418,12 @@ public class KeyboardView extends View { blendAlpha(paint, params.mAnimAlpha); final float labelCharHeight = TypefaceUtils.getReferenceCharHeight(paint); final float labelCharWidth = TypefaceUtils.getReferenceCharWidth(paint); - final KeyVisualAttributes visualAttr = key.getVisualAttributes(); - final float adjustmentY = (visualAttr == null) ? 0.0f - : visualAttr.mHintLabelVerticalAdjustment * labelCharHeight; + final float adjustmentY = params.mHintLabelVerticalAdjustment * labelCharHeight; final float hintX, hintY; if (key.hasHintLabel()) { // The hint label is placed just right of the key label. Used mainly on // "phone number" layout. - // TODO: Generalize the following calculations. - hintX = positionX + labelCharWidth * 2.0f; + hintX = positionX + params.mHintLabelOffCenterRatio * labelCharWidth; hintY = centerY + labelCharHeight / 2.0f; paint.setTextAlign(Align.LEFT); } else if (key.hasShiftedLetterHint()) { diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java b/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java index 07ac06bab..6dbee55ae 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyDrawParams.java @@ -42,6 +42,10 @@ public final class KeyDrawParams { public int mShiftedLetterHintActivatedColor; public int mPreviewTextColor; + public float mHintLabelVerticalAdjustment; + public float mLabelOffCenterRatio; + public float mHintLabelOffCenterRatio; + public int mAnimAlpha; public KeyDrawParams() {} @@ -68,6 +72,10 @@ public final class KeyDrawParams { mShiftedLetterHintActivatedColor = copyFrom.mShiftedLetterHintActivatedColor; mPreviewTextColor = copyFrom.mPreviewTextColor; + mHintLabelVerticalAdjustment = copyFrom.mHintLabelVerticalAdjustment; + mLabelOffCenterRatio = copyFrom.mLabelOffCenterRatio; + mHintLabelOffCenterRatio = copyFrom.mHintLabelOffCenterRatio; + mAnimAlpha = copyFrom.mAnimAlpha; } @@ -103,6 +111,13 @@ public final class KeyDrawParams { mShiftedLetterHintActivatedColor = selectColor( attr.mShiftedLetterHintActivatedColor, mShiftedLetterHintActivatedColor); mPreviewTextColor = selectColor(attr.mPreviewTextColor, mPreviewTextColor); + + mHintLabelVerticalAdjustment = selectFloatIfNonZero( + attr.mHintLabelVerticalAdjustment, mHintLabelVerticalAdjustment); + mLabelOffCenterRatio = selectFloatIfNonZero( + attr.mLabelOffCenterRatio, mLabelOffCenterRatio); + mHintLabelOffCenterRatio = selectFloatIfNonZero( + attr.mHintLabelOffCenterRatio, mHintLabelOffCenterRatio); } public KeyDrawParams mayCloneAndUpdateParams(final int keyHeight, @@ -115,7 +130,7 @@ public final class KeyDrawParams { return newParams; } - private static final int selectTextSizeFromDimensionOrRatio(final int keyHeight, + private static int selectTextSizeFromDimensionOrRatio(final int keyHeight, final int dimens, final float ratio, final int defaultDimens) { if (ResourceUtils.isValidDimensionPixelSize(dimens)) { return dimens; @@ -126,7 +141,7 @@ public final class KeyDrawParams { return defaultDimens; } - private static final int selectTextSize(final int keyHeight, final float ratio, + private static int selectTextSize(final int keyHeight, final float ratio, final int defaultSize) { if (ResourceUtils.isValidFraction(ratio)) { return (int)(keyHeight * ratio); @@ -134,10 +149,17 @@ public final class KeyDrawParams { return defaultSize; } - private static final int selectColor(final int attrColor, final int defaultColor) { + private static int selectColor(final int attrColor, final int defaultColor) { if (attrColor != 0) { return attrColor; } return defaultColor; } + + private static float selectFloatIfNonZero(final float attrFloat, final float defaultFloat) { + if (attrFloat != 0) { + return attrFloat; + } + return defaultFloat; + } } diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyVisualAttributes.java b/java/src/com/android/inputmethod/keyboard/internal/KeyVisualAttributes.java index 133462ac7..69cf2e389 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyVisualAttributes.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyVisualAttributes.java @@ -48,6 +48,8 @@ public final class KeyVisualAttributes { public final int mPreviewTextColor; public final float mHintLabelVerticalAdjustment; + public final float mLabelOffCenterRatio; + public final float mHintLabelOffCenterRatio; private static final int[] VISUAL_ATTRIBUTE_IDS = { R.styleable.Keyboard_Key_keyTypeface, @@ -69,6 +71,8 @@ public final class KeyVisualAttributes { R.styleable.Keyboard_Key_keyShiftedLetterHintActivatedColor, R.styleable.Keyboard_Key_keyPreviewTextColor, R.styleable.Keyboard_Key_keyHintLabelVerticalAdjustment, + R.styleable.Keyboard_Key_keyLabelOffCenterRatio, + R.styleable.Keyboard_Key_keyHintLabelOffCenterRatio }; private static final SparseIntArray sVisualAttributeIds = new SparseIntArray(); private static final int ATTR_DEFINED = 1; @@ -135,5 +139,9 @@ public final class KeyVisualAttributes { mHintLabelVerticalAdjustment = ResourceUtils.getFraction(keyAttr, R.styleable.Keyboard_Key_keyHintLabelVerticalAdjustment, 0.0f); + mLabelOffCenterRatio = ResourceUtils.getFraction(keyAttr, + R.styleable.Keyboard_Key_keyLabelOffCenterRatio, 0.0f); + mHintLabelOffCenterRatio = ResourceUtils.getFraction(keyAttr, + R.styleable.Keyboard_Key_keyHintLabelOffCenterRatio, 0.0f); } }