Merge "Support 9-patch drawable for spacebar icon"

This commit is contained in:
Tadashi G. Takaoka 2014-05-16 01:28:04 +00:00 committed by Android (Google) Code Review
commit 812d7d935a
13 changed files with 50 additions and 45 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 199 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

View file

@ -51,6 +51,7 @@
<!-- Background image for the spacebar. This image needs to be a <!-- Background image for the spacebar. This image needs to be a
{@link StateListDrawable}, with the following possible states: normal, pressed. --> {@link StateListDrawable}, with the following possible states: normal, pressed. -->
<attr name="spacebarBackground" format="reference" /> <attr name="spacebarBackground" format="reference" />
<attr name="spacebarIconWidthRatio" format="float" />
<!-- Horizontal padding of left/right aligned key label to the edge of the key. --> <!-- Horizontal padding of left/right aligned key label to the edge of the key. -->
<attr name="keyLabelHorizontalPadding" format="dimension" /> <attr name="keyLabelHorizontalPadding" format="dimension" />
<!-- Right padding of hint letter to the edge of the key.--> <!-- Right padding of hint letter to the edge of the key.-->
@ -284,6 +285,7 @@
<!-- This should be aligned with Key.LABEL_FLAGS__* --> <!-- This should be aligned with Key.LABEL_FLAGS__* -->
<flag name="alignLeft" value="0x01" /> <flag name="alignLeft" value="0x01" />
<flag name="alignRight" value="0x02" /> <flag name="alignRight" value="0x02" />
<flag name="alignButtom" value="0x04" />
<flag name="alignLeftOfCenter" value="0x08" /> <flag name="alignLeftOfCenter" value="0x08" />
<flag name="fontNormal" value="0x10" /> <flag name="fontNormal" value="0x10" />
<flag name="fontMonoSpace" value="0x20" /> <flag name="fontMonoSpace" value="0x20" />

View file

@ -25,7 +25,7 @@
<item name="iconShiftKey">@drawable/sym_keyboard_shift_holo_dark</item> <item name="iconShiftKey">@drawable/sym_keyboard_shift_holo_dark</item>
<item name="iconDeleteKey">@drawable/sym_keyboard_delete_lxx_dark</item> <item name="iconDeleteKey">@drawable/sym_keyboard_delete_lxx_dark</item>
<item name="iconSettingsKey">@drawable/sym_keyboard_settings_lxx_dark</item> <item name="iconSettingsKey">@drawable/sym_keyboard_settings_lxx_dark</item>
<item name="iconSpaceKey">@drawable/sym_keyboard_space_holo_dark</item> <item name="iconSpaceKey">@drawable/sym_keyboard_spacebar_lxx_dark</item>
<item name="iconEnterKey">@drawable/sym_keyboard_return_holo_dark</item> <item name="iconEnterKey">@drawable/sym_keyboard_return_holo_dark</item>
<item name="iconGoKey">@drawable/sym_keyboard_go_lxx_dark</item> <item name="iconGoKey">@drawable/sym_keyboard_go_lxx_dark</item>
<item name="iconSearchKey">@drawable/sym_keyboard_search_lxx_dark</item> <item name="iconSearchKey">@drawable/sym_keyboard_search_lxx_dark</item>

View file

@ -50,6 +50,7 @@
<item name="keyBackground">@drawable/btn_keyboard_key_lxx</item> <item name="keyBackground">@drawable/btn_keyboard_key_lxx</item>
<item name="functionalKeyBackground">@drawable/btn_keyboard_key_functional_lxx</item> <item name="functionalKeyBackground">@drawable/btn_keyboard_key_functional_lxx</item>
<item name="spacebarBackground">@drawable/btn_keyboard_spacebar_lxx</item> <item name="spacebarBackground">@drawable/btn_keyboard_spacebar_lxx</item>
<item name="spacebarIconWidthRatio">0.9</item>
<item name="keyTypeface">bold</item> <item name="keyTypeface">bold</item>
<item name="keyTextColor">@color/key_text_color_holo</item> <item name="keyTextColor">@color/key_text_color_holo</item>
<item name="keyTextInactivatedColor">@color/key_text_inactive_color_lxx</item> <item name="keyTextInactivatedColor">@color/key_text_inactive_color_lxx</item>

