Merge "Refactor key drawing code in KeyboardView"

main
Tadashi G. Takaoka 2012-03-21 04:31:55 -07:00 committed by Android (Google) Code Review
commit 4897df803d
4 changed files with 31 additions and 33 deletions

View File

@ -99,7 +99,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
// Main keyboard
private Keyboard mKeyboard;
private final KeyDrawParams mKeyDrawParams;
protected final KeyDrawParams mKeyDrawParams;
// Key preview
private final int mKeyPreviewLayoutId;
@ -171,7 +171,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
}
}
/* package */ static class KeyDrawParams {
protected static class KeyDrawParams {
// XML attributes
public final int mKeyTextColor;
public final int mKeyTextInactivatedColor;
@ -351,7 +351,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
mDelayAfterPreview = mKeyPreviewDrawParams.mLingerTimeout;
mPaint.setAntiAlias(true);
mPaint.setTextAlign(Align.CENTER);
}
// Read fraction value in TypedArray as float.
@ -729,10 +728,10 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
}
}
private static int getCharGeometryCacheKey(char reference, Paint paint) {
private static int getCharGeometryCacheKey(char referenceChar, Paint paint) {
final int labelSize = (int)paint.getTextSize();
final Typeface face = paint.getTypeface();
final int codePointOffset = reference << 15;
final int codePointOffset = referenceChar << 15;
if (face == Typeface.DEFAULT) {
return codePointOffset + labelSize;
} else if (face == Typeface.DEFAULT_BOLD) {
@ -747,41 +746,35 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
// Working variable for the following methods.
private final Rect mTextBounds = new Rect();
private float getCharHeight(char[] character, Paint paint) {
final Integer key = getCharGeometryCacheKey(character[0], paint);
private float getCharHeight(char[] referenceChar, Paint paint) {
final Integer key = getCharGeometryCacheKey(referenceChar[0], paint);
final Float cachedValue = sTextHeightCache.get(key);
if (cachedValue != null)
return cachedValue;
paint.getTextBounds(character, 0, 1, mTextBounds);
paint.getTextBounds(referenceChar, 0, 1, mTextBounds);
final float height = mTextBounds.height();
sTextHeightCache.put(key, height);
return height;
}
private float getCharWidth(char[] character, Paint paint) {
final Integer key = getCharGeometryCacheKey(character[0], paint);
private float getCharWidth(char[] referenceChar, Paint paint) {
final Integer key = getCharGeometryCacheKey(referenceChar[0], paint);
final Float cachedValue = sTextWidthCache.get(key);
if (cachedValue != null)
return cachedValue;
paint.getTextBounds(character, 0, 1, mTextBounds);
paint.getTextBounds(referenceChar, 0, 1, mTextBounds);
final float width = mTextBounds.width();
sTextWidthCache.put(key, width);
return width;
}
protected float getLabelWidth(CharSequence label, Paint paint) {
public float getLabelWidth(String label, Paint paint) {
paint.getTextBounds(label.toString(), 0, label.length(), mTextBounds);
return mTextBounds.width();
}
public float getDefaultLabelWidth(String label, Paint paint) {
paint.setTextSize(mKeyDrawParams.mKeyLabelSize);
paint.setTypeface(mKeyDrawParams.mKeyTextStyle);
return getLabelWidth(label, paint);
}
protected static void drawIcon(Canvas canvas, Drawable icon, int x, int y, int width,
int height) {
canvas.translate(x, y);
@ -815,6 +808,14 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
canvas.translate(-x, -y);
}
public Paint newDefaultLabelPaint() {
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTypeface(mKeyDrawParams.mKeyTextStyle);
paint.setTextSize(mKeyDrawParams.mKeyLabelSize);
return paint;
}
public void cancelAllMessages() {
mDrawingHandler.cancelAllMessages();
}

View File

@ -296,17 +296,16 @@ public class MoreKeysKeyboard extends Keyboard {
final int padding = (int)(view.getResources()
.getDimension(R.dimen.more_keys_keyboard_key_horizontal_padding)
+ (parentKey.hasLabelsInMoreKeys() ? minKeyWidth * LABEL_PADDING_RATIO : 0));
Paint paint = null;
final Paint paint = view.newDefaultLabelPaint();
paint.setTextSize(parentKey.hasLabelsInMoreKeys()
? view.mKeyDrawParams.mKeyLabelSize
: view.mKeyDrawParams.mKeyLetterSize);
int maxWidth = minKeyWidth;
for (String moreKeySpec : parentKey.mMoreKeys) {
final String label = KeySpecParser.getLabel(moreKeySpec);
// If the label is single letter, minKeyWidth is enough to hold the label.
if (label != null && StringUtils.codePointCount(label) > 1) {
if (paint == null) {
paint = new Paint();
paint.setAntiAlias(true);
}
final int width = (int)view.getDefaultLabelWidth(label, paint) + padding;
final int width = (int)view.getLabelWidth(label, paint) + padding;
if (maxWidth < width) {
maxWidth = width;
}

View File

@ -23,7 +23,6 @@ import android.graphics.drawable.Drawable;
import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.keyboard.KeyboardView;
import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SuggestedWords;
@ -53,10 +52,8 @@ public class MoreSuggestions extends Keyboard {
public int mDividerWidth;
public int layout(SuggestedWords suggestions, int fromPos, int maxWidth, int minWidth,
int maxRow, KeyboardView view) {
int maxRow, MoreSuggestionsView view) {
clearKeys();
final Paint paint = new Paint();
paint.setAntiAlias(true);
final Resources res = view.getContext().getResources();
mDivider = res.getDrawable(R.drawable.more_suggestions_divider);
// TODO: Drawable itself should have an alpha value.
@ -64,6 +61,7 @@ public class MoreSuggestions extends Keyboard {
mDividerWidth = mDivider.getIntrinsicWidth();
final int padding = (int) res.getDimension(
R.dimen.more_suggestions_key_horizontal_padding);
final Paint paint = view.newDefaultLabelPaint();
int row = 0;
int pos = fromPos, rowStartPos = fromPos;
@ -71,7 +69,7 @@ public class MoreSuggestions extends Keyboard {
while (pos < size) {
final String word = suggestions.getWord(pos).toString();
// TODO: Should take care of text x-scaling.
mWidths[pos] = (int)view.getDefaultLabelWidth(word, paint) + padding;
mWidths[pos] = (int)view.getLabelWidth(word, paint) + padding;
final int numColumn = pos - rowStartPos + 1;
final int columnWidth =
(maxWidth - mDividerWidth * (numColumn - 1)) / numColumn;

View File

@ -43,17 +43,17 @@ import com.android.inputmethod.latin.R;
public class MoreSuggestionsView extends KeyboardView implements MoreKeysPanel {
private final int[] mCoordinates = new int[2];
private final KeyDetector mModalPanelKeyDetector;
final KeyDetector mModalPanelKeyDetector;
private final KeyDetector mSlidingPanelKeyDetector;
private Controller mController;
private KeyboardActionListener mListener;
KeyboardActionListener mListener;
private int mOriginX;
private int mOriginY;
private static final TimerProxy EMPTY_TIMER_PROXY = new TimerProxy.Adapter();
static final TimerProxy EMPTY_TIMER_PROXY = new TimerProxy.Adapter();
private final KeyboardActionListener mSuggestionsPaneListener =
final KeyboardActionListener mSuggestionsPaneListener =
new KeyboardActionListener.Adapter() {
@Override
public void onPressKey(int primaryCode) {