Merge "Refactor drawing preview classes"

This commit is contained in:
Tadashi G. Takaoka 2014-06-21 01:38:11 +00:00 committed by Android (Google) Code Review
commit e9d12c9f74
6 changed files with 44 additions and 70 deletions

View file

@ -236,16 +236,14 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
R.styleable.MainKeyboardView_gestureFloatingPreviewTextLingerTimeout, 0); R.styleable.MainKeyboardView_gestureFloatingPreviewTextLingerTimeout, 0);
mGestureFloatingTextDrawingPreview = new GestureFloatingTextDrawingPreview( mGestureFloatingTextDrawingPreview = new GestureFloatingTextDrawingPreview(
mDrawingPreviewPlacerView, mainKeyboardViewAttr); mainKeyboardViewAttr);
mDrawingPreviewPlacerView.addPreview(mGestureFloatingTextDrawingPreview); mGestureFloatingTextDrawingPreview.setDrawingView(mDrawingPreviewPlacerView);
mGestureTrailsDrawingPreview = new GestureTrailsDrawingPreview( mGestureTrailsDrawingPreview = new GestureTrailsDrawingPreview(mainKeyboardViewAttr);
mDrawingPreviewPlacerView, mainKeyboardViewAttr); mGestureTrailsDrawingPreview.setDrawingView(mDrawingPreviewPlacerView);
mDrawingPreviewPlacerView.addPreview(mGestureTrailsDrawingPreview);
mSlidingKeyInputDrawingPreview = new SlidingKeyInputDrawingPreview( mSlidingKeyInputDrawingPreview = new SlidingKeyInputDrawingPreview(mainKeyboardViewAttr);
mDrawingPreviewPlacerView, mainKeyboardViewAttr); mSlidingKeyInputDrawingPreview.setDrawingView(mDrawingPreviewPlacerView);
mDrawingPreviewPlacerView.addPreview(mSlidingKeyInputDrawingPreview);
mainKeyboardViewAttr.recycle(); mainKeyboardViewAttr.recycle();
mMoreKeysKeyboardContainer = LayoutInflater.from(getContext()) mMoreKeysKeyboardContainer = LayoutInflater.from(getContext())

View file