View file

@ -79,6 +79,7 @@
<key-style <key-style
latin:styleName="spaceKeyStyle" latin:styleName="spaceKeyStyle"
latin:keySpec="!icon/space_key|!code/key_space" latin:keySpec="!icon/space_key|!code/key_space"
latin:keyLabelFlags="alignButtom"
latin:keyActionFlags="noKeyPreview|enableLongPress" /> latin:keyActionFlags="noKeyPreview|enableLongPress" />
<!-- U+200C: ZERO WIDTH NON-JOINER <!-- U+200C: ZERO WIDTH NON-JOINER
U+200D: ZERO WIDTH JOINER --> U+200D: ZERO WIDTH JOINER -->

View file

@ -88,6 +88,7 @@
<key-style <key-style
latin:styleName="spaceKeyStyle" latin:styleName="spaceKeyStyle"
latin:keySpec="!icon/space_key|!code/key_space" latin:keySpec="!icon/space_key|!code/key_space"
latin:keyLabelFlags="alignButtom"
latin:keyActionFlags="noKeyPreview|enableLongPress" /> latin:keyActionFlags="noKeyPreview|enableLongPress" />
<!-- U+200C: ZERO WIDTH NON-JOINER <!-- U+200C: ZERO WIDTH NON-JOINER
U+200D: ZERO WIDTH JOINER --> U+200D: ZERO WIDTH JOINER -->

View file

@ -120,6 +120,7 @@
<key-style <key-style
latin:styleName="numSpaceKeyStyle" latin:styleName="numSpaceKeyStyle"
latin:keySpec="!icon/space_key_for_number_layout|!code/key_space" latin:keySpec="!icon/space_key_for_number_layout|!code/key_space"
latin:keyLabelFlags="alignButtom"
latin:keyActionFlags="enableLongPress" latin:keyActionFlags="enableLongPress"
latin:parentStyle="numKeyBaseStyle" /> latin:parentStyle="numKeyBaseStyle" />
<!-- Override defaultEnterKeyStyle in key_styles_enter.xml --> <!-- Override defaultEnterKeyStyle in key_styles_enter.xml -->

View file

@ -60,6 +60,7 @@ public class Key implements Comparable<Key> {
private final int mLabelFlags; private final int mLabelFlags;
private static final int LABEL_FLAGS_ALIGN_LEFT = 0x01; private static final int LABEL_FLAGS_ALIGN_LEFT = 0x01;
private static final int LABEL_FLAGS_ALIGN_RIGHT = 0x02; private static final int LABEL_FLAGS_ALIGN_RIGHT = 0x02;
private static final int LABEL_FLAGS_ALIGN_BUTTOM = 0x04;
private static final int LABEL_FLAGS_ALIGN_LEFT_OF_CENTER = 0x08; private static final int LABEL_FLAGS_ALIGN_LEFT_OF_CENTER = 0x08;
private static final int LABEL_FLAGS_FONT_NORMAL = 0x10; private static final int LABEL_FLAGS_FONT_NORMAL = 0x10;
private static final int LABEL_FLAGS_FONT_MONO_SPACE = 0x20; private static final int LABEL_FLAGS_FONT_MONO_SPACE = 0x20;
@ -646,6 +647,10 @@ public class Key implements Comparable<Key> {
return (mLabelFlags & LABEL_FLAGS_ALIGN_RIGHT) != 0; return (mLabelFlags & LABEL_FLAGS_ALIGN_RIGHT) != 0;
} }
public final boolean isAlignButtom() {
return (mLabelFlags & LABEL_FLAGS_ALIGN_BUTTOM) != 0;
}
public final boolean isAlignLeftOfCenter() { public final boolean isAlignLeftOfCenter() {
return (mLabelFlags & LABEL_FLAGS_ALIGN_LEFT_OF_CENTER) != 0; return (mLabelFlags & LABEL_FLAGS_ALIGN_LEFT_OF_CENTER) != 0;
} }

View file

