blob: 817541cb91aa67ee56207d0d0b642513a2af6c90 [file] [log] [blame]
George Mountd6107a32014-03-10 16:51:16 -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
18import android.graphics.Rect;
George Mount7764b922016-01-13 14:51:33 -080019import android.transition.Slide.GravityFlag;
George Mountdc21d3b2014-06-05 09:42:48 -070020import android.view.Gravity;
George Mountd6107a32014-03-10 16:51:16 -070021import android.view.View;
22import android.view.ViewGroup;
23
24/**
25 * A <code>TransitionPropagation</code> that propagates based on the distance to the side
26 * and, orthogonally, the distance to epicenter. If the transitioning View is visible in
27 * the start of the transition, then it will transition sooner when closer to the side and
28 * later when farther. If the view is not visible in the start of the transition, then
29 * it will transition later when closer to the side and sooner when farther from the edge.
30 * This is the default TransitionPropagation used with {@link android.transition.Slide}.
31 */
32public class SidePropagation extends VisibilityPropagation {
33 private static final String TAG = "SlidePropagation";
34
George Mount265f2092014-04-30 16:24:53 -070035 private float mPropagationSpeed = 3.0f;
George Mountdc21d3b2014-06-05 09:42:48 -070036 private int mSide = Gravity.BOTTOM;
George Mountd6107a32014-03-10 16:51:16 -070037
38 /**
39 * Sets the side that is used to calculate the transition propagation. If the transitioning
40 * View is visible in the start of the transition, then it will transition sooner when
41 * closer to the side and later when farther. If the view is not visible in the start of
42 * the transition, then it will transition later when closer to the side and sooner when
George Mountdc21d3b2014-06-05 09:42:48 -070043 * farther from the edge. The default is {@link Gravity#BOTTOM}.
George Mountd6107a32014-03-10 16:51:16 -070044 *
45 * @param side The side that is used to calculate the transition propagation. Must be one of
George Mount2db3bf52014-11-19 14:05:57 -080046 * {@link Gravity#LEFT}, {@link Gravity#TOP}, {@link Gravity#RIGHT},
47 * {@link Gravity#BOTTOM}, {@link Gravity#START}, or {@link Gravity#END}.
George Mountd6107a32014-03-10 16:51:16 -070048 */
George Mount7764b922016-01-13 14:51:33 -080049 public void setSide(@GravityFlag int side) {
George Mountd6107a32014-03-10 16:51:16 -070050 mSide = side;
51 }
52
53 /**
54 * Sets the speed at which transition propagation happens, relative to the duration of the
55 * Transition. A <code>propagationSpeed</code> of 1 means that a View centered at the side
56 * set in {@link #setSide(int)} and View centered at the opposite edge will have a difference
57 * in start delay of approximately the duration of the Transition. A speed of 2 means the
58 * start delay difference will be approximately half of the duration of the transition. A
59 * value of 0 is illegal, but negative values will invert the propagation.
60 *
61 * @param propagationSpeed The speed at which propagation occurs, relative to the duration
62 * of the transition. A speed of 4 means it works 4 times as fast
63 * as the duration of the transition. May not be 0.
64 */
65 public void setPropagationSpeed(float propagationSpeed) {
66 if (propagationSpeed == 0) {
67 throw new IllegalArgumentException("propagationSpeed may not be 0");
68 }
69 mPropagationSpeed = propagationSpeed;
70 }
71
72 @Override
73 public long getStartDelay(ViewGroup sceneRoot, Transition transition,
74 TransitionValues startValues, TransitionValues endValues) {
75 if (startValues == null && endValues == null) {
76 return 0;
77 }
78 int directionMultiplier = 1;
79 Rect epicenter = transition.getEpicenter();
80 TransitionValues positionValues;
81 if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) {
82 positionValues = startValues;
83 directionMultiplier = -1;
84 } else {
85 positionValues = endValues;
86 }
87
88 int viewCenterX = getViewX(positionValues);
89 int viewCenterY = getViewY(positionValues);
90
91 int[] loc = new int[2];
92 sceneRoot.getLocationOnScreen(loc);
93 int left = loc[0] + Math.round(sceneRoot.getTranslationX());
94 int top = loc[1] + Math.round(sceneRoot.getTranslationY());
95 int right = left + sceneRoot.getWidth();
96 int bottom = top + sceneRoot.getHeight();
97
98 int epicenterX;
99 int epicenterY;
100 if (epicenter != null) {
101 epicenterX = epicenter.centerX();
102 epicenterY = epicenter.centerY();
103 } else {
104 epicenterX = (left + right) / 2;
105 epicenterY = (top + bottom) / 2;
106 }
107
George Mount2db3bf52014-11-19 14:05:57 -0800108 float distance = distance(sceneRoot, viewCenterX, viewCenterY, epicenterX, epicenterY,
George Mountd6107a32014-03-10 16:51:16 -0700109 left, top, right, bottom);
110 float maxDistance = getMaxDistance(sceneRoot);
111 float distanceFraction = distance/maxDistance;
112
George Mount265f2092014-04-30 16:24:53 -0700113 long duration = transition.getDuration();
114 if (duration < 0) {
115 duration = 300;
116 }
117
118 return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
George Mountd6107a32014-03-10 16:51:16 -0700119 }
120
George Mount2db3bf52014-11-19 14:05:57 -0800121 private int distance(View sceneRoot, int viewX, int viewY, int epicenterX, int epicenterY,
George Mountd6107a32014-03-10 16:51:16 -0700122 int left, int top, int right, int bottom) {
George Mount2db3bf52014-11-19 14:05:57 -0800123 final int side;
124 if (mSide == Gravity.START) {
125 final boolean isRtl = sceneRoot.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
126 side = isRtl ? Gravity.RIGHT : Gravity.LEFT;
127 } else if (mSide == Gravity.END) {
128 final boolean isRtl = sceneRoot.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
129 side = isRtl ? Gravity.LEFT : Gravity.RIGHT;
130 } else {
131 side = mSide;
132 }
George Mountd6107a32014-03-10 16:51:16 -0700133 int distance = 0;
George Mount2db3bf52014-11-19 14:05:57 -0800134 switch (side) {
George Mountdc21d3b2014-06-05 09:42:48 -0700135 case Gravity.LEFT:
George Mountd6107a32014-03-10 16:51:16 -0700136 distance = right - viewX + Math.abs(epicenterY - viewY);
137 break;
George Mountdc21d3b2014-06-05 09:42:48 -0700138 case Gravity.TOP:
George Mountd6107a32014-03-10 16:51:16 -0700139 distance = bottom - viewY + Math.abs(epicenterX - viewX);
140 break;
George Mountdc21d3b2014-06-05 09:42:48 -0700141 case Gravity.RIGHT:
George Mountd6107a32014-03-10 16:51:16 -0700142 distance = viewX - left + Math.abs(epicenterY - viewY);
143 break;
George Mountdc21d3b2014-06-05 09:42:48 -0700144 case Gravity.BOTTOM:
George Mountd6107a32014-03-10 16:51:16 -0700145 distance = viewY - top + Math.abs(epicenterX - viewX);
146 break;
147 }
148 return distance;
149 }
150
151 private int getMaxDistance(ViewGroup sceneRoot) {
152 switch (mSide) {
George Mountdc21d3b2014-06-05 09:42:48 -0700153 case Gravity.LEFT:
154 case Gravity.RIGHT:
George Mount2db3bf52014-11-19 14:05:57 -0800155 case Gravity.START:
156 case Gravity.END:
George Mountd6107a32014-03-10 16:51:16 -0700157 return sceneRoot.getWidth();
158 default:
159 return sceneRoot.getHeight();
160 }
161 }
162}