1 /* 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 */ 16 package android.transition; 17 18 import android.view.View; 19 20 /** 21 * Base class for <code>TransitionPropagation</code>s that care about 22 * View Visibility and the center position of the View. 23 */ 24 public abstract class VisibilityPropagation extends TransitionPropagation { 25 26 /** 27 * The property key used for {@link android.view.View#getVisibility()}. 28 */ 29 private static final String PROPNAME_VISIBILITY = "android:visibilityPropagation:visibility"; 30 31 /** 32 * The property key used for the center of the View in screen coordinates. This is an 33 * int[2] with the index 0 taking the x coordinate and index 1 taking the y coordinate. 34 */ 35 private static final String PROPNAME_VIEW_CENTER = "android:visibilityPropagation:center"; 36 37 private static final String[] VISIBILITY_PROPAGATION_VALUES = { 38 PROPNAME_VISIBILITY, 39 PROPNAME_VIEW_CENTER, 40 }; 41 42 @Override captureValues(TransitionValues values)43 public void captureValues(TransitionValues values) { 44 View view = values.view; 45 Integer visibility = (Integer) values.values.get(Visibility.PROPNAME_VISIBILITY); 46 if (visibility == null) { 47 visibility = view.getVisibility(); 48 } 49 values.values.put(PROPNAME_VISIBILITY, visibility); 50 int[] loc = new int[2]; 51 view.getLocationOnScreen(loc); 52 loc[0] += Math.round(view.getTranslationX()); 53 loc[0] += view.getWidth() / 2; 54 loc[1] += Math.round(view.getTranslationY()); 55 loc[1] += view.getHeight() / 2; 56 values.values.put(PROPNAME_VIEW_CENTER, loc); 57 } 58 59 @Override getPropagationProperties()60 public String[] getPropagationProperties() { 61 return VISIBILITY_PROPAGATION_VALUES; 62 } 63 64 /** 65 * Returns {@link android.view.View#getVisibility()} for the View at the time the values 66 * were captured. 67 * @param values The TransitionValues captured at the start or end of the Transition. 68 * @return {@link android.view.View#getVisibility()} for the View at the time the values 69 * were captured. 70 */ getViewVisibility(TransitionValues values)71 public int getViewVisibility(TransitionValues values) { 72 if (values == null) { 73 return View.GONE; 74 } 75 Integer visibility = (Integer) values.values.get(PROPNAME_VISIBILITY); 76 if (visibility == null) { 77 return View.GONE; 78 } 79 return visibility; 80 } 81 82 /** 83 * Returns the View's center x coordinate, relative to the screen, at the time the values 84 * were captured. 85 * @param values The TransitionValues captured at the start or end of the Transition. 86 * @return the View's center x coordinate, relative to the screen, at the time the values 87 * were captured. 88 */ getViewX(TransitionValues values)89 public int getViewX(TransitionValues values) { 90 return getViewCoordinate(values, 0); 91 } 92 93 /** 94 * Returns the View's center y coordinate, relative to the screen, at the time the values 95 * were captured. 96 * @param values The TransitionValues captured at the start or end of the Transition. 97 * @return the View's center y coordinate, relative to the screen, at the time the values 98 * were captured. 99 */ getViewY(TransitionValues values)100 public int getViewY(TransitionValues values) { 101 return getViewCoordinate(values, 1); 102 } 103 getViewCoordinate(TransitionValues values, int coordinateIndex)104 private static int getViewCoordinate(TransitionValues values, int coordinateIndex) { 105 if (values == null) { 106 return -1; 107 } 108 109 int[] coordinates = (int[]) values.values.get(PROPNAME_VIEW_CENTER); 110 if (coordinates == null) { 111 return -1; 112 } 113 114 return coordinates[coordinateIndex]; 115 } 116 } 117