@ -28,6 +28,7 @@ import android.graphics.Rect;
import android.graphics.Region; import android.graphics.Region;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
@ -49,6 +50,7 @@ import java.util.HashSet;
* @attr ref R.styleable#KeyboardView_keyBackground * @attr ref R.styleable#KeyboardView_keyBackground
* @attr ref R.styleable#KeyboardView_functionalKeyBackground * @attr ref R.styleable#KeyboardView_functionalKeyBackground
* @attr ref R.styleable#KeyboardView_spacebarBackground * @attr ref R.styleable#KeyboardView_spacebarBackground
* @attr ref R.styleable#KeyboardView_spacebarIconWidthRatio
* @attr ref R.styleable#KeyboardView_keyLabelHorizontalPadding * @attr ref R.styleable#KeyboardView_keyLabelHorizontalPadding
* @attr ref R.styleable#KeyboardView_keyHintLetterPadding * @attr ref R.styleable#KeyboardView_keyHintLetterPadding
* @attr ref R.styleable#KeyboardView_keyPopupHintLetterPadding * @attr ref R.styleable#KeyboardView_keyPopupHintLetterPadding
@ -85,6 +87,7 @@ public class KeyboardView extends View {
private final Drawable mKeyBackground; private final Drawable mKeyBackground;
private final Drawable mFunctionalKeyBackground; private final Drawable mFunctionalKeyBackground;
private final Drawable mSpacebarBackground; private final Drawable mSpacebarBackground;
private final float mSpacebarIconWidthRatio;
private final Rect mKeyBackgroundPadding = new Rect(); private final Rect mKeyBackgroundPadding = new Rect();
private static final float KET_TEXT_SHADOW_RADIUS_DISABLED = -1.0f; private static final float KET_TEXT_SHADOW_RADIUS_DISABLED = -1.0f;
@ -135,8 +138,9 @@ public class KeyboardView extends View {
: mKeyBackground; : mKeyBackground;
final Drawable spacebarBackground = keyboardViewAttr.getDrawable( final Drawable spacebarBackground = keyboardViewAttr.getDrawable(
R.styleable.KeyboardView_spacebarBackground); R.styleable.KeyboardView_spacebarBackground);
mSpacebarBackground = (spacebarBackground != null) ? spacebarBackground mSpacebarBackground = (spacebarBackground != null) ? spacebarBackground : mKeyBackground;
: mKeyBackground; mSpacebarIconWidthRatio = keyboardViewAttr.getFloat(
R.styleable.KeyboardView_spacebarIconWidthRatio, 1.0f);
mKeyLabelHorizontalPadding = keyboardViewAttr.getDimensionPixelOffset( mKeyLabelHorizontalPadding = keyboardViewAttr.getDimensionPixelOffset(
R.styleable.KeyboardView_keyLabelHorizontalPadding, 0); R.styleable.KeyboardView_keyLabelHorizontalPadding, 0);
mKeyHintLetterPadding = keyboardViewAttr.getDimension( mKeyHintLetterPadding = keyboardViewAttr.getDimension(
@ -513,10 +517,16 @@ public class KeyboardView extends View {
// Draw key icon. // Draw key icon.
if (label == null && icon != null) { if (label == null && icon != null) {
final int iconWidth = Math.min(icon.getIntrinsicWidth(), keyWidth); final int iconWidth;
if (key.getCode() == Constants.CODE_SPACE && icon instanceof NinePatchDrawable) {
iconWidth = (int)(keyWidth * mSpacebarIconWidthRatio);
} else {
iconWidth = Math.min(icon.getIntrinsicWidth(), keyWidth);
}
final int iconHeight = icon.getIntrinsicHeight(); final int iconHeight = icon.getIntrinsicHeight();
final int iconX, alignX; final int iconX, alignX;
final int iconY = (keyHeight - iconHeight) / 2; final int iconY = key.isAlignButtom() ? keyHeight - iconHeight
: (keyHeight - iconHeight) / 2;
if (key.isAlignLeft()) { if (key.isAlignLeft()) {
iconX = mKeyLabelHorizontalPadding; iconX = mKeyLabelHorizontalPadding;
alignX = iconX; alignX = iconX;

View file

@ -27,7 +27,6 @@ import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Paint.Align; import android.graphics.Paint.Align;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
@ -116,7 +115,6 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
/* Space key and its icon and background. */ /* Space key and its icon and background. */
private Key mSpaceKey; private Key mSpaceKey;
private Drawable mSpacebarIcon;
// Stuff to draw language name on spacebar. // Stuff to draw language name on spacebar.
private final int mLanguageOnSpacebarFinalAlpha; private final int mLanguageOnSpacebarFinalAlpha;
private ObjectAnimator mLanguageOnSpacebarFadeoutAnimator; private ObjectAnimator mLanguageOnSpacebarFadeoutAnimator;
@ -389,8 +387,6 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
mMoreKeysKeyboardCache.clear(); mMoreKeysKeyboardCache.clear();
mSpaceKey = keyboard.getKey(Constants.CODE_SPACE); mSpaceKey = keyboard.getKey(Constants.CODE_SPACE);
mSpacebarIcon = (mSpaceKey != null)
? mSpaceKey.getIcon(keyboard.mIconsSet, Constants.Color.ALPHA_OPAQUE) : null;
final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap; final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
mLanguageOnSpacebarTextSize = keyHeight * mLanguageOnSpacebarTextRatio; mLanguageOnSpacebarTextSize = keyHeight * mLanguageOnSpacebarTextRatio;
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
@ -847,18 +843,19 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
if (key.altCodeWhileTyping() && key.isEnabled()) { if (key.altCodeWhileTyping() && key.isEnabled()) {
params.mAnimAlpha = mAltCodeKeyWhileTypingAnimAlpha; params.mAnimAlpha = mAltCodeKeyWhileTypingAnimAlpha;
} }
super.onDrawKeyTopVisuals(key, canvas, paint, params);
final int code = key.getCode(); final int code = key.getCode();
if (code == Constants.CODE_SPACE) { if (code == Constants.CODE_SPACE) {
drawSpacebar(key, canvas, paint); // If input language are explicitly selected.
if (mLanguageOnSpacebarFormatType != LanguageOnSpacebarHelper.FORMAT_TYPE_NONE) {
drawLanguageOnSpacebar(key, canvas, paint);
}
// Whether space key needs to show the "..." popup hint for special purposes // Whether space key needs to show the "..." popup hint for special purposes
if (key.isLongPressEnabled() && mHasMultipleEnabledIMEsOrSubtypes) { if (key.isLongPressEnabled() && mHasMultipleEnabledIMEsOrSubtypes) {
drawKeyPopupHint(key, canvas, paint, params); drawKeyPopupHint(key, canvas, paint, params);
} }
} else if (code == Constants.CODE_LANGUAGE_SWITCH) { } else if (code == Constants.CODE_LANGUAGE_SWITCH) {
super.onDrawKeyTopVisuals(key, canvas, paint, params);
drawKeyPopupHint(key, canvas, paint, params); drawKeyPopupHint(key, canvas, paint, params);
} else {
super.onDrawKeyTopVisuals(key, canvas, paint, params);
} }
} }
@ -898,12 +895,9 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
return ""; return "";
} }
private void drawSpacebar(final Key key, final Canvas canvas, final Paint paint) { private void drawLanguageOnSpacebar(final Key key, final Canvas canvas, final Paint paint) {
final int width = key.getWidth(); final int width = key.getWidth();
final int height = key.getHeight(); final int height = key.getHeight();
// If input language are explicitly selected.
if (mLanguageOnSpacebarFormatType != LanguageOnSpacebarHelper.FORMAT_TYPE_NONE) {
paint.setTextAlign(Align.CENTER); paint.setTextAlign(Align.CENTER);
paint.setTypeface(Typeface.DEFAULT); paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(mLanguageOnSpacebarTextSize); paint.setTextSize(mLanguageOnSpacebarTextSize);
@ -926,16 +920,6 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
paint.setTextScaleX(1.0f); paint.setTextScaleX(1.0f);
} }
// Draw the spacebar icon at the bottom
if (mSpacebarIcon != null) {
final int iconWidth = mSpacebarIcon.getIntrinsicWidth();
final int iconHeight = mSpacebarIcon.getIntrinsicHeight();
int x = (width - iconWidth) / 2;
int y = height - iconHeight;
drawIcon(canvas, mSpacebarIcon, x, y, iconWidth, iconHeight);
}
}
@Override @Override
public void deallocateMemory() { public void deallocateMemory() {
super.deallocateMemory(); super.deallocateMemory();