Android examples for Graphics:Path Point
Samples a stroke temporally into a given number of evenly-distributed points.
/*//from w w w . j a va 2 s. c om * Copyright (C) 2008-2009 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. */ import android.graphics.RectF; import android.util.Log; import java.util.ArrayList; import java.util.Arrays; import java.io.Closeable; import java.io.IOException; import static android.gesture.GestureConstants.*; public class Main{ /** * Samples a stroke temporally into a given number of evenly-distributed * points. * * @param stroke the gesture stroke to be sampled * @param numPoints the number of points * @return the sampled points in the form of [x1, y1, x2, y2, ..., xn, yn] */ public static float[] temporalSampling(GestureStroke stroke, int numPoints) { final float increment = stroke.length / (numPoints - 1); int vectorLength = numPoints * 2; float[] vector = new float[vectorLength]; float distanceSoFar = 0; float[] pts = stroke.points; float lstPointX = pts[0]; float lstPointY = pts[1]; int index = 0; float currentPointX = Float.MIN_VALUE; float currentPointY = Float.MIN_VALUE; vector[index] = lstPointX; index++; vector[index] = lstPointY; index++; int i = 0; int count = pts.length / 2; while (i < count) { if (currentPointX == Float.MIN_VALUE) { i++; if (i >= count) { break; } currentPointX = pts[i * 2]; currentPointY = pts[i * 2 + 1]; } float deltaX = currentPointX - lstPointX; float deltaY = currentPointY - lstPointY; float distance = (float) Math.hypot(deltaX, deltaY); if (distanceSoFar + distance >= increment) { float ratio = (increment - distanceSoFar) / distance; float nx = lstPointX + ratio * deltaX; float ny = lstPointY + ratio * deltaY; vector[index] = nx; index++; vector[index] = ny; index++; lstPointX = nx; lstPointY = ny; distanceSoFar = 0; } else { lstPointX = currentPointX; lstPointY = currentPointY; currentPointX = Float.MIN_VALUE; currentPointY = Float.MIN_VALUE; distanceSoFar += distance; } } for (i = index; i < vectorLength; i += 2) { vector[i] = lstPointX; vector[i + 1] = lstPointY; } return vector; } }