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