blob: 0a7adf63719745919caa63a9739b5fd48146989b [file] [log] [blame]
Eric Erfanian2ca43182017-08-31 06:57:16 -07001/*
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
sail3bcea982017-09-03 13:57:22 -070017package com.android.bubble;
Eric Erfanian2ca43182017-08-31 06:57:16 -070018
19import android.animation.Animator;
20import android.animation.AnimatorSet;
21import android.animation.ObjectAnimator;
22import android.graphics.Path;
23import android.graphics.PointF;
24import android.graphics.Rect;
25import android.support.annotation.VisibleForTesting;
26import android.transition.Transition;
27import android.transition.TransitionValues;
28import android.util.Property;
29import android.view.View;
30import android.view.ViewGroup;
31
32/** Similar to {@link android.transition.ChangeBounds ChangeBounds} but works across windows */
33public class ChangeOnScreenBounds extends Transition {
34
35 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
36 static final String PROPNAME_BOUNDS = "bubble:changeScreenBounds:bounds";
37
38 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
39 static final String PROPNAME_SCREEN_X = "bubble:changeScreenBounds:screenX";
40
41 @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
42 static final String PROPNAME_SCREEN_Y = "bubble:changeScreenBounds:screenY";
43
44 static final String PROPNAME_WIDTH = "bubble:changeScreenBounds:width";
45 static final String PROPNAME_HEIGHT = "bubble:changeScreenBounds:height";
46
47 private static final Property<ViewBounds, PointF> TOP_LEFT_PROPERTY =
48 new Property<ViewBounds, PointF>(PointF.class, "topLeft") {
49 @Override
50 public void set(ViewBounds viewBounds, PointF topLeft) {
51 viewBounds.setTopLeft(topLeft);
52 }
53
54 @Override
55 public PointF get(ViewBounds viewBounds) {
56 return null;
57 }
58 };
59
60 private static final Property<ViewBounds, PointF> BOTTOM_RIGHT_PROPERTY =
61 new Property<ViewBounds, PointF>(PointF.class, "bottomRight") {
62 @Override
63 public void set(ViewBounds viewBounds, PointF bottomRight) {
64 viewBounds.setBottomRight(bottomRight);
65 }
66
67 @Override
68 public PointF get(ViewBounds viewBounds) {
69 return null;
70 }
71 };
72 private final int[] tempLocation = new int[2];
73
74 @Override
75 public void captureStartValues(TransitionValues transitionValues) {
76 captureValuesWithSize(transitionValues);
77 }
78
79 @Override
80 public void captureEndValues(TransitionValues transitionValues) {
81 captureValuesWithSize(transitionValues);
82 }
83
84 /**
85 * Capture location (left and top) from {@code values.view} and size (width and height) from
86 * {@code values.values}. If size is not set, use the size of {@code values.view}.
87 */
88 private void captureValuesWithSize(TransitionValues values) {
89 View view = values.view;
90
91 if (view.isLaidOut() || view.getWidth() != 0 || view.getHeight() != 0) {
92 Integer width = (Integer) values.values.get(PROPNAME_WIDTH);
93 Integer height = (Integer) values.values.get(PROPNAME_HEIGHT);
94
95 values.values.put(
96 PROPNAME_BOUNDS,
97 new Rect(
98 view.getLeft(),
99 view.getTop(),
100 width == null ? view.getRight() : view.getLeft() + width,
101 height == null ? view.getBottom() : view.getTop() + height));
102 values.view.getLocationOnScreen(tempLocation);
103 values.values.put(PROPNAME_SCREEN_X, tempLocation[0]);
104 values.values.put(PROPNAME_SCREEN_Y, tempLocation[1]);
105 }
106 }
107
108 @Override
109 public Animator createAnimator(
110 ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
111 Rect startBounds = (Rect) startValues.values.get(PROPNAME_BOUNDS);
112 Rect endBounds = (Rect) endValues.values.get(PROPNAME_BOUNDS);
113
114 if (startBounds == null || endBounds == null) {
115 // start or end values were not captured, so don't animate.
116 return null;
117 }
118
119 // Offset the startBounds by the difference in screen position
120 int startScreenX = (Integer) startValues.values.get(PROPNAME_SCREEN_X);
121 int startScreenY = (Integer) startValues.values.get(PROPNAME_SCREEN_Y);
122 int endScreenX = (Integer) endValues.values.get(PROPNAME_SCREEN_X);
123 int endScreenY = (Integer) endValues.values.get(PROPNAME_SCREEN_Y);
124 startBounds.offset(startScreenX - endScreenX, startScreenY - endScreenY);
125
126 final int startLeft = startBounds.left;
127 final int endLeft = endBounds.left;
128 final int startTop = startBounds.top;
129 final int endTop = endBounds.top;
130 final int startRight = startBounds.right;
131 final int endRight = endBounds.right;
132 final int startBottom = startBounds.bottom;
133 final int endBottom = endBounds.bottom;
134 ViewBounds viewBounds = new ViewBounds(endValues.view);
135 viewBounds.setTopLeft(new PointF(startLeft, startTop));
136 viewBounds.setBottomRight(new PointF(startRight, startBottom));
137
138 // Animate the top left and bottom right corners along a path
139 Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop);
140 ObjectAnimator topLeftAnimator =
141 ObjectAnimator.ofObject(viewBounds, TOP_LEFT_PROPERTY, null, topLeftPath);
142
143 Path bottomRightPath = getPathMotion().getPath(startRight, startBottom, endRight, endBottom);
144 ObjectAnimator bottomRightAnimator =
145 ObjectAnimator.ofObject(viewBounds, BOTTOM_RIGHT_PROPERTY, null, bottomRightPath);
146 AnimatorSet set = new AnimatorSet();
147 set.playTogether(topLeftAnimator, bottomRightAnimator);
148 return set;
149 }
150
151 private static class ViewBounds {
152 private int left;
153 private int top;
154 private int right;
155 private int bottom;
156 private final View view;
157 private int topLeftCalls;
158 private int bottomRightCalls;
159
160 public ViewBounds(View view) {
161 this.view = view;
162 }
163
164 public void setTopLeft(PointF topLeft) {
165 left = Math.round(topLeft.x);
166 top = Math.round(topLeft.y);
167 topLeftCalls++;
168 if (topLeftCalls == bottomRightCalls) {
169 updateLeftTopRightBottom();
170 }
171 }
172
173 public void setBottomRight(PointF bottomRight) {
174 right = Math.round(bottomRight.x);
175 bottom = Math.round(bottomRight.y);
176 bottomRightCalls++;
177 if (topLeftCalls == bottomRightCalls) {
178 updateLeftTopRightBottom();
179 }
180 }
181
182 private void updateLeftTopRightBottom() {
183 view.setLeft(left);
184 view.setTop(top);
185 view.setRight(right);
186 view.setBottom(bottom);
187 topLeftCalls = 0;
188 bottomRightCalls = 0;
189 }
190 }
191}