blob: 2ac6e725e3866cdddda5b68b58bc8b65bfc80ba7 [file] [log] [blame]
Justin Klaassen10d07c82017-09-15 17:58:39 -04001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.transition;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.graphics.Matrix;
22import android.graphics.Path;
23import android.graphics.PathMeasure;
24import android.support.v4.content.res.TypedArrayUtils;
25import android.support.v4.graphics.PathParser;
26import android.util.AttributeSet;
27
28import org.xmlpull.v1.XmlPullParser;
29
30/**
31 * A PathMotion that takes a Path pattern and applies it to the separation between two points.
32 * The starting point of the Path will be moved to the origin and the end point will be scaled
33 * and rotated so that it matches with the target end point.
34 * <p>This may be used in XML as an element inside a transition.</p>
35 * <pre>{@code
36 * <changeBounds>
37 * <patternPathMotion android:patternPathData="M0 0 L0 100 L100 100"/>
38 * </changeBounds>}
39 * </pre>
40 */
41public class PatternPathMotion extends PathMotion {
42
43 private Path mOriginalPatternPath;
44
45 private final Path mPatternPath = new Path();
46
47 private final Matrix mTempMatrix = new Matrix();
48
49 /**
50 * Constructs a PatternPathMotion with a straight-line pattern.
51 */
52 public PatternPathMotion() {
53 mPatternPath.lineTo(1, 0);
54 mOriginalPatternPath = mPatternPath;
55 }
56
57 public PatternPathMotion(Context context, AttributeSet attrs) {
58 TypedArray a = context.obtainStyledAttributes(attrs, Styleable.PATTERN_PATH_MOTION);
59 try {
60 String pathData = TypedArrayUtils.getNamedString(a, (XmlPullParser) attrs,
61 "patternPathData", Styleable.PatternPathMotion.PATTERN_PATH_DATA);
62 if (pathData == null) {
63 throw new RuntimeException("pathData must be supplied for patternPathMotion");
64 }
65 Path pattern = PathParser.createPathFromPathData(pathData);
66 setPatternPath(pattern);
67 } finally {
68 a.recycle();
69 }
70 }
71
72 /**
73 * Creates a PatternPathMotion with the Path defining a pattern of motion between two
74 * coordinates. The pattern will be translated, rotated, and scaled to fit between the start
75 * and end points. The pattern must not be empty and must have the end point differ from the
76 * start point.
77 *
78 * @param patternPath A Path to be used as a pattern for two-dimensional motion.
79 */
80 public PatternPathMotion(Path patternPath) {
81 setPatternPath(patternPath);
82 }
83
84 /**
85 * Returns the Path defining a pattern of motion between two coordinates.
86 * The pattern will be translated, rotated, and scaled to fit between the start and end points.
87 * The pattern must not be empty and must have the end point differ from the start point.
88 *
89 * @return the Path defining a pattern of motion between two coordinates.
90 */
91 public Path getPatternPath() {
92 return mOriginalPatternPath;
93 }
94
95 /**
96 * Sets the Path defining a pattern of motion between two coordinates.
97 * The pattern will be translated, rotated, and scaled to fit between the start and end points.
98 * The pattern must not be empty and must have the end point differ from the start point.
99 *
100 * @param patternPath A Path to be used as a pattern for two-dimensional motion.
101 */
102 public void setPatternPath(Path patternPath) {
103 PathMeasure pathMeasure = new PathMeasure(patternPath, false);
104 float length = pathMeasure.getLength();
105 float[] pos = new float[2];
106 pathMeasure.getPosTan(length, pos, null);
107 float endX = pos[0];
108 float endY = pos[1];
109 pathMeasure.getPosTan(0, pos, null);
110 float startX = pos[0];
111 float startY = pos[1];
112
113 if (startX == endX && startY == endY) {
114 throw new IllegalArgumentException("pattern must not end at the starting point");
115 }
116
117 mTempMatrix.setTranslate(-startX, -startY);
118 float dx = endX - startX;
119 float dy = endY - startY;
120 float distance = distance(dx, dy);
121 float scale = 1 / distance;
122 mTempMatrix.postScale(scale, scale);
123 double angle = Math.atan2(dy, dx);
124 mTempMatrix.postRotate((float) Math.toDegrees(-angle));
125 patternPath.transform(mTempMatrix, mPatternPath);
126 mOriginalPatternPath = patternPath;
127 }
128
129 @Override
130 public Path getPath(float startX, float startY, float endX, float endY) {
131 float dx = endX - startX;
132 float dy = endY - startY;
133 float length = distance(dx, dy);
134 double angle = Math.atan2(dy, dx);
135
136 mTempMatrix.setScale(length, length);
137 mTempMatrix.postRotate((float) Math.toDegrees(angle));
138 mTempMatrix.postTranslate(startX, startY);
139 Path path = new Path();
140 mPatternPath.transform(mTempMatrix, path);
141 return path;
142 }
143
144 private static float distance(float x, float y) {
145 return (float) Math.sqrt((x * x) + (y * y));
146 }
147
148}