Fix keyboard row height calculation

The keyboard height will be distrubuted as:
  top_padding + (key_height + vertical_gap) * row_count - vertical_gap + bottom_padding

Change-Id: I841f356b9dbf8cfaf3756178bc9e4e6b2aa61364
This commit is contained in:
Tadashi G. Takaoka 2011-09-22 19:29:58 +09:00
parent 8d6fd877c5
commit 8fbfac4ffb
7 changed files with 78 additions and 56 deletions

View file

@ -219,16 +219,17 @@ public class Key {
final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser), final TypedArray keyboardAttr = res.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard); R.styleable.Keyboard);
mHeight = (int)KeyboardBuilder.getDimensionOrFraction(keyboardAttr, mHeight = (int)KeyboardBuilder.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_rowHeight, params.mHeight, row.mRowHeight) R.styleable.Keyboard_rowHeight, params.mBaseHeight, row.mRowHeight)
- params.mVerticalGap; - params.mVerticalGap;
final float horizontalGap = isSpacer() ? 0 final float horizontalGap = isSpacer() ? 0
: KeyboardBuilder.getDimensionOrFraction(keyboardAttr, : KeyboardBuilder.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_horizontalGap, params.mWidth, params.mHorizontalGap); R.styleable.Keyboard_horizontalGap, params.mBaseWidth,
params.mHorizontalGap);
mVerticalGap = params.mVerticalGap; mVerticalGap = params.mVerticalGap;
final int widthType = KeyboardBuilder.getEnumValue(keyboardAttr, final int widthType = KeyboardBuilder.getEnumValue(keyboardAttr,
R.styleable.Keyboard_keyWidth, KEYWIDTH_NOT_ENUM); R.styleable.Keyboard_keyWidth, KEYWIDTH_NOT_ENUM);
float keyWidth = KeyboardBuilder.getDimensionOrFraction(keyboardAttr, float keyWidth = KeyboardBuilder.getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_keyWidth, params.mWidth, row.mDefaultKeyWidth); R.styleable.Keyboard_keyWidth, params.mBaseWidth, row.mDefaultKeyWidth);
keyboardAttr.recycle(); keyboardAttr.recycle();
final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser), final TypedArray keyAttr = res.obtainAttributes(Xml.asAttributeSet(parser),

View file

@ -90,15 +90,12 @@ public class Keyboard {
/** Total width of the keyboard, including the padding and keys */ /** Total width of the keyboard, including the padding and keys */
public final int mOccupiedWidth; public final int mOccupiedWidth;
public final int mHeight; /** The padding above the keyboard */
public final int mWidth; public final int mTopPadding;
/** Default row height */
public final int mDefaultRowHeight;
/** Default gap between rows */ /** Default gap between rows */
public final int mVerticalGap; public final int mVerticalGap;
public final int mMostCommonKeyHeight;
public final int mMostCommonKeyWidth; public final int mMostCommonKeyWidth;
/** More keys keyboard template */ /** More keys keyboard template */
@ -126,14 +123,13 @@ public class Keyboard {
mId = params.mId; mId = params.mId;
mOccupiedHeight = params.mOccupiedHeight; mOccupiedHeight = params.mOccupiedHeight;
mOccupiedWidth = params.mOccupiedWidth; mOccupiedWidth = params.mOccupiedWidth;
mHeight = params.mHeight; mMostCommonKeyHeight = params.mMostCommonKeyHeight;
mWidth = params.mWidth;
mMostCommonKeyWidth = params.mMostCommonKeyWidth; mMostCommonKeyWidth = params.mMostCommonKeyWidth;
mIsRtlKeyboard = params.mIsRtlKeyboard; mIsRtlKeyboard = params.mIsRtlKeyboard;
mMoreKeysTemplate = params.mMoreKeysTemplate; mMoreKeysTemplate = params.mMoreKeysTemplate;
mMaxMiniKeyboardColumn = params.mMaxMiniKeyboardColumn; mMaxMiniKeyboardColumn = params.mMaxMiniKeyboardColumn;
mDefaultRowHeight = params.mDefaultRowHeight; mTopPadding = params.mTopPadding;
mVerticalGap = params.mVerticalGap; mVerticalGap = params.mVerticalGap;
mKeys = Collections.unmodifiableList(params.mKeys); mKeys = Collections.unmodifiableList(params.mKeys);

View file

@ -391,7 +391,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
mDirtyRect.set(0, 0, getWidth(), getHeight()); mDirtyRect.set(0, 0, getWidth(), getHeight());
mBufferNeedsUpdate = true; mBufferNeedsUpdate = true;
invalidateAllKeys(); invalidateAllKeys();
final int keyHeight = keyboard.mDefaultRowHeight - keyboard.mVerticalGap; final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
mKeyDrawParams.updateKeyHeight(keyHeight); mKeyDrawParams.updateKeyHeight(keyHeight);
mKeyPreviewDrawParams.updateKeyHeight(keyHeight); mKeyPreviewDrawParams.updateKeyHeight(keyHeight);
} }

View file

@ -122,9 +122,9 @@ public class MiniKeyboard extends Keyboard {
mTopRowAdjustment = -1; mTopRowAdjustment = -1;
} }
mWidth = mOccupiedWidth = mNumColumns * mDefaultKeyWidth; mBaseWidth = mOccupiedWidth = mNumColumns * mDefaultKeyWidth;
// Need to subtract the bottom row's gutter only. // Need to subtract the bottom row's gutter only.
mHeight = mOccupiedHeight = mNumRows * mDefaultRowHeight - mVerticalGap mBaseHeight = mOccupiedHeight = mNumRows * mDefaultRowHeight - mVerticalGap
+ mTopPadding + mBottomPadding; + mTopPadding + mBottomPadding;
} }
@ -219,7 +219,7 @@ public class MiniKeyboard extends Keyboard {
final int keyWidth = getMaxKeyWidth(view, mMoreKeys, mParams.mDefaultKeyWidth); final int keyWidth = getMaxKeyWidth(view, mMoreKeys, mParams.mDefaultKeyWidth);
mParams.setParameters(mMoreKeys.length, parentKey.mMaxMoreKeysColumn, mParams.setParameters(mMoreKeys.length, parentKey.mMaxMoreKeysColumn,
keyWidth, parentKeyboard.mDefaultRowHeight, parentKey.mX keyWidth, parentKeyboard.mMostCommonKeyHeight, parentKey.mX
+ (mParams.mDefaultKeyWidth - keyWidth) / 2, view.getMeasuredWidth()); + (mParams.mDefaultKeyWidth - keyWidth) / 2, view.getMeasuredWidth());
} }

