Make RoundedLine member variables as method parameters (Step 5)

Change-Id: Ib2ade2bf51c293c65fc9206a9a16694e6d18da50
main
Tadashi G. Takaoka 2012-11-29 15:14:44 +09:00
parent f90475b5d4
commit e14df775d4
2 changed files with 75 additions and 78 deletions

View File

@ -19,7 +19,6 @@ import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Path; import android.graphics.Path;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.RectF;
import android.os.SystemClock; import android.os.SystemClock;
import com.android.inputmethod.latin.Constants; import com.android.inputmethod.latin.Constants;
@ -153,34 +152,34 @@ final class GesturePreviewTrail {
paint.setColor(params.mTrailColor); paint.setColor(params.mTrailColor);
paint.setStyle(Paint.Style.FILL); paint.setStyle(Paint.Style.FILL);
final RoundedLine line = mRoundedLine; final RoundedLine line = mRoundedLine;
line.p1x = getXCoordValue(xCoords[startIndex]); int p1x = getXCoordValue(xCoords[startIndex]);
line.p1y = yCoords[startIndex]; int p1y = yCoords[startIndex];
int lastTime = sinceDown - eventTimes[startIndex]; int lastTime = sinceDown - eventTimes[startIndex];
float maxWidth = getWidth(lastTime, params); float maxWidth = getWidth(lastTime, params);
line.r1 = maxWidth / 2.0f; float r1 = maxWidth / 2.0f;
// Initialize bounds rectangle. // Initialize bounds rectangle.
outBoundsRect.set((int)line.p1x, (int)line.p1y, (int)line.p1x, (int)line.p1y); outBoundsRect.set(p1x, p1y, p1x, p1y);
for (int i = startIndex + 1; i < trailSize - 1; i++) { for (int i = startIndex + 1; i < trailSize - 1; i++) {
final int elapsedTime = sinceDown - eventTimes[i]; final int elapsedTime = sinceDown - eventTimes[i];
line.p2x = getXCoordValue(xCoords[i]); final int p2x = getXCoordValue(xCoords[i]);
line.p2y = yCoords[i]; final int p2y = yCoords[i];
final float width = getWidth(elapsedTime, params);
final float r2 = width / 2.0f;
// 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(xCoords[i])) { if (!isDownEventXCoord(xCoords[i])) {
final int alpha = getAlpha(elapsedTime, params); final int alpha = getAlpha(elapsedTime, params);
paint.setAlpha(alpha); paint.setAlpha(alpha);
final float width = getWidth(elapsedTime, params); final Path path = line.makePath(p1x, p1y, r1, p2x, p2y, r2);
line.r2 = width / 2.0f;
final Path path = line.makePath();
if (path != null) { if (path != null) {
canvas.drawPath(path, paint); canvas.drawPath(path, paint);
outBoundsRect.union((int)line.p2x, (int)line.p2y); outBoundsRect.union(p2x, p2y);
} }
// Take union for the bounds. // Take union for the bounds.
maxWidth = Math.max(maxWidth, width); maxWidth = Math.max(maxWidth, width);
} }
line.p1x = line.p2x; p1x = p2x;
line.p1y = line.p2y; p1y = p2y;
line.r1 = line.r2; r1 = r2;
lastTime = elapsedTime; lastTime = elapsedTime;
} }
// Take care of trail line width. // Take care of trail line width.

View File

