ArcMotionpublic class ArcMotion extends PathMotion A PathMotion that generates a curved path along an arc on an imaginary circle containing
the two points. If the horizontal distance between the points is less than the vertical
distance, then the circle's center point will be horizontally aligned with the end point. If the
vertical distance is less than the horizontal distance then the circle's center point
will be vertically aligned with the end point.
When the two points are near horizontal or vertical, the curve of the motion will be
small as the center of the circle will be far from both points. To force curvature of
the path, {@link #setMinimumHorizontalAngle(float)} and
{@link #setMinimumVerticalAngle(float)} may be used to set the minimum angle of the
arc between two points.
This may be used in XML as an element inside a transition.
{@code
<changeBounds>
<arcMotion android:minimumHorizontalAngle="15"
android:minimumVerticalAngle="0"
android:maximumAngle="90"/>
</changeBounds>}
|
Fields Summary |
---|
private static final float | DEFAULT_MIN_ANGLE_DEGREES | private static final float | DEFAULT_MAX_ANGLE_DEGREES | private static final float | DEFAULT_MAX_TANGENT | private float | mMinimumHorizontalAngle | private float | mMinimumVerticalAngle | private float | mMaximumAngle | private float | mMinimumHorizontalTangent | private float | mMinimumVerticalTangent | private float | mMaximumTangent |
Constructors Summary |
---|
public ArcMotion()
| public ArcMotion(android.content.Context context, android.util.AttributeSet attrs)
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ArcMotion);
float minimumVerticalAngle = a.getFloat(R.styleable.ArcMotion_minimumVerticalAngle,
DEFAULT_MIN_ANGLE_DEGREES);
setMinimumVerticalAngle(minimumVerticalAngle);
float minimumHorizontalAngle = a.getFloat(R.styleable.ArcMotion_minimumHorizontalAngle,
DEFAULT_MIN_ANGLE_DEGREES);
setMinimumHorizontalAngle(minimumHorizontalAngle);
float maximumAngle = a.getFloat(R.styleable.ArcMotion_maximumAngle,
DEFAULT_MAX_ANGLE_DEGREES);
setMaximumAngle(maximumAngle);
a.recycle();
|
Methods Summary |
---|
public float | getMaximumAngle()Returns the maximum arc along the circle between two points. When start and end points
have close to equal x and y differences, the curve between them is large. This forces
the curved path to have an arc of at most the given angle.
The default value is 70 degrees.
return mMaximumAngle;
| public float | getMinimumHorizontalAngle()Returns the minimum arc along the circle between two points aligned near horizontally.
When start and end points are close to horizontal, the calculated center point of the
circle will be far from both points, giving a near straight path between the points.
By setting a minimum angle, this forces the center point to be closer and give an
exaggerated curve to the path.
The default value is 0.
return mMinimumHorizontalAngle;
| public float | getMinimumVerticalAngle()Returns the minimum arc along the circle between two points aligned near vertically.
When start and end points are close to vertical, the calculated center point of the
circle will be far from both points, giving a near straight path between the points.
By setting a minimum angle, this forces the center point to be closer and give an
exaggerated curve to the path.
The default value is 0.
return mMinimumVerticalAngle;
| public android.graphics.Path | getPath(float startX, float startY, float endX, float endY)
// Here's a little ascii art to show how this is calculated:
// c---------- b
// \ / |
// \ d |
// \ / e
// a----f
// This diagram assumes that the horizontal distance is less than the vertical
// distance between The start point (a) and end point (b).
// d is the midpoint between a and b. c is the center point of the circle with
// This path is formed by assuming that start and end points are in
// an arc on a circle. The end point is centered in the circle vertically
// and start is a point on the circle.
// Triangles bfa and bde form similar right triangles. The control points
// for the cubic Bezier arc path are the midpoints between a and e and e and b.
Path path = new Path();
path.moveTo(startX, startY);
float ex;
float ey;
if (startY == endY) {
ex = (startX + endX) / 2;
ey = startY + mMinimumHorizontalTangent * Math.abs(endX - startX) / 2;
} else if (startX == endX) {
ex = startX + mMinimumVerticalTangent * Math.abs(endY - startY) / 2;
ey = (startY + endY) / 2;
} else {
float deltaX = endX - startX;
float deltaY = startY - endY; // Y is inverted compared to diagram above.
// hypotenuse squared.
float h2 = deltaX * deltaX + deltaY * deltaY;
// Midpoint between start and end
float dx = (startX + endX) / 2;
float dy = (startY + endY) / 2;
// Distance squared between end point and mid point is (1/2 hypotenuse)^2
float midDist2 = h2 * 0.25f;
float minimumArcDist2 = 0;
if (Math.abs(deltaX) < Math.abs(deltaY)) {
// Similar triangles bfa and bde mean that (ab/fb = eb/bd)
// Therefore, eb = ab * bd / fb
// ab = hypotenuse
// bd = hypotenuse/2
// fb = deltaY
float eDistY = h2 / (2 * deltaY);
ey = endY + eDistY;
ex = endX;
minimumArcDist2 = midDist2 * mMinimumVerticalTangent
* mMinimumVerticalTangent;
} else {
// Same as above, but flip X & Y
float eDistX = h2 / (2 * deltaX);
ex = endX + eDistX;
ey = endY;
minimumArcDist2 = midDist2 * mMinimumHorizontalTangent
* mMinimumHorizontalTangent;
}
float arcDistX = dx - ex;
float arcDistY = dy - ey;
float arcDist2 = arcDistX * arcDistX + arcDistY * arcDistY;
float maximumArcDist2 = midDist2 * mMaximumTangent * mMaximumTangent;
float newArcDistance2 = 0;
if (arcDist2 < minimumArcDist2) {
newArcDistance2 = minimumArcDist2;
} else if (arcDist2 > maximumArcDist2) {
newArcDistance2 = maximumArcDist2;
}
if (newArcDistance2 != 0) {
float ratio2 = newArcDistance2 / arcDist2;
float ratio = FloatMath.sqrt(ratio2);
ex = dx + (ratio * (ex - dx));
ey = dy + (ratio * (ey - dy));
}
}
float controlX1 = (startX + ex) / 2;
float controlY1 = (startY + ey) / 2;
float controlX2 = (ex + endX) / 2;
float controlY2 = (ey + endY) / 2;
path.cubicTo(controlX1, controlY1, controlX2, controlY2, endX, endY);
return path;
| public void | setMaximumAngle(float angleInDegrees)Sets the maximum arc along the circle between two points. When start and end points
have close to equal x and y differences, the curve between them is large. This forces
the curved path to have an arc of at most the given angle.
The default value is 70 degrees.
mMaximumAngle = angleInDegrees;
mMaximumTangent = toTangent(angleInDegrees);
| public void | setMinimumHorizontalAngle(float angleInDegrees)Sets the minimum arc along the circle between two points aligned near horizontally.
When start and end points are close to horizontal, the calculated center point of the
circle will be far from both points, giving a near straight path between the points.
By setting a minimum angle, this forces the center point to be closer and give an
exaggerated curve to the path.
The default value is 0.
mMinimumHorizontalAngle = angleInDegrees;
mMinimumHorizontalTangent = toTangent(angleInDegrees);
| public void | setMinimumVerticalAngle(float angleInDegrees)Sets the minimum arc along the circle between two points aligned near vertically.
When start and end points are close to vertical, the calculated center point of the
circle will be far from both points, giving a near straight path between the points.
By setting a minimum angle, this forces the center point to be closer and give an
exaggerated curve to the path.
The default value is 0.
mMinimumVerticalAngle = angleInDegrees;
mMinimumVerticalTangent = toTangent(angleInDegrees);
| private static float | toTangent(float arcInDegrees)
if (arcInDegrees < 0 || arcInDegrees > 90) {
throw new IllegalArgumentException("Arc must be between 0 and 90 degrees");
}
return (float) Math.tan(Math.toRadians(arcInDegrees / 2));
|
|