View file

@ -123,6 +123,9 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
private static final String TAG_DEFAULT = "default"; private static final String TAG_DEFAULT = "default";
public static final String TAG_KEY_STYLE = "key-style"; public static final String TAG_KEY_STYLE = "key-style";
private static final int DEFAULT_KEYBOARD_COLUMNS = 10;
private static final int DEFAULT_KEYBOARD_ROWS = 4;
protected final KP mParams; protected final KP mParams;
protected final Context mContext; protected final Context mContext;
protected final Resources mResources; protected final Resources mResources;
@ -151,14 +154,12 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
public float mCurrentX; public float mCurrentX;
public Row(Resources res, KeyboardParams params, XmlResourceParser parser, int y) { public Row(Resources res, KeyboardParams params, XmlResourceParser parser, int y) {
final int keyboardWidth = params.mWidth;
final int keyboardHeight = params.mHeight;
TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser),
R.styleable.Keyboard); R.styleable.Keyboard);
mDefaultKeyWidth = KeyboardBuilder.getDimensionOrFraction(a, mDefaultKeyWidth = KeyboardBuilder.getDimensionOrFraction(a,
R.styleable.Keyboard_keyWidth, keyboardWidth, params.mDefaultKeyWidth); R.styleable.Keyboard_keyWidth, params.mBaseWidth, params.mDefaultKeyWidth);
mRowHeight = (int)KeyboardBuilder.getDimensionOrFraction(a, mRowHeight = (int)KeyboardBuilder.getDimensionOrFraction(a,
R.styleable.Keyboard_rowHeight, keyboardHeight, params.mDefaultRowHeight); R.styleable.Keyboard_rowHeight, params.mBaseHeight, params.mDefaultRowHeight);
a.recycle(); a.recycle();
mCurrentY = y; mCurrentY = y;
@ -259,38 +260,40 @@ public class KeyboardBuilder<KP extends KeyboardParams> {
minKeyboardHeight = -(int)getDimensionOrFraction(keyboardAttr, minKeyboardHeight = -(int)getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_minKeyboardHeight, displayWidth, displayWidth / 2); R.styleable.Keyboard_minKeyboardHeight, displayWidth, displayWidth / 2);
} }
final KeyboardParams params = mParams;
// Keyboard height will not exceed maxKeyboardHeight and will not be less than // Keyboard height will not exceed maxKeyboardHeight and will not be less than
// minKeyboardHeight. // minKeyboardHeight.
mParams.mOccupiedHeight = Math.max( params.mOccupiedHeight = Math.max(
Math.min(keyboardHeight, maxKeyboardHeight), minKeyboardHeight); Math.min(keyboardHeight, maxKeyboardHeight), minKeyboardHeight);
mParams.mOccupiedWidth = mParams.mId.mWidth; params.mOccupiedWidth = params.mId.mWidth;
mParams.mTopPadding = (int)getDimensionOrFraction(keyboardAttr, params.mTopPadding = (int)getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_keyboardTopPadding, mParams.mOccupiedHeight, 0); R.styleable.Keyboard_keyboardTopPadding, params.mOccupiedHeight, 0);
mParams.mBottomPadding = (int)getDimensionOrFraction(keyboardAttr, params.mBottomPadding = (int)getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_keyboardBottomPadding, mParams.mOccupiedHeight, 0); R.styleable.Keyboard_keyboardBottomPadding, params.mOccupiedHeight, 0);
final int height = mParams.mOccupiedHeight; params.mBaseWidth = params.mOccupiedWidth - params.mHorizontalEdgesPadding * 2
final int width = mParams.mOccupiedWidth - mParams.mHorizontalEdgesPadding * 2 - params.mHorizontalCenterPadding;
- mParams.mHorizontalCenterPadding; params.mDefaultKeyWidth = (int)getDimensionOrFraction(keyboardAttr,
mParams.mHeight = height; R.styleable.Keyboard_keyWidth, params.mBaseWidth,
mParams.mWidth = width; params.mBaseWidth / DEFAULT_KEYBOARD_COLUMNS);
mParams.mDefaultKeyWidth = (int)getDimensionOrFraction(keyboardAttr, params.mHorizontalGap = (int)getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_keyWidth, width, width / 10); R.styleable.Keyboard_horizontalGap, params.mBaseWidth, 0);
mParams.mDefaultRowHeight = (int)getDimensionOrFraction(keyboardAttr, params.mVerticalGap = (int)getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_rowHeight, height, height / 4); R.styleable.Keyboard_verticalGap, params.mOccupiedHeight, 0);
mParams.mHorizontalGap = (int)getDimensionOrFraction(keyboardAttr, params.mBaseHeight = params.mOccupiedHeight - params.mTopPadding
R.styleable.Keyboard_horizontalGap, width, 0); - params.mBottomPadding + params.mVerticalGap;
mParams.mVerticalGap = (int)getDimensionOrFraction(keyboardAttr, params.mDefaultRowHeight = (int)getDimensionOrFraction(keyboardAttr,
R.styleable.Keyboard_verticalGap, height, 0); R.styleable.Keyboard_rowHeight, params.mBaseHeight,
params.mBaseHeight / DEFAULT_KEYBOARD_ROWS);
mParams.mIsRtlKeyboard = keyboardAttr.getBoolean( params.mIsRtlKeyboard = keyboardAttr.getBoolean(
R.styleable.Keyboard_isRtlKeyboard, false); R.styleable.Keyboard_isRtlKeyboard, false);
mParams.mMoreKeysTemplate = keyboardAttr.getResourceId( params.mMoreKeysTemplate = keyboardAttr.getResourceId(
R.styleable.Keyboard_moreKeysTemplate, 0); R.styleable.Keyboard_moreKeysTemplate, 0);
mParams.mMaxMiniKeyboardColumn = keyAttr.getInt( params.mMaxMiniKeyboardColumn = keyAttr.getInt(
R.styleable.Keyboard_Key_maxMoreKeysColumn, 5); R.styleable.Keyboard_Key_maxMoreKeysColumn, 5);
mParams.mIconsSet.loadIcons(keyboardAttr); params.mIconsSet.loadIcons(keyboardAttr);
} finally { } finally {
keyAttr.recycle(); keyAttr.recycle();
keyboardAttr.recycle(); keyboardAttr.recycle();

View file

@ -32,11 +32,13 @@ import java.util.Set;
public class KeyboardParams { public class KeyboardParams {
public KeyboardId mId; public KeyboardId mId;
/** Total height and width of the keyboard, including the paddings and keys */
public int mOccupiedHeight; public int mOccupiedHeight;
public int mOccupiedWidth; public int mOccupiedWidth;
public int mHeight; /** Base height and width of the keyboard used to calculate rows' or keys' heights and widths */
public int mWidth; public int mBaseHeight;
public int mBaseWidth;
public int mTopPadding; public int mTopPadding;
public int mBottomPadding; public int mBottomPadding;
@ -62,6 +64,7 @@ public class KeyboardParams {
public final Map<Key, Drawable> mUnshiftedIcons = new HashMap<Key, Drawable>(); public final Map<Key, Drawable> mUnshiftedIcons = new HashMap<Key, Drawable>();
public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet(); public final KeyboardIconsSet mIconsSet = new KeyboardIconsSet();
public int mMostCommonKeyHeight = 0;
public int mMostCommonKeyWidth = 0; public int mMostCommonKeyWidth = 0;
protected void clearKeys() { protected void clearKeys() {
@ -89,21 +92,39 @@ public class KeyboardParams {
mShiftedIcons.put(key, icon); mShiftedIcons.put(key, icon);
} }
private int mMaxCount = 0; private int mMaxHeightCount = 0;
private final Map<Integer, Integer> mHistogram = new HashMap<Integer, Integer>(); private int mMaxWidthCount = 0;
private final Map<Integer, Integer> mHeightHistogram = new HashMap<Integer, Integer>();
private final Map<Integer, Integer> mWidthHistogram = new HashMap<Integer, Integer>();
private void clearHistogram() { private void clearHistogram() {
mMostCommonKeyHeight = 0;
mMaxHeightCount = 0;
mHeightHistogram.clear();
mMaxWidthCount = 0;
mMostCommonKeyWidth = 0; mMostCommonKeyWidth = 0;
mMaxCount = 0; mWidthHistogram.clear();
mHistogram.clear(); }
private static int updateHistogramCounter(Map<Integer, Integer> histogram, Integer key) {
final int count = (histogram.containsKey(key) ? histogram.get(key) : 0) + 1;
histogram.put(key, count);
return count;
} }
private void updateHistogram(Key key) { private void updateHistogram(Key key) {
final Integer height = key.mHeight + key.mVerticalGap;
final int heightCount = updateHistogramCounter(mHeightHistogram, height);
if (heightCount > mMaxHeightCount) {
mMaxHeightCount = heightCount;
mMostCommonKeyHeight = height;
}
final Integer width = key.mWidth + key.mHorizontalGap; final Integer width = key.mWidth + key.mHorizontalGap;
final int count = (mHistogram.containsKey(width) ? mHistogram.get(width) : 0) + 1; final int widthCount = updateHistogramCounter(mWidthHistogram, width);
mHistogram.put(width, count); if (widthCount > mMaxWidthCount) {
if (count > mMaxCount) { mMaxWidthCount = widthCount;
mMaxCount = count;
mMostCommonKeyWidth = width; mMostCommonKeyWidth = width;
} }
} }

View file

@ -92,8 +92,9 @@ public class MoreSuggestions extends Keyboard {
} }
mNumColumnsInRow[row] = pos - rowStartPos; mNumColumnsInRow[row] = pos - rowStartPos;
mNumRows = row + 1; mNumRows = row + 1;
mWidth = mOccupiedWidth = Math.max(minWidth, calcurateMaxRowWidth(fromPos, pos)); mBaseWidth = mOccupiedWidth = Math.max(
mHeight = mOccupiedHeight = mNumRows * mDefaultRowHeight + mVerticalGap; minWidth, calcurateMaxRowWidth(fromPos, pos));
mBaseHeight = mOccupiedHeight = mNumRows * mDefaultRowHeight + mVerticalGap;
return pos - fromPos; return pos - fromPos;
} }
@ -149,7 +150,7 @@ public class MoreSuggestions extends Keyboard {
public int getWidth(int pos) { public int getWidth(int pos) {
final int numColumnInRow = getNumColumnInRow(pos); final int numColumnInRow = getNumColumnInRow(pos);
return (mWidth - mDividerWidth * (numColumnInRow - 1)) / numColumnInRow; return (mOccupiedWidth - mDividerWidth * (numColumnInRow - 1)) / numColumnInRow;
} }
public int getFlags(int pos) { public int getFlags(int pos) {