Use ResizableIntArray in GestureStroke
Change-Id: I034e80df03c7c6c0895fdf2c03763627d410d425main
parent
2474b37abd
commit
7519091f7c
|
@ -21,13 +21,15 @@ import android.util.FloatMath;
|
||||||
|
|
||||||
import com.android.inputmethod.latin.Constants;
|
import com.android.inputmethod.latin.Constants;
|
||||||
import com.android.inputmethod.latin.InputPointers;
|
import com.android.inputmethod.latin.InputPointers;
|
||||||
|
import com.android.inputmethod.latin.ResizableIntArray;
|
||||||
|
|
||||||
public class GestureStroke {
|
public class GestureStroke {
|
||||||
public static final int DEFAULT_CAPACITY = 128;
|
public static final int DEFAULT_CAPACITY = 128;
|
||||||
|
|
||||||
private final int mPointerId;
|
private final int mPointerId;
|
||||||
// TODO: Replace this {@link InputPointers} with a set of {@link ScalableIntArray}s.
|
private final ResizableIntArray mEventTimes = new ResizableIntArray(DEFAULT_CAPACITY);
|
||||||
private final InputPointers mInputPointers = new InputPointers(DEFAULT_CAPACITY);
|
private final ResizableIntArray mXCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
|
||||||
|
private final ResizableIntArray mYCoordinates = new ResizableIntArray(DEFAULT_CAPACITY);
|
||||||
private float mLength;
|
private float mLength;
|
||||||
private float mAngle;
|
private float mAngle;
|
||||||
private int mIncrementalRecognitionSize;
|
private int mIncrementalRecognitionSize;
|
||||||
|
@ -85,7 +87,9 @@ public class GestureStroke {
|
||||||
mIncrementalRecognitionSize = 0;
|
mIncrementalRecognitionSize = 0;
|
||||||
mLastIncrementalBatchSize = 0;
|
mLastIncrementalBatchSize = 0;
|
||||||
mLastPointTime = 0;
|
mLastPointTime = 0;
|
||||||
mInputPointers.reset();
|
mEventTimes.setLength(0);
|
||||||
|
mXCoordinates.setLength(0);
|
||||||
|
mYCoordinates.setLength(0);
|
||||||
mDrawingRect.setEmpty();
|
mDrawingRect.setEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,26 +100,24 @@ public class GestureStroke {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPoint(final int x, final int y, final int time, final boolean isHistorical) {
|
public void addPoint(final int x, final int y, final int time, final boolean isHistorical) {
|
||||||
final int size = mInputPointers.getPointerSize();
|
final int size = mEventTimes.getLength();
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
mInputPointers.addPointer(x, y, mPointerId, time);
|
mEventTimes.add(time);
|
||||||
|
mXCoordinates.add(x);
|
||||||
|
mYCoordinates.add(y);
|
||||||
if (!isHistorical) {
|
if (!isHistorical) {
|
||||||
updateLastPoint(x, y, time);
|
updateLastPoint(x, y, time);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final int[] xCoords = mInputPointers.getXCoordinates();
|
|
||||||
final int[] yCoords = mInputPointers.getYCoordinates();
|
final int lastX = mXCoordinates.get(size - 1);
|
||||||
final int lastX = xCoords[size - 1];
|
final int lastY = mYCoordinates.get(size - 1);
|
||||||
final int lastY = yCoords[size - 1];
|
|
||||||
final float dist = getDistance(lastX, lastY, x, y);
|
final float dist = getDistance(lastX, lastY, x, y);
|
||||||
if (dist > mMinGestureSampleLength) {
|
if (dist > mMinGestureSampleLength) {
|
||||||
mInputPointers.addPointer(x, y, mPointerId, time);
|
mEventTimes.add(time);
|
||||||
if (mDrawingRect.isEmpty()) {
|
mXCoordinates.add(x);
|
||||||
mDrawingRect.set(x - 1, y - 1, x + 1, y + 1);
|
mYCoordinates.add(y);
|
||||||
} else {
|
|
||||||
mDrawingRect.union(x, y);
|
|
||||||
}
|
|
||||||
mLength += dist;
|
mLength += dist;
|
||||||
final float angle = getAngle(lastX, lastY, x, y);
|
final float angle = getAngle(lastX, lastY, x, y);
|
||||||
if (size > 1) {
|
if (size > 1) {
|
||||||
|
@ -142,15 +144,17 @@ public class GestureStroke {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void appendAllBatchPoints(final InputPointers out) {
|
public void appendAllBatchPoints(final InputPointers out) {
|
||||||
final int size = mInputPointers.getPointerSize();
|
appendBatchPoints(out, mEventTimes.getLength());
|
||||||
out.append(mInputPointers, mLastIncrementalBatchSize, size - mLastIncrementalBatchSize);
|
|
||||||
mLastIncrementalBatchSize = size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void appendIncrementalBatchPoints(final InputPointers out) {
|
public void appendIncrementalBatchPoints(final InputPointers out) {
|
||||||
out.append(mInputPointers, mLastIncrementalBatchSize,
|
appendBatchPoints(out, mIncrementalRecognitionSize);
|
||||||
mIncrementalRecognitionSize - mLastIncrementalBatchSize);
|
}
|
||||||
mLastIncrementalBatchSize = mIncrementalRecognitionSize;
|
|
||||||
|
private void appendBatchPoints(final InputPointers out, final int size) {
|
||||||
|
out.append(mPointerId, mEventTimes, mXCoordinates, mYCoordinates,
|
||||||
|
mLastIncrementalBatchSize, size - mLastIncrementalBatchSize);
|
||||||
|
mLastIncrementalBatchSize = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static float getDistance(final int p1x, final int p1y,
|
private static float getDistance(final int p1x, final int p1y,
|
||||||
|
@ -179,9 +183,9 @@ public class GestureStroke {
|
||||||
public void drawGestureTrail(Canvas canvas, Paint paint, int lastX, int lastY) {
|
public void drawGestureTrail(Canvas canvas, Paint paint, int lastX, int lastY) {
|
||||||
// TODO: These paint parameter interpolation should be tunable, possibly introduce an object
|
// TODO: These paint parameter interpolation should be tunable, possibly introduce an object
|
||||||
// that implements an interface such as Paint getPaint(int step, int strokePoints)
|
// that implements an interface such as Paint getPaint(int step, int strokePoints)
|
||||||
final int size = mInputPointers.getPointerSize();
|
final int size = mXCoordinates.getLength();
|
||||||
int[] xCoords = mInputPointers.getXCoordinates();
|
int[] xCoords = mXCoordinates.getPrimitiveArray();
|
||||||
int[] yCoords = mInputPointers.getYCoordinates();
|
int[] yCoords = mYCoordinates.getPrimitiveArray();
|
||||||
int alpha = Constants.Color.ALPHA_OPAQUE;
|
int alpha = Constants.Color.ALPHA_OPAQUE;
|
||||||
for (int i = size - 1; i > 0 && alpha > 0; i--) {
|
for (int i = size - 1; i > 0 && alpha > 0; i--) {
|
||||||
paint.setAlpha(alpha);
|
paint.setAlpha(alpha);
|
||||||
|
|
|
@ -76,6 +76,27 @@ public class InputPointers {
|
||||||
mTimes.append(src.mTimes, startPos, length);
|
mTimes.append(src.mTimes, startPos, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append the times, x-coordinates and y-coordinates in the specified {@link ResizableIntArray}
|
||||||
|
* to the end of this.
|
||||||
|
* @param pointerId the pointer id of the source.
|
||||||
|
* @param times the source {@link ResizableIntArray} to read the event times from.
|
||||||
|
* @param xCoordinates the source {@link ResizableIntArray} to read the x-coordinates from.
|
||||||
|
* @param yCoordinates the source {@link ResizableIntArray} to read the y-coordinates from.
|
||||||
|
* @param startPos the starting index of the data in {@code times} and etc.
|
||||||
|
* @param length the number of data to be appended.
|
||||||
|
*/
|
||||||
|
public void append(int pointerId, ResizableIntArray times, ResizableIntArray xCoordinates,
|
||||||
|
ResizableIntArray yCoordinates, int startPos, int length) {
|
||||||
|
if (length == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mXCoordinates.append(xCoordinates, startPos, length);
|
||||||
|
mYCoordinates.append(yCoordinates, startPos, length);
|
||||||
|
mPointerIds.fill(pointerId, startPos, length);
|
||||||
|
mTimes.append(times, startPos, length);
|
||||||
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
final int defaultCapacity = mDefaultCapacity;
|
final int defaultCapacity = mDefaultCapacity;
|
||||||
mXCoordinates.reset(defaultCapacity);
|
mXCoordinates.reset(defaultCapacity);
|
||||||
|
|
Loading…
Reference in New Issue