@ -18,88 +18,86 @@ import android.graphics.Path;
import android.graphics.RectF; import android.graphics.RectF;
public final class RoundedLine { public final class RoundedLine {
// Start point (P1) coordinates and trail radius. private final RectF mArc1 = new RectF();
public float p1x, p1y; private final RectF mArc2 = new RectF();
public float r1; private final Path mPath = new Path();
// End point (P2) coordinates and trail radius.
public float p2x, p2y;
public float r2;
// Closing point of arc at P1. private static final double RADIAN_TO_DEGREE = 180.0d / Math.PI;
private float p1ax, p1ay; private static final double RIGHT_ANGLE = Math.PI / 2.0d;
// Opening point of arc at P1.
private float p1bx, p1by;
// Opening point of arc at P2.
private float p2ax, p2ay;
// Closing point of arc at P2.
private float p2bx, p2by;
// Start angle of the trail arcs.
private float angle;
// Sweep angle of the trail arc at P1.
private float a1;
private final RectF arc1 = new RectF();
// Sweep angle of the trail arc at P2.
private float a2;
private final RectF arc2 = new RectF();
private final Path path = new Path();
private static final float RADIAN_TO_DEGREE = (float)(180.0d / Math.PI); /**
private static final float RIGHT_ANGLE = (float)(Math.PI / 2.0d); * Make a rounded line path
*
public Path makePath() { * @param p1x the x-coordinate of the start point.
final float dx = p2x - p1x; * @param p1y the y-coordinate of the start point.
final float dy = p2y - p1y; * @param r1 the radius at the start point
* @param p2x the x-coordinate of the end point.
* @param p2y the y-coordinate of the end point.
* @param r2 the radius at the end point
* @return the path of rounded line
*/
public Path makePath(final float p1x, final float p1y, final float r1,
final float p2x, final float p2y, final float r2) {
final double dx = p2x - p1x;
final double dy = p2y - p1y;
// Distance of the points. // Distance of the points.
final double l = Math.hypot(dx, dy); final double l = Math.hypot(dx, dy);
if (Double.compare(0.0d, l) == 0) { if (Double.compare(0.0d, l) == 0) {
return null; return null;
} }
// Angle of the line p1-p2 // Angle of the line p1-p2
final float a = (float)Math.atan2(dy, dx); final double a = Math.atan2(dy, dx);
// Difference of trail cap radius. // Difference of trail cap radius.
final float dr = r2 - r1; final double dr = r2 - r1;
// Variation of angle at trail cap. // Variation of angle at trail cap.
final float ar = (float)Math.asin(dr / l); final double ar = Math.asin(dr / l);
// The start angle of trail cap arc at P1. // The start angle of trail cap arc at P1.
final float aa = a - (RIGHT_ANGLE + ar); final double aa = a - (RIGHT_ANGLE + ar);
// The end angle of trail cap arc at P2. // The end angle of trail cap arc at P2.
final float ab = a + (RIGHT_ANGLE + ar); final double ab = a + (RIGHT_ANGLE + ar);
final float cosa = (float)Math.cos(aa); final float cosa = (float)Math.cos(aa);
final float sina = (float)Math.sin(aa); final float sina = (float)Math.sin(aa);
final float cosb = (float)Math.cos(ab); final float cosb = (float)Math.cos(ab);
final float sinb = (float)Math.sin(ab); final float sinb = (float)Math.sin(ab);
p1ax = p1x + r1 * cosa; // Closing point of arc at P1.
p1ay = p1y + r1 * sina; final float p1ax = p1x + r1 * cosa;
p1bx = p1x + r1 * cosb; final float p1ay = p1y + r1 * sina;
p1by = p1y + r1 * sinb; // Opening point of arc at P1.
p2ax = p2x + r2 * cosa; final float p1bx = p1x + r1 * cosb;
p2ay = p2y + r2 * sina; final float p1by = p1y + r1 * sinb;
p2bx = p2x + r2 * cosb; // Opening point of arc at P2.
p2by = p2y + r2 * sinb; final float p2ax = p2x + r2 * cosa;
angle = aa * RADIAN_TO_DEGREE; final float p2ay = p2y + r2 * sina;
final float ar2degree = ar * 2.0f * RADIAN_TO_DEGREE; // Closing point of arc at P2.
a1 = -180.0f + ar2degree; final float p2bx = p2x + r2 * cosb;
a2 = 180.0f + ar2degree; final float p2by = p2y + r2 * sinb;
arc1.set(p1x, p1y, p1x, p1y); // Start angle of the trail arcs.
arc1.inset(-r1, -r1); final float angle = (float)(aa * RADIAN_TO_DEGREE);
arc2.set(p2x, p2y, p2x, p2y); final float ar2degree = (float)(ar * 2.0d * RADIAN_TO_DEGREE);
arc2.inset(-r2, -r2); // Sweep angle of the trail arc at P1.
final float a1 = -180.0f + ar2degree;
// Sweep angle of the trail arc at P2.
final float a2 = 180.0f + ar2degree;
mArc1.set(p1x, p1y, p1x, p1y);
mArc1.inset(-r1, -r1);
mArc2.set(p2x, p2y, p2x, p2y);
mArc2.inset(-r2, -r2);
path.rewind(); mPath.rewind();
// Trail cap at P1. // Trail cap at P1.
path.moveTo(p1x, p1y); mPath.moveTo(p1x, p1y);
path.arcTo(arc1, angle, a1); mPath.arcTo(mArc1, angle, a1);
// Trail cap at P2. // Trail cap at P2.
path.moveTo(p2x, p2y); mPath.moveTo(p2x, p2y);
path.arcTo(arc2, angle, a2); mPath.arcTo(mArc2, angle, a2);
// Two trapezoids connecting P1 and P2. // Two trapezoids connecting P1 and P2.
path.moveTo(p1ax, p1ay); mPath.moveTo(p1ax, p1ay);
path.lineTo(p1x, p1y); mPath.lineTo(p1x, p1y);
path.lineTo(p1bx, p1by); mPath.lineTo(p1bx, p1by);
path.lineTo(p2bx, p2by); mPath.lineTo(p2bx, p2by);
path.lineTo(p2x, p2y); mPath.lineTo(p2x, p2y);
path.lineTo(p2ax, p2ay); mPath.lineTo(p2ax, p2ay);
path.close(); mPath.close();
return path; return mPath;
} }
} }