@ -27,16 +27,19 @@ import com.android.inputmethod.keyboard.PointerTracker;
* SlidingKeyInputDrawingPreview. * SlidingKeyInputDrawingPreview.
*/ */
public abstract class AbstractDrawingPreview { public abstract class AbstractDrawingPreview {
private final View mDrawingView; private View mDrawingView;
private boolean mPreviewEnabled; private boolean mPreviewEnabled;
private boolean mHasValidGeometry; private boolean mHasValidGeometry;
protected AbstractDrawingPreview(final View drawingView) { public void setDrawingView(final DrawingPreviewPlacerView drawingView) {
mDrawingView = drawingView; mDrawingView = drawingView;
drawingView.addPreview(this);
} }
protected final View getDrawingView() { protected void invalidateDrawingView() {
return mDrawingView; if (mDrawingView != null) {
mDrawingView.invalidate();
}
} }
protected final boolean isPreviewEnabled() { protected final boolean isPreviewEnabled() {

View file

@ -46,7 +46,9 @@ public final class DrawingPreviewPlacerView extends RelativeLayout {
} }
public void addPreview(final AbstractDrawingPreview preview) { public void addPreview(final AbstractDrawingPreview preview) {
mPreviews.add(preview); if (mPreviews.indexOf(preview) < 0) {
mPreviews.add(preview);
}
} }
public void setKeyboardViewGeometry(final int[] originCoords, final int width, public void setKeyboardViewGeometry(final int[] originCoords, final int width,

View file

@ -23,7 +23,6 @@ import android.graphics.Paint.Align;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View;
import com.android.inputmethod.keyboard.PointerTracker; import com.android.inputmethod.keyboard.PointerTracker;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
@ -49,6 +48,7 @@ public class GestureFloatingTextDrawingPreview extends AbstractDrawingPreview {
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 int mDisplayWidth;
private final int mGesturePreviewTextSize; private final int mGesturePreviewTextSize;
private final int mGesturePreviewTextColor; private final int mGesturePreviewTextColor;
@ -72,6 +72,7 @@ public class GestureFloatingTextDrawingPreview extends AbstractDrawingPreview {
R.styleable.MainKeyboardView_gestureFloatingPreviewVerticalPadding, 0.0f); R.styleable.MainKeyboardView_gestureFloatingPreviewVerticalPadding, 0.0f);
mGesturePreviewRoundRadius = mainKeyboardViewAttr.getDimension( mGesturePreviewRoundRadius = mainKeyboardViewAttr.getDimension(
R.styleable.MainKeyboardView_gestureFloatingPreviewRoundRadius, 0.0f); R.styleable.MainKeyboardView_gestureFloatingPreviewRoundRadius, 0.0f);
mDisplayWidth = mainKeyboardViewAttr.getResources().getDisplayMetrics().widthPixels;
final Paint textPaint = getTextPaint(); final Paint textPaint = getTextPaint();
final Rect textRect = new Rect(); final Rect textRect = new Rect();
@ -100,9 +101,8 @@ public class GestureFloatingTextDrawingPreview extends AbstractDrawingPreview {
private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY;
private final int[] mLastPointerCoords = CoordinateUtils.newInstance(); private final int[] mLastPointerCoords = CoordinateUtils.newInstance();
public GestureFloatingTextDrawingPreview(final View drawingView, final TypedArray typedArray) { public GestureFloatingTextDrawingPreview(final TypedArray mainKeyboardViewAttr) {
super(drawingView); mParams = new GesturePreviewTextParams(mainKeyboardViewAttr);
mParams = new GesturePreviewTextParams(typedArray);
} }
@Override @Override
@ -149,7 +149,7 @@ public class GestureFloatingTextDrawingPreview extends AbstractDrawingPreview {
*/ */
protected void updatePreviewPosition() { protected void updatePreviewPosition() {
if (mSuggestedWords.isEmpty() || TextUtils.isEmpty(mSuggestedWords.getWord(0))) { if (mSuggestedWords.isEmpty() || TextUtils.isEmpty(mSuggestedWords.getWord(0))) {
getDrawingView().invalidate(); invalidateDrawingView();
return; return;
} }
final String text = mSuggestedWords.getWord(0); final String text = mSuggestedWords.getWord(0);
@ -163,10 +163,9 @@ public class GestureFloatingTextDrawingPreview extends AbstractDrawingPreview {
final float rectWidth = textWidth + hPad * 2.0f; final float rectWidth = textWidth + hPad * 2.0f;
final float rectHeight = textHeight + vPad * 2.0f; final float rectHeight = textHeight + vPad * 2.0f;
final int displayWidth = getDrawingView().getResources().getDisplayMetrics().widthPixels;
final float rectX = Math.min( final float rectX = Math.min(
Math.max(CoordinateUtils.x(mLastPointerCoords) - rectWidth / 2.0f, 0.0f), Math.max(CoordinateUtils.x(mLastPointerCoords) - rectWidth / 2.0f, 0.0f),
displayWidth - rectWidth); mParams.mDisplayWidth - rectWidth);
final float rectY = CoordinateUtils.y(mLastPointerCoords) final float rectY = CoordinateUtils.y(mLastPointerCoords)
- mParams.mGesturePreviewTextOffset - rectHeight; - mParams.mGesturePreviewTextOffset - rectHeight;
rectangle.set(rectX, rectY, rectX + rectWidth, rectY + rectHeight); rectangle.set(rectX, rectY, rectX + rectWidth, rectY + rectHeight);
@ -174,6 +173,6 @@ public class GestureFloatingTextDrawingPreview extends AbstractDrawingPreview {
mPreviewTextX = (int)(rectX + hPad + textWidth / 2.0f); mPreviewTextX = (int)(rectX + hPad + textWidth / 2.0f);
mPreviewTextY = (int)(rectY + vPad) + textHeight; mPreviewTextY = (int)(rectY + vPad) + textHeight;
// TODO: Should narrow the invalidate region. // TODO: Should narrow the invalidate region.
getDrawingView().invalidate(); invalidateDrawingView();
} }
} }

View file

@ -24,17 +24,15 @@ import android.graphics.Paint;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode; import android.graphics.PorterDuffXfermode;
import android.graphics.Rect; import android.graphics.Rect;
import android.os.Message; import android.os.Handler;
import android.util.SparseArray; import android.util.SparseArray;
import android.view.View;
import com.android.inputmethod.keyboard.PointerTracker; import com.android.inputmethod.keyboard.PointerTracker;
import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper;
/** /**
* Draw preview graphics of multiple gesture trails during gesture input. * Draw preview graphics of multiple gesture trails during gesture input.
*/ */
public final class GestureTrailsDrawingPreview extends AbstractDrawingPreview { public final class GestureTrailsDrawingPreview extends AbstractDrawingPreview implements Runnable {
private final SparseArray<GestureTrailDrawingPoints> mGestureTrails = new SparseArray<>(); private final SparseArray<GestureTrailDrawingPoints> mGestureTrails = new SparseArray<>();
private final GestureTrailDrawingParams mDrawingParams; private final GestureTrailDrawingParams mDrawingParams;
private final Paint mGesturePaint; private final Paint mGesturePaint;
@ -47,45 +45,10 @@ public final class GestureTrailsDrawingPreview extends AbstractDrawingPreview {
private final Rect mDirtyRect = new Rect(); private final Rect mDirtyRect = new Rect();
private final Rect mGestureTrailBoundsRect = new Rect(); // per trail private final Rect mGestureTrailBoundsRect = new Rect(); // per trail
private final DrawingHandler mDrawingHandler; private final Handler mDrawingHandler = new Handler();
private static final class DrawingHandler public GestureTrailsDrawingPreview(final TypedArray mainKeyboardViewAttr) {
extends LeakGuardHandlerWrapper<GestureTrailsDrawingPreview> {
private static final int MSG_UPDATE_GESTURE_TRAIL = 0;
private final GestureTrailDrawingParams mDrawingParams;
public DrawingHandler(final GestureTrailsDrawingPreview ownerInstance,
final GestureTrailDrawingParams drawingParams) {
super(ownerInstance);
mDrawingParams = drawingParams;
}
@Override
public void handleMessage(final Message msg) {
final GestureTrailsDrawingPreview preview = getOwnerInstance();
if (preview == null) {
return;
}
switch (msg.what) {
case MSG_UPDATE_GESTURE_TRAIL:
preview.getDrawingView().invalidate();
break;
}
}
public void postUpdateGestureTrailPreview() {
removeMessages(MSG_UPDATE_GESTURE_TRAIL);
sendMessageDelayed(obtainMessage(MSG_UPDATE_GESTURE_TRAIL),
mDrawingParams.mUpdateInterval);
}
}
public GestureTrailsDrawingPreview(final View drawingView,
final TypedArray mainKeyboardViewAttr) {
super(drawingView);
mDrawingParams = new GestureTrailDrawingParams(mainKeyboardViewAttr); mDrawingParams = new GestureTrailDrawingParams(mainKeyboardViewAttr);
mDrawingHandler = new DrawingHandler(this, mDrawingParams);
final Paint gesturePaint = new Paint(); final Paint gesturePaint = new Paint();
gesturePaint.setAntiAlias(true); gesturePaint.setAntiAlias(true);
gesturePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); gesturePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
@ -153,6 +116,12 @@ public final class GestureTrailsDrawingPreview extends AbstractDrawingPreview {
return needsUpdatingGestureTrail; return needsUpdatingGestureTrail;
} }
@Override
public void run() {
// Update preview.
invalidateDrawingView();
}
/** /**
* Draws the preview * Draws the preview
* @param canvas The canvas where the preview is drawn. * @param canvas The canvas where the preview is drawn.
@ -167,7 +136,8 @@ public final class GestureTrailsDrawingPreview extends AbstractDrawingPreview {
final boolean needsUpdatingGestureTrail = drawGestureTrails( final boolean needsUpdatingGestureTrail = drawGestureTrails(
mOffscreenCanvas, mGesturePaint, mDirtyRect); mOffscreenCanvas, mGesturePaint, mDirtyRect);
if (needsUpdatingGestureTrail) { if (needsUpdatingGestureTrail) {
mDrawingHandler.postUpdateGestureTrailPreview(); mDrawingHandler.removeCallbacks(this);
mDrawingHandler.postDelayed(this, mDrawingParams.mUpdateInterval);
} }
// Transfer offscreen buffer to screen. // Transfer offscreen buffer to screen.
if (!mDirtyRect.isEmpty()) { if (!mDirtyRect.isEmpty()) {
@ -199,6 +169,6 @@ public final class GestureTrailsDrawingPreview extends AbstractDrawingPreview {
trail.addStroke(tracker.getGestureStrokeDrawingPoints(), tracker.getDownTime()); trail.addStroke(tracker.getGestureStrokeDrawingPoints(), tracker.getDownTime());
// TODO: Should narrow the invalidate region. // TODO: Should narrow the invalidate region.
getDrawingView().invalidate(); invalidateDrawingView();
} }
} }

View file

@ -20,7 +20,6 @@ import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Path; import android.graphics.Path;
import android.view.View;
import com.android.inputmethod.keyboard.PointerTracker; import com.android.inputmethod.keyboard.PointerTracker;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
@ -28,6 +27,11 @@ import com.android.inputmethod.latin.utils.CoordinateUtils;
/** /**
* Draw rubber band preview graphics during sliding key input. * Draw rubber band preview graphics during sliding key input.
*
* @attr ref R.styleable#MainKeyboardView_slidingKeyInputPreviewColor
* @attr ref R.styleable#MainKeyboardView_slidingKeyInputPreviewWidth
* @attr ref R.styleable#MainKeyboardView_slidingKeyInputPreviewBodyRatio
* @attr ref R.styleable#MainKeyboardView_slidingKeyInputPreviewShadowRatio
*/ */
public final class SlidingKeyInputDrawingPreview extends AbstractDrawingPreview { public final class SlidingKeyInputDrawingPreview extends AbstractDrawingPreview {
private final float mPreviewBodyRadius; private final float mPreviewBodyRadius;
@ -40,9 +44,7 @@ public final class SlidingKeyInputDrawingPreview extends AbstractDrawingPreview
private final RoundedLine mRoundedLine = new RoundedLine(); private final RoundedLine mRoundedLine = new RoundedLine();
private final Paint mPaint = new Paint(); private final Paint mPaint = new Paint();
public SlidingKeyInputDrawingPreview(final View drawingView, public SlidingKeyInputDrawingPreview(final TypedArray mainKeyboardViewAttr) {
final TypedArray mainKeyboardViewAttr) {
super(drawingView);
final int previewColor = mainKeyboardViewAttr.getColor( final int previewColor = mainKeyboardViewAttr.getColor(
R.styleable.MainKeyboardView_slidingKeyInputPreviewColor, 0); R.styleable.MainKeyboardView_slidingKeyInputPreviewColor, 0);
final float previewRadius = mainKeyboardViewAttr.getDimension( final float previewRadius = mainKeyboardViewAttr.getDimension(
@ -69,7 +71,7 @@ public final class SlidingKeyInputDrawingPreview extends AbstractDrawingPreview
public void dismissSlidingKeyInputPreview() { public void dismissSlidingKeyInputPreview() {
mShowsSlidingKeyInputPreview = false; mShowsSlidingKeyInputPreview = false;
getDrawingView().invalidate(); invalidateDrawingView();
} }
/** /**
@ -99,6 +101,6 @@ public final class SlidingKeyInputDrawingPreview extends AbstractDrawingPreview
tracker.getDownCoordinates(mPreviewFrom); tracker.getDownCoordinates(mPreviewFrom);
tracker.getLastCoordinates(mPreviewTo); tracker.getLastCoordinates(mPreviewTo);
mShowsSlidingKeyInputPreview = true; mShowsSlidingKeyInputPreview = true;
getDrawingView().invalidate(); invalidateDrawingView();
} }
} }