Merge "Calculate mini keyboard key width from labels"

This commit is contained in:
Tadashi G. Takaoka 2011-01-06 06:18:27 -08:00 committed by Android (Google) Code Review
commit fd8f3a3aa0
5 changed files with 47 additions and 13 deletions

View file

@ -30,6 +30,7 @@
<dimen name="keyboard_bottom_padding">0.0mm</dimen> <dimen name="keyboard_bottom_padding">0.0mm</dimen>
<!-- key_height x 1.0 --> <!-- key_height x 1.0 -->
<dimen name="key_preview_height">13.0mm</dimen> <dimen name="key_preview_height">13.0mm</dimen>
<dimen name="mini_keyboard_key_horizontal_padding">12dip</dimen>
<!-- Amount of allowance for selecting keys in a mini popup keyboard by sliding finger. --> <!-- Amount of allowance for selecting keys in a mini popup keyboard by sliding finger. -->
<!-- popup_key_height x 1.2 --> <!-- popup_key_height x 1.2 -->
<dimen name="mini_keyboard_slide_allowance">15.6mm</dimen> <dimen name="mini_keyboard_slide_allowance">15.6mm</dimen>

View file

@ -30,6 +30,7 @@
<dimen name="keyboard_bottom_padding">0.06in</dimen> <dimen name="keyboard_bottom_padding">0.06in</dimen>
<!-- key_preview_text_size_large x 2 --> <!-- key_preview_text_size_large x 2 -->
<dimen name="key_preview_height">80sp</dimen> <dimen name="key_preview_height">80sp</dimen>
<dimen name="mini_keyboard_key_horizontal_padding">8dip</dimen>
<!-- Amount of allowance for selecting keys in a mini popup keyboard by sliding finger. --> <!-- Amount of allowance for selecting keys in a mini popup keyboard by sliding finger. -->
<!-- popup_key_height x 1.2 --> <!-- popup_key_height x 1.2 -->
<dimen name="mini_keyboard_slide_allowance">0.390in</dimen> <dimen name="mini_keyboard_slide_allowance">0.390in</dimen>

View file

