Add surrounding drawing view to AbstractDrawingPreview constructor

Change-Id: If57e59f25fc3e29c0e7f46e8218d04fb233c27c6
main
Tadashi G. Takaoka 2013-01-21 18:36:34 +09:00
parent 820b5e079c
commit 4987108f34
3 changed files with 50 additions and 26 deletions

View File

@ -17,6 +17,7 @@
package com.android.inputmethod.keyboard.internal;
import android.graphics.Canvas;
import android.view.View;
import com.android.inputmethod.keyboard.PointerTracker;
@ -25,9 +26,18 @@ import com.android.inputmethod.keyboard.PointerTracker;
* GestureFloatingPrevewText, GestureTrail, and SlidingKeyInputPreview.
*/
public abstract class AbstractDrawingPreview {
private final View mDrawingView;
private boolean mPreviewEnabled;
public void setPreviewEnabled(final boolean enabled) {
protected AbstractDrawingPreview(final View drawingView) {
mDrawingView = drawingView;
}
public final View getDrawingView() {
return mDrawingView;
}
public final void setPreviewEnabled(final boolean enabled) {
mPreviewEnabled = enabled;
}
@ -35,6 +45,14 @@ public abstract class AbstractDrawingPreview {
return mPreviewEnabled;
}
public void setKeyboardGeometry(final int[] originCoords, final int width, final int height) {
// Default implementation is empty.
}
public void onDetachFromWindow() {
// Default implementation is empty.
}
/**
* Draws the preview
* @param canvas The canvas where the preview is drawn.
@ -43,7 +61,7 @@ public abstract class AbstractDrawingPreview {
/**
* Set the position of the preview.
* @param pt The new location of the preview is based on the points in PointerTracker pt.
* @param tracker The new location of the preview is based on the points in PointerTracker.
*/
public abstract void setPreviewPosition(final PointerTracker pt);
public abstract void setPreviewPosition(final PointerTracker tracker);
}

View File

@ -16,7 +16,6 @@
package com.android.inputmethod.keyboard.internal;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
@ -24,6 +23,7 @@ import android.graphics.Paint.Align;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextUtils;
import android.view.View;
import com.android.inputmethod.keyboard.PointerTracker;
import com.android.inputmethod.latin.CoordinateUtils;
@ -98,16 +98,18 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview {
PREVIEW_TEXT_ARRAY_CAPACITY);
protected SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
protected final Context mContext;
public final int[] mLastPointerCoords = CoordinateUtils.newInstance();
public GestureFloatingPreviewText(final TypedArray typedArray, final Context context) {
public GestureFloatingPreviewText(final View drawingView, final TypedArray typedArray) {
super(drawingView);
mParams = new GesturePreviewTextParams(typedArray);
mHighlightedWordIndex = 0;
mContext = context;
}
public void setSuggetedWords(final SuggestedWords suggestedWords) {
if (!isPreviewEnabled()) {
return;
}
mSuggestedWords = suggestedWords;
updatePreviewPosition();
}
@ -120,8 +122,13 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview {
}
@Override
public void setPreviewPosition(final PointerTracker pt) {
pt.getLastCoordinates(mLastPointerCoords);
public void setPreviewPosition(final PointerTracker tracker) {
final boolean needsToUpdateLastPointer =
tracker.isOldestTrackerInQueue() && isPreviewEnabled();
if (!needsToUpdateLastPointer) {
return;
}
tracker.getLastCoordinates(mLastPointerCoords);
updatePreviewPosition();
}
@ -164,7 +171,7 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview {
final float rectWidth = textWidth + hPad * 2.0f;
final float rectHeight = textHeight + vPad * 2.0f;
final int displayWidth = mContext.getResources().getDisplayMetrics().widthPixels;
final int displayWidth = getDrawingView().getResources().getDisplayMetrics().widthPixels;
final float rectX = Math.min(
Math.max(CoordinateUtils.x(mLastPointerCoords) - rectWidth / 2.0f, 0.0f),
displayWidth - rectWidth);
@ -176,5 +183,7 @@ public class GestureFloatingPreviewText extends AbstractDrawingPreview {
final int textY = (int)(rectY + vPad) + textHeight;
mPreviewTextXArray.add(0, textX);
mPreviewTextYArray.add(0, textY);
// TODO: Should narrow the invalidate region.
getDrawingView().invalidate();
}
}

View File

@ -41,7 +41,7 @@ import com.android.inputmethod.latin.SuggestedWords;
public final class PreviewPlacerView extends RelativeLayout {
private final int[] mKeyboardViewOrigin = CoordinateUtils.newInstance();
// TODO: Consolidate gesture preview trail with {@link KeyboardView}
// TODO: Separate gesture preview trail drawing code into separate class.
private final SparseArray<GesturePreviewTrail> mGesturePreviewTrails =
CollectionUtils.newSparseArray();
private final Params mGesturePreviewTrailParams;
@ -55,6 +55,7 @@ public final class PreviewPlacerView extends RelativeLayout {
private final Rect mOffscreenSrcRect = new Rect();
private final Rect mDirtyRect = new Rect();
private final Rect mGesturePreviewTrailBoundsRect = new Rect(); // per trail
// TODO: Move these AbstractDrawingPvreiew objects to MainKeyboardView.
private final GestureFloatingPreviewText mGestureFloatingPreviewText;
private boolean mShowSlidingKeyInputPreview;
private final int[] mRubberBandFrom = CoordinateUtils.newInstance();
@ -104,7 +105,7 @@ public final class PreviewPlacerView extends RelativeLayout {
attrs, R.styleable.MainKeyboardView, defStyle, R.style.MainKeyboardView);
// TODO: mGestureFloatingPreviewText could be an instance of GestureFloatingPreviewText or
// MultiGesturePreviewText, depending on the user's choice in the settings.
mGestureFloatingPreviewText = new GestureFloatingPreviewText(mainKeyboardViewAttr, context);
mGestureFloatingPreviewText = new GestureFloatingPreviewText(this, mainKeyboardViewAttr);
mGesturePreviewTrailParams = new Params(mainKeyboardViewAttr);
mainKeyboardViewAttr.recycle();
@ -120,11 +121,14 @@ public final class PreviewPlacerView extends RelativeLayout {
setLayerType(LAYER_TYPE_HARDWARE, layerPaint);
}
public void setKeyboardViewGeometry(final int[] originCoords, final int w, final int h) {
public void setKeyboardViewGeometry(final int[] originCoords, final int width,
final int height) {
CoordinateUtils.copy(mKeyboardViewOrigin, originCoords);
mOffscreenOffsetY = (int)(h * GestureStroke.EXTRA_GESTURE_TRAIL_AREA_ABOVE_KEYBOARD_RATIO);
mOffscreenWidth = w;
mOffscreenHeight = mOffscreenOffsetY + h;
mGestureFloatingPreviewText.setKeyboardGeometry(originCoords, width, height);
mOffscreenOffsetY = (int)(
height * GestureStroke.EXTRA_GESTURE_TRAIL_AREA_ABOVE_KEYBOARD_RATIO);
mOffscreenWidth = width;
mOffscreenHeight = mOffscreenOffsetY + height;
}
public void setGesturePreviewMode(final boolean drawsGesturePreviewTrail,
@ -134,11 +138,7 @@ public final class PreviewPlacerView extends RelativeLayout {
}
public void invalidatePointer(final PointerTracker tracker) {
final boolean needsToUpdateLastPointer =
tracker.isOldestTrackerInQueue() && mGestureFloatingPreviewText.isPreviewEnabled();
if (needsToUpdateLastPointer) {
mGestureFloatingPreviewText.setPreviewPosition(tracker);
}
mGestureFloatingPreviewText.setPreviewPosition(tracker);
if (mDrawsGesturePreviewTrail) {
GesturePreviewTrail trail;
@ -150,10 +150,8 @@ public final class PreviewPlacerView extends RelativeLayout {
}
}
trail.addStroke(tracker.getGestureStrokeWithPreviewPoints(), tracker.getDownTime());
}
// TODO: Should narrow the invalidate region.
if (mDrawsGesturePreviewTrail || needsToUpdateLastPointer) {
// TODO: Should narrow the invalidate region.
invalidate();
}
}
@ -175,6 +173,7 @@ public final class PreviewPlacerView extends RelativeLayout {
@Override
protected void onDetachedFromWindow() {
mGestureFloatingPreviewText.onDetachFromWindow();
freeOffscreenBuffer();
}
@ -254,9 +253,7 @@ public final class PreviewPlacerView extends RelativeLayout {
}
public void setGestureFloatingPreviewText(final SuggestedWords suggestedWords) {
if (!mGestureFloatingPreviewText.isPreviewEnabled()) return;
mGestureFloatingPreviewText.setSuggetedWords(suggestedWords);
invalidate();
}
private void drawSlidingKeyInputPreview(final Canvas canvas) {