1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/*
* Copyright (C) 2014 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 android.transition;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Path;
import android.util.AttributeSet;
import com.android.internal.R;
/**
* 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.
* <p>
* 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.
* </p>
* <p>This may be used in XML as an element inside a transition.</p>
* <pre>{@code
* <changeBounds>
* <arcMotion android:minimumHorizontalAngle="15"
* android:minimumVerticalAngle="0"
* android:maximumAngle="90"/>
* </changeBounds>}
* </pre>
*/
public class ArcMotion extends PathMotion {
private static final float DEFAULT_MIN_ANGLE_DEGREES = 0;
private static final float DEFAULT_MAX_ANGLE_DEGREES = 70;
private static final float DEFAULT_MAX_TANGENT = (float)
Math.tan(Math.toRadians(DEFAULT_MAX_ANGLE_DEGREES/2));
private float mMinimumHorizontalAngle = 0;
private float mMinimumVerticalAngle = 0;
private float mMaximumAngle = DEFAULT_MAX_ANGLE_DEGREES;
private float mMinimumHorizontalTangent = 0;
private float mMinimumVerticalTangent = 0;
private float mMaximumTangent = DEFAULT_MAX_TANGENT;
public ArcMotion() {}
public ArcMotion(Context context, 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();
}
/**
* 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.
* <p>The default value is 0.</p>
*
* @param angleInDegrees The minimum angle of the arc on a circle describing the Path
* between two nearly horizontally-separated points.
* @attr ref android.R.styleable#ArcMotion_minimumHorizontalAngle
*/
public void setMinimumHorizontalAngle(float angleInDegrees) {
mMinimumHorizontalAngle = angleInDegrees;
mMinimumHorizontalTangent = toTangent(angleInDegrees);
}
/**
* 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.
* <p>The default value is 0.</p>
*
* @return The minimum arc along the circle between two points aligned near horizontally.
* @attr ref android.R.styleable#ArcMotion_minimumHorizontalAngle
*/
public float getMinimumHorizontalAngle() {
return mMinimumHorizontalAngle;
}
/**
* 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.
* <p>The default value is 0.</p>
*
* @param angleInDegrees The minimum angle of the arc on a circle describing the Path
* between two nearly vertically-separated points.
* @attr ref android.R.styleable#ArcMotion_minimumVerticalAngle
*/
public void setMinimumVerticalAngle(float angleInDegrees) {
mMinimumVerticalAngle = angleInDegrees;
mMinimumVerticalTangent = toTangent(angleInDegrees);
}
/**
* 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.
* <p>The default value is 0.</p>
*
* @return The minimum angle of the arc on a circle describing the Path
* between two nearly vertically-separated points.
* @attr ref android.R.styleable#ArcMotion_minimumVerticalAngle
*/
public float getMinimumVerticalAngle() {
return mMinimumVerticalAngle;
}
/**
* 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.
* <p>The default value is 70 degrees.</p>
*
* @param angleInDegrees The maximum angle of the arc on a circle describing the Path
* between the start and end points.
* @attr ref android.R.styleable#ArcMotion_maximumAngle
*/
public void setMaximumAngle(float angleInDegrees) {
mMaximumAngle = angleInDegrees;
mMaximumTangent = toTangent(angleInDegrees);
}
/**
* 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.
* <p>The default value is 70 degrees.</p>
*
* @return The maximum angle of the arc on a circle describing the Path
* between the start and end points.
* @attr ref android.R.styleable#ArcMotion_maximumAngle
*/
public float getMaximumAngle() {
return mMaximumAngle;
}
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));
}
@Override
public 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;
float deltaX = endX - startX;
float deltaY = endY - startY;
// 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;
boolean isMovingUpwards = startY > endY;
if (deltaY == 0) {
ex = dx;
ey = dy + (Math.abs(deltaX) * 0.5f * mMinimumHorizontalTangent);
} else if (deltaX == 0) {
ex = dx + (Math.abs(deltaY) * 0.5f * mMinimumVerticalTangent);
ey = dy;
} else 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 = Math.abs(h2 / (2 * deltaY));
if (isMovingUpwards) {
ey = endY + eDistY;
ex = endX;
} else {
ey = startY + eDistY;
ex = startX;
}
minimumArcDist2 = midDist2 * mMinimumVerticalTangent
* mMinimumVerticalTangent;
} else {
// Same as above, but flip X & Y and account for negative eDist
float eDistX = h2 / (2 * deltaX);
if (isMovingUpwards) {
ex = startX + eDistX;
ey = startY;
} else {
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 != 0 && arcDist2 < minimumArcDist2) {
newArcDistance2 = minimumArcDist2;
} else if (arcDist2 > maximumArcDist2) {
newArcDistance2 = maximumArcDist2;
}
if (newArcDistance2 != 0) {
float ratio2 = newArcDistance2 / arcDist2;
float ratio = (float) Math.sqrt(ratio2);
ex = dx + (ratio * (ex - dx));
ey = dy + (ratio * (ey - dy));
}
float control1X = (startX + ex) / 2;
float control1Y = (startY + ey) / 2;
float control2X = (ex + endX) / 2;
float control2Y = (ey + endY) / 2;
path.cubicTo(control1X, control1Y, control2X, control2Y, endX, endY);
return path;
}
}
|