@ -136,11 +136,11 @@ public class Key {
* This constructor is being used only for key in mini popup keyboard. * This constructor is being used only for key in mini popup keyboard.
*/ */
public Key(Resources res, Keyboard keyboard, CharSequence popupCharacter, int x, int y, public Key(Resources res, Keyboard keyboard, CharSequence popupCharacter, int x, int y,
int edgeFlags) { int width, int edgeFlags) {
mKeyboard = keyboard; mKeyboard = keyboard;
mHeight = keyboard.getRowHeight() - keyboard.getVerticalGap(); mHeight = keyboard.getRowHeight() - keyboard.getVerticalGap();
mGap = keyboard.getHorizontalGap(); mGap = keyboard.getHorizontalGap();
mWidth = keyboard.getKeyWidth() - mGap; mWidth = width - mGap;
mEdgeFlags = edgeFlags; mEdgeFlags = edgeFlags;
mHintIcon = null; mHintIcon = null;
mManualTemporaryUpperCaseHintIcon = null; mManualTemporaryUpperCaseHintIcon = null;

View file

@ -692,7 +692,7 @@ public class KeyboardView extends View implements PointerTracker.UIProxy {
// Draw key label // Draw key label
if (label != null) { if (label != null) {
// For characters, use large font. For labels like "Done", use small font. // For characters, use large font. For labels like "Done", use small font.
final int labelSize = getLabelSizeAndSetPaint(label, key, paint); final int labelSize = getLabelSizeAndSetPaint(label, key.mLabelOption, paint);
final int labelCharHeight = getLabelCharHeight(labelSize, paint); final int labelCharHeight = getLabelCharHeight(labelSize, paint);
// Vertical label text alignment. // Vertical label text alignment.
@ -828,13 +828,13 @@ public class KeyboardView extends View implements PointerTracker.UIProxy {
mDirtyRect.setEmpty(); mDirtyRect.setEmpty();
} }
private int getLabelSizeAndSetPaint(CharSequence label, Key key, Paint paint) { public int getLabelSizeAndSetPaint(CharSequence label, int keyLabelOption, Paint paint) {
// For characters, use large font. For labels like "Done", use small font. // For characters, use large font. For labels like "Done", use small font.
final int labelSize; final int labelSize;
final Typeface labelStyle; final Typeface labelStyle;
if (label.length() > 1) { if (label.length() > 1) {
labelSize = mLabelTextSize; labelSize = mLabelTextSize;
if ((key.mLabelOption & KEY_LABEL_OPTION_FONT_NORMAL) != 0) { if ((keyLabelOption & KEY_LABEL_OPTION_FONT_NORMAL) != 0) {
labelStyle = Typeface.DEFAULT; labelStyle = Typeface.DEFAULT;
} else { } else {
labelStyle = Typeface.DEFAULT_BOLD; labelStyle = Typeface.DEFAULT_BOLD;
@ -1126,7 +1126,7 @@ public class KeyboardView extends View implements PointerTracker.UIProxy {
// Remove gesture detector on mini-keyboard // Remove gesture detector on mini-keyboard
miniKeyboard.mGestureDetector = null; miniKeyboard.mGestureDetector = null;
Keyboard keyboard = new MiniKeyboardBuilder(getContext(), popupKeyboardResId, popupKey) Keyboard keyboard = new MiniKeyboardBuilder(this, popupKeyboardResId, popupKey)
.build(); .build();
miniKeyboard.setKeyboard(keyboard); miniKeyboard.setKeyboard(keyboard);
miniKeyboard.setPopupParent(this); miniKeyboard.setPopupParent(this);

View file

@ -16,8 +16,12 @@
package com.android.inputmethod.keyboard; package com.android.inputmethod.keyboard;
import com.android.inputmethod.latin.R;
import android.content.Context; import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Rect;
import java.util.List; import java.util.List;
@ -25,6 +29,8 @@ public class MiniKeyboardBuilder {
private final Resources mRes; private final Resources mRes;
private final Keyboard mKeyboard; private final Keyboard mKeyboard;
private final CharSequence[] mPopupCharacters; private final CharSequence[] mPopupCharacters;
private final int mMiniKeyboardKeyHorizontalPadding;
private final int mKeyWidth;
private final int mMaxColumns; private final int mMaxColumns;
private final int mNumRows; private final int mNumRows;
private int mColPos; private int mColPos;
@ -32,35 +38,61 @@ public class MiniKeyboardBuilder {
private int mX; private int mX;
private int mY; private int mY;
public MiniKeyboardBuilder(Context context, int layoutTemplateResId, Key popupKey) { public MiniKeyboardBuilder(KeyboardView view, int layoutTemplateResId, Key popupKey) {
final Context context = view.getContext();
mRes = context.getResources(); mRes = context.getResources();
final Keyboard keyboard = new Keyboard(context, layoutTemplateResId, null); final Keyboard keyboard = new Keyboard(context, layoutTemplateResId, null);
mKeyboard = keyboard; mKeyboard = keyboard;
mPopupCharacters = popupKey.mPopupCharacters; mPopupCharacters = popupKey.mPopupCharacters;
final int numKeys = mPopupCharacters.length; mMiniKeyboardKeyHorizontalPadding = (int)mRes.getDimension(
R.dimen.mini_keyboard_key_horizontal_padding);
mKeyWidth = getMaxKeyWidth(view, mPopupCharacters, mKeyboard.getKeyWidth());
final int maxColumns = popupKey.mMaxPopupColumn; final int maxColumns = popupKey.mMaxPopupColumn;
mMaxColumns = maxColumns;
final int numKeys = mPopupCharacters.length;
int numRows = numKeys / maxColumns; int numRows = numKeys / maxColumns;
if (numKeys % maxColumns != 0) numRows++; if (numKeys % maxColumns != 0) numRows++;
mMaxColumns = maxColumns;
mNumRows = numRows; mNumRows = numRows;
keyboard.setHeight((keyboard.getRowHeight() + keyboard.getVerticalGap()) * numRows keyboard.setHeight((keyboard.getRowHeight() + keyboard.getVerticalGap()) * numRows
- keyboard.getVerticalGap()); - keyboard.getVerticalGap());
// TODO: To determine key width we should pay attention to key label length.
if (numRows > 1) { if (numRows > 1) {
mColPos = numKeys % maxColumns; mColPos = numKeys % maxColumns;
if (mColPos > 0) mColPos = maxColumns - mColPos; if (mColPos > 0) mColPos = maxColumns - mColPos;
// Centering top-row keys. // Centering top-row keys.
mX = mColPos * (keyboard.getKeyWidth() + keyboard.getHorizontalGap()) / 2; mX = mColPos * (mKeyWidth + keyboard.getHorizontalGap()) / 2;
} }
mKeyboard.setMinWidth(0); mKeyboard.setMinWidth(0);
} }
private int getMaxKeyWidth(KeyboardView view, CharSequence[] popupCharacters, int minKeyWidth) {
Paint paint = null;
Rect bounds = null;
int maxWidth = 0;
for (CharSequence popupSpec : popupCharacters) {
final CharSequence label = PopupCharactersParser.getLabel(popupSpec.toString());
// If the label is single letter, minKeyWidth is enough to hold the label.
if (label.length() > 1) {
if (paint == null) {
paint = new Paint();
paint.setAntiAlias(true);
}
final int labelSize = view.getLabelSizeAndSetPaint(label, 0, paint);
paint.setTextSize(labelSize);
if (bounds == null) bounds = new Rect();
paint.getTextBounds(label.toString(), 0, label.length(), bounds);
if (maxWidth < bounds.width())
maxWidth = bounds.width();
}
}
return Math.max(minKeyWidth, maxWidth + mMiniKeyboardKeyHorizontalPadding);
}
public Keyboard build() { public Keyboard build() {
final Keyboard keyboard = mKeyboard; final Keyboard keyboard = mKeyboard;
final List<Key> keys = keyboard.getKeys(); final List<Key> keys = keyboard.getKeys();
for (CharSequence label : mPopupCharacters) { for (CharSequence label : mPopupCharacters) {
refresh(); refresh();
final Key key = new Key(mRes, keyboard, label, mX, mY, getRowFlags()); final Key key = new Key(mRes, keyboard, label, mX, mY, mKeyWidth, getRowFlags());
keys.add(key); keys.add(key);
advance(); advance();
} }
@ -89,7 +121,7 @@ public class MiniKeyboardBuilder {
private void advance() { private void advance() {
final Keyboard keyboard = mKeyboard; final Keyboard keyboard = mKeyboard;
// TODO: Allocate key position depending the precedence of popup characters. // TODO: Allocate key position depending the precedence of popup characters.
mX += keyboard.getKeyWidth() + keyboard.getHorizontalGap(); mX += mKeyWidth + keyboard.getHorizontalGap();
if (mX > keyboard.getMinWidth()) if (mX > keyboard.getMinWidth())
keyboard.setMinWidth(mX); keyboard.setMinWidth(mX);
mColPos++; mColPos++;