blob: 7a2c191be0ddcfc86bc52878c85b13815e8cec64 [file] [log] [blame]
George Mountecd857b2014-06-19 07:51:08 -07001/*
2 * Copyright (C) 2014 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 */
16package android.transition;
17
George Mountecd857b2014-06-19 07:51:08 -070018import android.content.Context;
19import android.content.res.TypedArray;
20import android.graphics.Matrix;
21import android.graphics.Path;
22import android.graphics.PathMeasure;
23import android.util.AttributeSet;
George Mountecd857b2014-06-19 07:51:08 -070024import android.util.PathParser;
25
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070026import com.android.internal.R;
27
George Mountecd857b2014-06-19 07:51:08 -070028/**
29 * A PathMotion that takes a Path pattern and applies it to the separation between two points.
30 * The starting point of the Path will be moved to the origin and the end point will be scaled
31 * and rotated so that it matches with the target end point.
32 * <p>This may be used in XML as an element inside a transition.</p>
Neil Fuller71fbb812015-11-30 09:51:33 +000033 * <pre>{@code
34 * <changeBounds>
35 * <patternPathMotion android:patternPathData="M0 0 L0 100 L100 100"/>
36 * </changeBounds>}
George Mountecd857b2014-06-19 07:51:08 -070037 * </pre>
38 */
George Mountf9557612014-08-29 11:32:13 -070039public class PatternPathMotion extends PathMotion {
George Mountecd857b2014-06-19 07:51:08 -070040
George Mountf9557612014-08-29 11:32:13 -070041 private Path mOriginalPatternPath;
George Mountecd857b2014-06-19 07:51:08 -070042
George Mountf9557612014-08-29 11:32:13 -070043 private final Path mPatternPath = new Path();
George Mountecd857b2014-06-19 07:51:08 -070044
45 private final Matrix mTempMatrix = new Matrix();
46
47 /**
George Mountf9557612014-08-29 11:32:13 -070048 * Constructs a PatternPathMotion with a straight-line pattern.
George Mountecd857b2014-06-19 07:51:08 -070049 */
George Mountf9557612014-08-29 11:32:13 -070050 public PatternPathMotion() {
51 mPatternPath.lineTo(1, 0);
52 mOriginalPatternPath = mPatternPath;
George Mountecd857b2014-06-19 07:51:08 -070053 }
54
George Mountf9557612014-08-29 11:32:13 -070055 public PatternPathMotion(Context context, AttributeSet attrs) {
56 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PatternPathMotion);
George Mountecd857b2014-06-19 07:51:08 -070057 try {
George Mountf9557612014-08-29 11:32:13 -070058 String pathData = a.getString(R.styleable.PatternPathMotion_patternPathData);
George Mountecd857b2014-06-19 07:51:08 -070059 if (pathData == null) {
George Mountf9557612014-08-29 11:32:13 -070060 throw new RuntimeException("pathData must be supplied for patternPathMotion");
George Mountecd857b2014-06-19 07:51:08 -070061 }
62 Path pattern = PathParser.createPathFromPathData(pathData);
George Mountf9557612014-08-29 11:32:13 -070063 setPatternPath(pattern);
George Mountecd857b2014-06-19 07:51:08 -070064 } finally {
65 a.recycle();
66 }
67
68 }
69
70 /**
George Mountf9557612014-08-29 11:32:13 -070071 * Creates a PatternPathMotion with the Path defining a pattern of motion between two
72 * coordinates. The pattern will be translated, rotated, and scaled to fit between the start
73 * and end points. The pattern must not be empty and must have the end point differ from the
74 * start point.
George Mountecd857b2014-06-19 07:51:08 -070075 *
George Mountf9557612014-08-29 11:32:13 -070076 * @param patternPath A Path to be used as a pattern for two-dimensional motion.
George Mountecd857b2014-06-19 07:51:08 -070077 */
George Mountf9557612014-08-29 11:32:13 -070078 public PatternPathMotion(Path patternPath) {
79 setPatternPath(patternPath);
George Mountecd857b2014-06-19 07:51:08 -070080 }
81
82 /**
83 * Returns the Path defining a pattern of motion between two coordinates.
84 * The pattern will be translated, rotated, and scaled to fit between the start and end points.
85 * The pattern must not be empty and must have the end point differ from the start point.
86 *
87 * @return the Path defining a pattern of motion between two coordinates.
George Mountf9557612014-08-29 11:32:13 -070088 * @attr ref android.R.styleable#PatternPathMotion_patternPathData
George Mountecd857b2014-06-19 07:51:08 -070089 */
George Mountf9557612014-08-29 11:32:13 -070090 public Path getPatternPath() {
91 return mOriginalPatternPath;
George Mountecd857b2014-06-19 07:51:08 -070092 }
93
94 /**
95 * Sets the Path defining a pattern of motion between two coordinates.
96 * The pattern will be translated, rotated, and scaled to fit between the start and end points.
97 * The pattern must not be empty and must have the end point differ from the start point.
98 *
George Mountf9557612014-08-29 11:32:13 -070099 * @param patternPath A Path to be used as a pattern for two-dimensional motion.
100 * @attr ref android.R.styleable#PatternPathMotion_patternPathData
George Mountecd857b2014-06-19 07:51:08 -0700101 */
George Mountf9557612014-08-29 11:32:13 -0700102 public void setPatternPath(Path patternPath) {
103 PathMeasure pathMeasure = new PathMeasure(patternPath, false);
George Mountecd857b2014-06-19 07:51:08 -0700104 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;
Neil Fullere573aa92015-02-11 15:49:47 +0000120 float distance = (float) Math.hypot(dx, dy);
George Mountecd857b2014-06-19 07:51:08 -0700121 float scale = 1 / distance;
122 mTempMatrix.postScale(scale, scale);
123 double angle = Math.atan2(dy, dx);
124 mTempMatrix.postRotate((float) Math.toDegrees(-angle));
George Mountf9557612014-08-29 11:32:13 -0700125 patternPath.transform(mTempMatrix, mPatternPath);
126 mOriginalPatternPath = patternPath;
George Mountecd857b2014-06-19 07:51:08 -0700127 }
128
129 @Override
130 public Path getPath(float startX, float startY, float endX, float endY) {
Neil Fullere573aa92015-02-11 15:49:47 +0000131 double dx = endX - startX;
132 double dy = endY - startY;
133 float length = (float) Math.hypot(dx, dy);
George Mountecd857b2014-06-19 07:51:08 -0700134 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();
George Mountf9557612014-08-29 11:32:13 -0700140 mPatternPath.transform(mTempMatrix, path);
George Mountecd857b2014-06-19 07:51:08 -0700141 return path;
142 }
143
George Mountecd857b2014-06-19 07:51:08 -0700144}