2012-08-20 03:57:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
|
|
* in compliance with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
|
|
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.keyboard.internal;
|
|
|
|
|
|
|
|
import android.content.res.TypedArray;
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.Paint;
|
2012-09-06 03:12:10 +00:00
|
|
|
import android.graphics.PorterDuff;
|
|
|
|
import android.graphics.PorterDuffXfermode;
|
|
|
|
import android.graphics.Xfermode;
|
2012-08-20 03:57:34 +00:00
|
|
|
import android.os.SystemClock;
|
|
|
|
|
|
|
|
import com.android.inputmethod.latin.Constants;
|
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
import com.android.inputmethod.latin.ResizableIntArray;
|
|
|
|
|
2012-09-06 03:12:10 +00:00
|
|
|
final class GesturePreviewTrail {
|
2012-08-20 03:57:34 +00:00
|
|
|
private static final int DEFAULT_CAPACITY = GestureStrokeWithPreviewTrail.PREVIEW_CAPACITY;
|
|
|
|
|
|
|
|
private final ResizableIntArray mXCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
private final ResizableIntArray mYCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
|
|
|
|
private final ResizableIntArray mEventTimes = new ResizableIntArray(DEFAULT_CAPACITY);
|
2012-08-24 08:48:23 +00:00
|
|
|
private int mCurrentStrokeId = -1;
|
2012-08-20 03:57:34 +00:00
|
|
|
private long mCurrentDownTime;
|
2012-08-27 05:43:39 +00:00
|
|
|
private int mTrailStartIndex;
|
2012-08-20 03:57:34 +00:00
|
|
|
|
2012-09-06 03:12:10 +00:00
|
|
|
private final static Xfermode PORTER_DUFF_MODE_SRC =
|
|
|
|
new PorterDuffXfermode(PorterDuff.Mode.SRC);
|
|
|
|
|
2012-08-20 03:57:34 +00:00
|
|
|
// Use this value as imaginary zero because x-coordinates may be zero.
|
|
|
|
private static final int DOWN_EVENT_MARKER = -128;
|
|
|
|
|
2012-09-06 03:12:10 +00:00
|
|
|
static final class Params {
|
|
|
|
public final int mTrailColor;
|
|
|
|
public final float mTrailStartWidth;
|
|
|
|
public final float mTrailEndWidth;
|
2012-08-20 03:57:34 +00:00
|
|
|
public final int mFadeoutStartDelay;
|
|
|
|
public final int mFadeoutDuration;
|
|
|
|
public final int mUpdateInterval;
|
|
|
|
|
2012-09-06 03:12:10 +00:00
|
|
|
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);
|
2012-08-20 03:57:34 +00:00
|
|
|
mFadeoutStartDelay = keyboardViewAttr.getInt(
|
|
|
|
R.styleable.KeyboardView_gesturePreviewTrailFadeoutStartDelay, 0);
|
|
|
|
mFadeoutDuration = keyboardViewAttr.getInt(
|
|
|
|
R.styleable.KeyboardView_gesturePreviewTrailFadeoutDuration, 0);
|
2012-09-06 03:12:10 +00:00
|
|
|
mTrailLingerDuration = mFadeoutStartDelay + mFadeoutDuration;
|
2012-08-20 03:57:34 +00:00
|
|
|
mUpdateInterval = keyboardViewAttr.getInt(
|
|
|
|
R.styleable.KeyboardView_gesturePreviewTrailUpdateInterval, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int markAsDownEvent(final int xCoord) {
|
|
|
|
return DOWN_EVENT_MARKER - xCoord;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isDownEventXCoord(final int xCoordOrMark) {
|
|
|
|
return xCoordOrMark <= DOWN_EVENT_MARKER;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int getXCoordValue(final int xCoordOrMark) {
|
|
|
|
return isDownEventXCoord(xCoordOrMark)
|
|
|
|
? DOWN_EVENT_MARKER - xCoordOrMark : xCoordOrMark;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addStroke(final GestureStrokeWithPreviewTrail stroke, final long downTime) {
|
|
|
|
final int strokeId = stroke.getGestureStrokeId();
|
|
|
|
final boolean isNewStroke = strokeId != mCurrentStrokeId;
|
|
|
|
final int trailSize = mEventTimes.getLength();
|
|
|
|
stroke.appendPreviewStroke(mEventTimes, mXCoordinates, mYCoordinates);
|
|
|
|
final int newTrailSize = mEventTimes.getLength();
|
|
|
|
if (stroke.getGestureStrokePreviewSize() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isNewStroke) {
|
|
|
|
final int elapsedTime = (int)(downTime - mCurrentDownTime);
|
|
|
|
final int[] eventTimes = mEventTimes.getPrimitiveArray();
|
2012-08-27 05:43:39 +00:00
|
|
|
for (int i = mTrailStartIndex; i < trailSize; i++) {
|
2012-08-20 03:57:34 +00:00
|
|
|
eventTimes[i] -= elapsedTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newTrailSize > trailSize) {
|
|
|
|
final int[] xCoords = mXCoordinates.getPrimitiveArray();
|
|
|
|
xCoords[trailSize] = markAsDownEvent(xCoords[trailSize]);
|
|
|
|
}
|
|
|
|
mCurrentDownTime = downTime;
|
|
|
|
mCurrentStrokeId = strokeId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 03:12:10 +00:00
|
|
|
private static int getAlpha(final int elapsedTime, final Params params) {
|
|
|
|
if (elapsedTime < params.mFadeoutStartDelay) {
|
2012-08-20 03:57:34 +00:00
|
|
|
return Constants.Color.ALPHA_OPAQUE;
|
|
|
|
}
|
|
|
|
final int decreasingAlpha = Constants.Color.ALPHA_OPAQUE
|
2012-09-06 03:12:10 +00:00
|
|
|
* (elapsedTime - params.mFadeoutStartDelay)
|
|
|
|
/ params.mFadeoutDuration;
|
2012-08-20 03:57:34 +00:00
|
|
|
return Constants.Color.ALPHA_OPAQUE - decreasingAlpha;
|
|
|
|
}
|
|
|
|
|
2012-09-06 03:12:10 +00:00
|
|
|
private static float getWidth(final int elapsedTime, final Params params) {
|
|
|
|
return Math.max((params.mTrailLingerDuration - elapsedTime)
|
|
|
|
* (params.mTrailStartWidth - params.mTrailEndWidth)
|
|
|
|
/ params.mTrailLingerDuration, 0.0f);
|
|
|
|
}
|
|
|
|
|
2012-08-20 03:57:34 +00:00
|
|
|
/**
|
|
|
|
* Draw 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
|
2012-09-06 03:12:10 +00:00
|
|
|
* @param params The drawing parameters of gesture preview trail
|
2012-08-20 03:57:34 +00:00
|
|
|
* @return true if some gesture preview trails remain to be drawn
|
|
|
|
*/
|
2012-09-06 03:12:10 +00:00
|
|
|
public boolean drawGestureTrail(final Canvas canvas, final Paint paint, final Params params) {
|
2012-08-20 03:57:34 +00:00
|
|
|
final int trailSize = mEventTimes.getLength();
|
|
|
|
if (trailSize == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
final int[] eventTimes = mEventTimes.getPrimitiveArray();
|
|
|
|
final int[] xCoords = mXCoordinates.getPrimitiveArray();
|
|
|
|
final int[] yCoords = mYCoordinates.getPrimitiveArray();
|
|
|
|
final int sinceDown = (int)(SystemClock.uptimeMillis() - mCurrentDownTime);
|
|
|
|
int startIndex;
|
2012-08-27 05:43:39 +00:00
|
|
|
for (startIndex = mTrailStartIndex; startIndex < trailSize; startIndex++) {
|
2012-08-20 03:57:34 +00:00
|
|
|
final int elapsedTime = sinceDown - eventTimes[startIndex];
|
|
|
|
// Skip too old trail points.
|
2012-09-06 03:12:10 +00:00
|
|
|
if (elapsedTime < params.mTrailLingerDuration) {
|
2012-08-20 03:57:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-08-27 05:43:39 +00:00
|
|
|
mTrailStartIndex = startIndex;
|
2012-08-20 03:57:34 +00:00
|
|
|
|
|
|
|
if (startIndex < trailSize) {
|
|
|
|
int lastX = getXCoordValue(xCoords[startIndex]);
|
|
|
|
int lastY = yCoords[startIndex];
|
2012-09-06 03:12:10 +00:00
|
|
|
paint.setColor(params.mTrailColor);
|
|
|
|
paint.setStyle(Paint.Style.STROKE);
|
|
|
|
paint.setStrokeCap(Paint.Cap.ROUND);
|
|
|
|
paint.setXfermode(PORTER_DUFF_MODE_SRC);
|
2012-08-20 03:57:34 +00:00
|
|
|
for (int i = startIndex + 1; i < trailSize - 1; i++) {
|
|
|
|
final int x = xCoords[i];
|
|
|
|
final int y = yCoords[i];
|
|
|
|
final int elapsedTime = sinceDown - eventTimes[i];
|
|
|
|
// Draw trail line only when the current point isn't a down point.
|
|
|
|
if (!isDownEventXCoord(x)) {
|
2012-09-06 03:12:10 +00:00
|
|
|
final int alpha = getAlpha(elapsedTime, params);
|
|
|
|
paint.setAlpha(alpha);
|
|
|
|
final float width = getWidth(elapsedTime, params);
|
|
|
|
paint.setStrokeWidth(width);
|
2012-08-20 03:57:34 +00:00
|
|
|
canvas.drawLine(lastX, lastY, x, y, paint);
|
|
|
|
}
|
|
|
|
lastX = getXCoordValue(x);
|
|
|
|
lastY = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final int newSize = trailSize - startIndex;
|
2012-08-27 05:43:39 +00:00
|
|
|
if (newSize < startIndex) {
|
|
|
|
mTrailStartIndex = 0;
|
|
|
|
if (newSize > 0) {
|
|
|
|
System.arraycopy(eventTimes, startIndex, eventTimes, 0, newSize);
|
|
|
|
System.arraycopy(xCoords, startIndex, xCoords, 0, newSize);
|
|
|
|
System.arraycopy(yCoords, startIndex, yCoords, 0, newSize);
|
|
|
|
}
|
|
|
|
mEventTimes.setLength(newSize);
|
|
|
|
mXCoordinates.setLength(newSize);
|
|
|
|
mYCoordinates.setLength(newSize);
|
|
|
|
}
|
2012-08-20 03:57:34 +00:00
|
|
|
return newSize > 0;
|
|
|
|
}
|
|
|
|
}
|