am f5639977: am a47a17a2: Merge "Revise gesture preview trail design" into jb-mr1-dev

* commit 'f56399772e11cc27a076357645cc98f59df2df97':
  Revise gesture preview trail design
main
Tadashi G. Takaoka 2012-09-06 09:12:28 -07:00 committed by Android Git Automerger
commit 568cca5004
5 changed files with 65 additions and 39 deletions

View File

@ -90,7 +90,8 @@
<!-- Interval of updating gesture preview trail in millisecond. --> <!-- Interval of updating gesture preview trail in millisecond. -->
<attr name="gesturePreviewTrailUpdateInterval" format="integer" /> <attr name="gesturePreviewTrailUpdateInterval" format="integer" />
<attr name="gesturePreviewTrailColor" format="color" /> <attr name="gesturePreviewTrailColor" format="color" />
<attr name="gesturePreviewTrailWidth" format="dimension" /> <attr name="gesturePreviewTrailStartWidth" format="dimension" />
<attr name="gesturePreviewTrailEndWidth" format="dimension" />
</declare-styleable> </declare-styleable>
<declare-styleable name="MainKeyboardView"> <declare-styleable name="MainKeyboardView">

View File

@ -100,7 +100,8 @@
<fraction name="center_suggestion_percentile">36%</fraction> <fraction name="center_suggestion_percentile">36%</fraction>
<!-- Gesture preview trail parameters --> <!-- Gesture preview trail parameters -->
<dimen name="gesture_preview_trail_width">2.5dp</dimen> <dimen name="gesture_preview_trail_start_width">18.0dp</dimen>
<dimen name="gesture_preview_trail_end_width">2.5dp</dimen>
<!-- Gesture floating preview text parameters --> <!-- Gesture floating preview text parameters -->
<dimen name="gesture_floating_preview_text_size">24dp</dimen> <dimen name="gesture_floating_preview_text_size">24dp</dimen>
<dimen name="gesture_floating_preview_text_offset">73dp</dimen> <dimen name="gesture_floating_preview_text_offset">73dp</dimen>

View File

@ -77,7 +77,8 @@
<item name="gesturePreviewTrailFadeoutDuration">@integer/config_gesture_preview_trail_fadeout_duration</item> <item name="gesturePreviewTrailFadeoutDuration">@integer/config_gesture_preview_trail_fadeout_duration</item>
<item name="gesturePreviewTrailUpdateInterval">@integer/config_gesture_preview_trail_update_interval</item> <item name="gesturePreviewTrailUpdateInterval">@integer/config_gesture_preview_trail_update_interval</item>
<item name="gesturePreviewTrailColor">@android:color/holo_blue_light</item> <item name="gesturePreviewTrailColor">@android:color/holo_blue_light</item>
<item name="gesturePreviewTrailWidth">@dimen/gesture_preview_trail_width</item> <item name="gesturePreviewTrailStartWidth">@dimen/gesture_preview_trail_start_width</item>
<item name="gesturePreviewTrailEndWidth">@dimen/gesture_preview_trail_end_width</item>
<!-- Common attributes of MainKeyboardView --> <!-- Common attributes of MainKeyboardView -->
<item name="keyHysteresisDistance">@dimen/config_key_hysteresis_distance</item> <item name="keyHysteresisDistance">@dimen/config_key_hysteresis_distance</item>
<item name="touchNoiseThresholdTime">@integer/config_touch_noise_threshold_time</item> <item name="touchNoiseThresholdTime">@integer/config_touch_noise_threshold_time</item>

View File

