Clean up GestureFloatingPreviewText

Change-Id: Ie64dd06a99e54b2f432725f69dc4f38c81359ce6
This commit is contained in:
Tadashi G. Takaoka 2013-02-01 12:46:37 +09:00
parent 40be4c3bdb
commit 5c58133c80

View file

@ -28,7 +28,6 @@ import android.view.View;
import com.android.inputmethod.keyboard.PointerTracker;
import com.android.inputmethod.latin.CoordinateUtils;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.ResizableIntArray;
import com.android.inputmethod.latin.SuggestedWords;
/**
@ -44,16 +43,17 @@ import com.android.inputmethod.latin.SuggestedWords;
* @attr ref R.styleable#KeyboardView_gestureFloatingPreviewRoundRadius
*/
public class GestureFloatingPreviewText extends AbstractDrawingPreview {
private static final class GesturePreviewTextParams {
public final int mGesturePreviewTextSize;
public final int mGesturePreviewTextColor;
protected static final class GesturePreviewTextParams {
public final int mGesturePreviewTextOffset;
public final int mGesturePreviewTextHeight;
public final int mGesturePreviewColor;
public final float mGesturePreviewHorizontalPadding;
public final float mGesturePreviewVerticalPadding;
public final float mGesturePreviewRoundRadius;
public final Paint mTextPaint;
private final int mGesturePreviewTextSize;
private final int mGesturePreviewTextColor;
private final int mGesturePreviewColor;
private final Paint mPaint = new Paint();
private static final char[] TEXT_HEIGHT_REFERENCE_CHAR = { 'M' };
@ -73,37 +73,36 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview {
mGesturePreviewRoundRadius = mainKeyboardViewAttr.getDimension(
R.styleable.MainKeyboardView_gestureFloatingPreviewRoundRadius, 0.0f);
final Paint textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTextSize(mGesturePreviewTextSize);
mTextPaint = textPaint;
final Paint textPaint = getTextPaint();
final Rect textRect = new Rect();
textPaint.getTextBounds(TEXT_HEIGHT_REFERENCE_CHAR, 0, 1, textRect);
mGesturePreviewTextHeight = textRect.height();
}
public Paint getTextPaint() {
mPaint.setAntiAlias(true);
mPaint.setTextAlign(Align.CENTER);
mPaint.setTextSize(mGesturePreviewTextSize);
mPaint.setColor(mGesturePreviewTextColor);
return mPaint;
}
public Paint getBackgroundPaint() {
mPaint.setColor(mGesturePreviewColor);
return mPaint;
}
}
protected final GesturePreviewTextParams mParams;
protected int mPreviewWordNum;
protected final RectF mGesturePreviewRectangle = new RectF();
protected int mHighlightedWordIndex;
private static final int PREVIEW_TEXT_ARRAY_CAPACITY = 10;
// These variables store the positions of preview words. In multi-preview mode, the gesture
// floating preview at most shows PREVIEW_TEXT_ARRAY_CAPACITY words.
protected final ResizableIntArray mPreviewTextXArray = new ResizableIntArray(
PREVIEW_TEXT_ARRAY_CAPACITY);
protected final ResizableIntArray mPreviewTextYArray = new ResizableIntArray(
PREVIEW_TEXT_ARRAY_CAPACITY);
protected SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
public final int[] mLastPointerCoords = CoordinateUtils.newInstance();
private final GesturePreviewTextParams mParams;
private final RectF mGesturePreviewRectangle = new RectF();
private int mPreviewTextX;
private int mPreviewTextY;
private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
private final int[] mLastPointerCoords = CoordinateUtils.newInstance();
public GestureFloatingPreviewText(final View drawingView, final TypedArray typedArray) {
super(drawingView);
mParams = new GesturePreviewTextParams(typedArray);
mHighlightedWordIndex = 0;
}
public void setSuggetedWords(final SuggestedWords suggestedWords) {
@ -114,13 +113,6 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview {
updatePreviewPosition();
}
protected void drawText(final Canvas canvas, final String text, final float textX,
final float textY, final int color) {
final Paint paint = mParams.mTextPaint;
paint.setColor(color);
canvas.drawText(text, textX, textY, paint);
}
@Override
public void setPreviewPosition(final PointerTracker tracker) {
final boolean needsToUpdateLastPointer =
@ -142,14 +134,11 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview {
|| TextUtils.isEmpty(mSuggestedWords.getWord(0))) {
return;
}
final Paint paint = mParams.mTextPaint;
paint.setColor(mParams.mGesturePreviewColor);
final float round = mParams.mGesturePreviewRoundRadius;
canvas.drawRoundRect(mGesturePreviewRectangle, round, round, paint);
canvas.drawRoundRect(
mGesturePreviewRectangle, round, round, mParams.getBackgroundPaint());
final String text = mSuggestedWords.getWord(0);
final int textX = mPreviewTextXArray.get(0);
final int textY = mPreviewTextYArray.get(0);
drawText(canvas, text, textX, textY, mParams.mGesturePreviewTextColor);
canvas.drawText(text, mPreviewTextX, mPreviewTextY, mParams.getTextPaint());
}
/**
@ -162,11 +151,10 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview {
}
final String text = mSuggestedWords.getWord(0);
final Paint paint = mParams.mTextPaint;
final RectF rectangle = mGesturePreviewRectangle;
final int textHeight = mParams.mGesturePreviewTextHeight;
final float textWidth = paint.measureText(text);
final float textWidth = mParams.getTextPaint().measureText(text);
final float hPad = mParams.mGesturePreviewHorizontalPadding;
final float vPad = mParams.mGesturePreviewVerticalPadding;
final float rectWidth = textWidth + hPad * 2.0f;
@ -180,10 +168,8 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview {
- mParams.mGesturePreviewTextOffset - rectHeight;
rectangle.set(rectX, rectY, rectX + rectWidth, rectY + rectHeight);
final int textX = (int)(rectX + hPad + textWidth / 2.0f);
final int textY = (int)(rectY + vPad) + textHeight;
mPreviewTextXArray.add(0, textX);
mPreviewTextYArray.add(0, textY);
mPreviewTextX = (int)(rectX + hPad + textWidth / 2.0f);
mPreviewTextY = (int)(rectY + vPad) + textHeight;
// TODO: Should narrow the invalidate region.
getDrawingView().invalidate();
}