Clean up GestureFloatingPreviewText

Change-Id: Ie64dd06a99e54b2f432725f69dc4f38c81359ce6
main
Tadashi G. Takaoka 2013-02-01 12:46:37 +09:00
parent 40be4c3bdb
commit 5c58133c80
1 changed files with 32 additions and 46 deletions

View File

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