@ -17,16 +17,18 @@ package com.android.inputmethod.keyboard.internal;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.os.SystemClock; import android.os.SystemClock;
import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Constants;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.ResizableIntArray; import com.android.inputmethod.latin.ResizableIntArray;
class GesturePreviewTrail { final class GesturePreviewTrail {
private static final int DEFAULT_CAPACITY = GestureStrokeWithPreviewTrail.PREVIEW_CAPACITY; private static final int DEFAULT_CAPACITY = GestureStrokeWithPreviewTrail.PREVIEW_CAPACITY;
private final GesturePreviewTrailParams mPreviewParams;
private final ResizableIntArray mXCoordinates = new ResizableIntArray(DEFAULT_CAPACITY); private final ResizableIntArray mXCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
private final ResizableIntArray mYCoordinates = new ResizableIntArray(DEFAULT_CAPACITY); private final ResizableIntArray mYCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
private final ResizableIntArray mEventTimes = new ResizableIntArray(DEFAULT_CAPACITY); private final ResizableIntArray mEventTimes = new ResizableIntArray(DEFAULT_CAPACITY);
@ -34,28 +36,39 @@ class GesturePreviewTrail {
private long mCurrentDownTime; private long mCurrentDownTime;
private int mTrailStartIndex; private int mTrailStartIndex;
private final static Xfermode PORTER_DUFF_MODE_SRC =
new PorterDuffXfermode(PorterDuff.Mode.SRC);
// Use this value as imaginary zero because x-coordinates may be zero. // Use this value as imaginary zero because x-coordinates may be zero.
private static final int DOWN_EVENT_MARKER = -128; private static final int DOWN_EVENT_MARKER = -128;
static class GesturePreviewTrailParams { static final class Params {
public final int mTrailColor;
public final float mTrailStartWidth;
public final float mTrailEndWidth;
public final int mFadeoutStartDelay; public final int mFadeoutStartDelay;
public final int mFadeoutDuration; public final int mFadeoutDuration;
public final int mUpdateInterval; public final int mUpdateInterval;
public GesturePreviewTrailParams(final TypedArray keyboardViewAttr) { public final int mTrailLingerDuration;
public Params(final TypedArray keyboardViewAttr) {
mTrailColor = keyboardViewAttr.getColor(
R.styleable.KeyboardView_gesturePreviewTrailColor, 0);
mTrailStartWidth = keyboardViewAttr.getDimension(
R.styleable.KeyboardView_gesturePreviewTrailStartWidth, 0.0f);
mTrailEndWidth = keyboardViewAttr.getDimension(
R.styleable.KeyboardView_gesturePreviewTrailEndWidth, 0.0f);
mFadeoutStartDelay = keyboardViewAttr.getInt( mFadeoutStartDelay = keyboardViewAttr.getInt(
R.styleable.KeyboardView_gesturePreviewTrailFadeoutStartDelay, 0); R.styleable.KeyboardView_gesturePreviewTrailFadeoutStartDelay, 0);
mFadeoutDuration = keyboardViewAttr.getInt( mFadeoutDuration = keyboardViewAttr.getInt(
R.styleable.KeyboardView_gesturePreviewTrailFadeoutDuration, 0); R.styleable.KeyboardView_gesturePreviewTrailFadeoutDuration, 0);
mTrailLingerDuration = mFadeoutStartDelay + mFadeoutDuration;
mUpdateInterval = keyboardViewAttr.getInt( mUpdateInterval = keyboardViewAttr.getInt(
R.styleable.KeyboardView_gesturePreviewTrailUpdateInterval, 0); R.styleable.KeyboardView_gesturePreviewTrailUpdateInterval, 0);
} }
} }
public GesturePreviewTrail(final GesturePreviewTrailParams params) {
mPreviewParams = params;
}
private static int markAsDownEvent(final int xCoord) { private static int markAsDownEvent(final int xCoord) {
return DOWN_EVENT_MARKER - xCoord; return DOWN_EVENT_MARKER - xCoord;
} }
@ -94,23 +107,30 @@ class GesturePreviewTrail {
} }
} }
private int getAlpha(final int elapsedTime) { private static int getAlpha(final int elapsedTime, final Params params) {
if (elapsedTime < mPreviewParams.mFadeoutStartDelay) { if (elapsedTime < params.mFadeoutStartDelay) {
return Constants.Color.ALPHA_OPAQUE; return Constants.Color.ALPHA_OPAQUE;
} }
final int decreasingAlpha = Constants.Color.ALPHA_OPAQUE final int decreasingAlpha = Constants.Color.ALPHA_OPAQUE
* (elapsedTime - mPreviewParams.mFadeoutStartDelay) * (elapsedTime - params.mFadeoutStartDelay)
/ mPreviewParams.mFadeoutDuration; / params.mFadeoutDuration;
return Constants.Color.ALPHA_OPAQUE - decreasingAlpha; return Constants.Color.ALPHA_OPAQUE - decreasingAlpha;
} }
private static float getWidth(final int elapsedTime, final Params params) {
return Math.max((params.mTrailLingerDuration - elapsedTime)
* (params.mTrailStartWidth - params.mTrailEndWidth)
/ params.mTrailLingerDuration, 0.0f);
}
/** /**
* Draw gesture preview trail * Draw gesture preview trail
* @param canvas The canvas to draw the gesture preview trail * @param canvas The canvas to draw the gesture preview trail
* @param paint The paint object to be used to draw the gesture preview trail * @param paint The paint object to be used to draw the gesture preview trail
* @param params The drawing parameters of gesture preview trail
* @return true if some gesture preview trails remain to be drawn * @return true if some gesture preview trails remain to be drawn
*/ */
public boolean drawGestureTrail(final Canvas canvas, final Paint paint) { public boolean drawGestureTrail(final Canvas canvas, final Paint paint, final Params params) {
final int trailSize = mEventTimes.getLength(); final int trailSize = mEventTimes.getLength();
if (trailSize == 0) { if (trailSize == 0) {
return false; return false;
@ -120,13 +140,11 @@ class GesturePreviewTrail {
final int[] xCoords = mXCoordinates.getPrimitiveArray(); final int[] xCoords = mXCoordinates.getPrimitiveArray();
final int[] yCoords = mYCoordinates.getPrimitiveArray(); final int[] yCoords = mYCoordinates.getPrimitiveArray();
final int sinceDown = (int)(SystemClock.uptimeMillis() - mCurrentDownTime); final int sinceDown = (int)(SystemClock.uptimeMillis() - mCurrentDownTime);
final int lingeringDuration = mPreviewParams.mFadeoutStartDelay
+ mPreviewParams.mFadeoutDuration;
int startIndex; int startIndex;
for (startIndex = mTrailStartIndex; startIndex < trailSize; startIndex++) { for (startIndex = mTrailStartIndex; startIndex < trailSize; startIndex++) {
final int elapsedTime = sinceDown - eventTimes[startIndex]; final int elapsedTime = sinceDown - eventTimes[startIndex];
// Skip too old trail points. // Skip too old trail points.
if (elapsedTime < lingeringDuration) { if (elapsedTime < params.mTrailLingerDuration) {
break; break;
} }
} }
@ -135,13 +153,20 @@ class GesturePreviewTrail {
if (startIndex < trailSize) { if (startIndex < trailSize) {
int lastX = getXCoordValue(xCoords[startIndex]); int lastX = getXCoordValue(xCoords[startIndex]);
int lastY = yCoords[startIndex]; int lastY = yCoords[startIndex];
paint.setColor(params.mTrailColor);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setXfermode(PORTER_DUFF_MODE_SRC);
for (int i = startIndex + 1; i < trailSize - 1; i++) { for (int i = startIndex + 1; i < trailSize - 1; i++) {
final int x = xCoords[i]; final int x = xCoords[i];
final int y = yCoords[i]; final int y = yCoords[i];
final int elapsedTime = sinceDown - eventTimes[i]; final int elapsedTime = sinceDown - eventTimes[i];
// Draw trail line only when the current point isn't a down point. // Draw trail line only when the current point isn't a down point.
if (!isDownEventXCoord(x)) { if (!isDownEventXCoord(x)) {
paint.setAlpha(getAlpha(elapsedTime)); final int alpha = getAlpha(elapsedTime, params);
paint.setAlpha(alpha);
final float width = getWidth(elapsedTime, params);
paint.setStrokeWidth(width);
canvas.drawLine(lastX, lastY, x, y, paint); canvas.drawLine(lastX, lastY, x, y, paint);
} }
lastX = getXCoordValue(x); lastX = getXCoordValue(x);

View File

@ -21,6 +21,8 @@ import android.content.res.TypedArray;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Paint.Align; import android.graphics.Paint.Align;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.RectF; import android.graphics.RectF;
import android.os.Message; import android.os.Message;
@ -30,14 +32,12 @@ import android.util.SparseArray;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import com.android.inputmethod.keyboard.PointerTracker; import com.android.inputmethod.keyboard.PointerTracker;
import com.android.inputmethod.keyboard.internal.GesturePreviewTrail.GesturePreviewTrailParams; import com.android.inputmethod.keyboard.internal.GesturePreviewTrail.Params;
import com.android.inputmethod.latin.CollectionUtils; import com.android.inputmethod.latin.CollectionUtils;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.StaticInnerHandlerWrapper; import com.android.inputmethod.latin.StaticInnerHandlerWrapper;
public class PreviewPlacerView extends RelativeLayout { public class PreviewPlacerView extends RelativeLayout {
private final Paint mGesturePaint;
private final Paint mTextPaint;
private final int mGestureFloatingPreviewTextColor; private final int mGestureFloatingPreviewTextColor;
private final int mGestureFloatingPreviewTextOffset; private final int mGestureFloatingPreviewTextOffset;
private final int mGestureFloatingPreviewColor; private final int mGestureFloatingPreviewColor;
@ -51,8 +51,11 @@ public class PreviewPlacerView extends RelativeLayout {
private final SparseArray<GesturePreviewTrail> mGesturePreviewTrails = private final SparseArray<GesturePreviewTrail> mGesturePreviewTrails =
CollectionUtils.newSparseArray(); CollectionUtils.newSparseArray();
private final GesturePreviewTrailParams mGesturePreviewTrailParams; private final Params mGesturePreviewTrailParams;
private final Paint mGesturePaint;
private boolean mDrawsGesturePreviewTrail;
private final Paint mTextPaint;
private String mGestureFloatingPreviewText; private String mGestureFloatingPreviewText;
private final int mGestureFloatingPreviewTextHeight; private final int mGestureFloatingPreviewTextHeight;
// {@link RectF} is needed for {@link Canvas#drawRoundRect(RectF, float, float, Paint)}. // {@link RectF} is needed for {@link Canvas#drawRoundRect(RectF, float, float, Paint)}.
@ -60,8 +63,6 @@ public class PreviewPlacerView extends RelativeLayout {
private int mLastPointerX; private int mLastPointerX;
private int mLastPointerY; private int mLastPointerY;
private static final char[] TEXT_HEIGHT_REFERENCE_CHAR = { 'M' }; private static final char[] TEXT_HEIGHT_REFERENCE_CHAR = { 'M' };
private boolean mDrawsGesturePreviewTrail;
private boolean mDrawsGestureFloatingPreviewText; private boolean mDrawsGestureFloatingPreviewText;
private final DrawingHandler mDrawingHandler; private final DrawingHandler mDrawingHandler;
@ -70,10 +71,10 @@ public class PreviewPlacerView extends RelativeLayout {
private static final int MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT = 0; private static final int MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT = 0;
private static final int MSG_UPDATE_GESTURE_PREVIEW_TRAIL = 1; private static final int MSG_UPDATE_GESTURE_PREVIEW_TRAIL = 1;
private final GesturePreviewTrailParams mGesturePreviewTrailParams; private final Params mGesturePreviewTrailParams;
public DrawingHandler(final PreviewPlacerView outerInstance, public DrawingHandler(final PreviewPlacerView outerInstance,
final GesturePreviewTrailParams gesturePreviewTrailParams) { final Params gesturePreviewTrailParams) {
super(outerInstance); super(outerInstance);
mGesturePreviewTrailParams = gesturePreviewTrailParams; mGesturePreviewTrailParams = gesturePreviewTrailParams;
} }
@ -146,21 +147,13 @@ public class PreviewPlacerView extends RelativeLayout {
R.styleable.KeyboardView_gestureFloatingPreviewRoundRadius, 0.0f); R.styleable.KeyboardView_gestureFloatingPreviewRoundRadius, 0.0f);
mGestureFloatingPreviewTextLingerTimeout = keyboardViewAttr.getInt( mGestureFloatingPreviewTextLingerTimeout = keyboardViewAttr.getInt(
R.styleable.KeyboardView_gestureFloatingPreviewTextLingerTimeout, 0); R.styleable.KeyboardView_gestureFloatingPreviewTextLingerTimeout, 0);
final int gesturePreviewTrailColor = keyboardViewAttr.getColor( mGesturePreviewTrailParams = new Params(keyboardViewAttr);
R.styleable.KeyboardView_gesturePreviewTrailColor, 0);
final int gesturePreviewTrailWidth = keyboardViewAttr.getDimensionPixelSize(
R.styleable.KeyboardView_gesturePreviewTrailWidth, 0);
mGesturePreviewTrailParams = new GesturePreviewTrailParams(keyboardViewAttr);
keyboardViewAttr.recycle(); keyboardViewAttr.recycle();
mDrawingHandler = new DrawingHandler(this, mGesturePreviewTrailParams); mDrawingHandler = new DrawingHandler(this, mGesturePreviewTrailParams);
final Paint gesturePaint = new Paint(); final Paint gesturePaint = new Paint();
gesturePaint.setAntiAlias(true); gesturePaint.setAntiAlias(true);
gesturePaint.setStyle(Paint.Style.STROKE);
gesturePaint.setStrokeJoin(Paint.Join.ROUND);
gesturePaint.setColor(gesturePreviewTrailColor);
gesturePaint.setStrokeWidth(gesturePreviewTrailWidth);
mGesturePaint = gesturePaint; mGesturePaint = gesturePaint;
final Paint textPaint = new Paint(); final Paint textPaint = new Paint();
@ -172,6 +165,10 @@ public class PreviewPlacerView extends RelativeLayout {
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);
mGestureFloatingPreviewTextHeight = textRect.height(); mGestureFloatingPreviewTextHeight = textRect.height();
final Paint layerPaint = new Paint();
layerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
setLayerType(LAYER_TYPE_HARDWARE, layerPaint);
} }
public void setOrigin(final int x, final int y) { public void setOrigin(final int x, final int y) {
@ -190,7 +187,7 @@ public class PreviewPlacerView extends RelativeLayout {
synchronized (mGesturePreviewTrails) { synchronized (mGesturePreviewTrails) {
trail = mGesturePreviewTrails.get(tracker.mPointerId); trail = mGesturePreviewTrails.get(tracker.mPointerId);
if (trail == null) { if (trail == null) {
trail = new GesturePreviewTrail(mGesturePreviewTrailParams); trail = new GesturePreviewTrail();
mGesturePreviewTrails.put(tracker.mPointerId, trail); mGesturePreviewTrails.put(tracker.mPointerId, trail);
} }
} }
@ -214,7 +211,8 @@ public class PreviewPlacerView extends RelativeLayout {
for (int index = 0; index < trailsCount; index++) { for (int index = 0; index < trailsCount; index++) {
final GesturePreviewTrail trail = mGesturePreviewTrails.valueAt(index); final GesturePreviewTrail trail = mGesturePreviewTrails.valueAt(index);
needsUpdatingGesturePreviewTrail |= needsUpdatingGesturePreviewTrail |=
trail.drawGestureTrail(canvas, mGesturePaint); trail.drawGestureTrail(canvas, mGesturePaint,
mGesturePreviewTrailParams);
} }
} }
if (needsUpdatingGesturePreviewTrail) { if (needsUpdatingGesturePreviewTrail) {