Home | History | Annotate | Download | only in transition
      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.animation.Animator;
     19 import android.animation.ObjectAnimator;
     20 import android.animation.RectEvaluator;
     21 import android.content.Context;
     22 import android.graphics.Rect;
     23 import android.util.AttributeSet;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 
     27 /**
     28  * ChangeClipBounds captures the {@link android.view.View#getClipBounds()} before and after the
     29  * scene change and animates those changes during the transition.
     30  */
     31 public class ChangeClipBounds extends Transition {
     32 
     33     private static final String TAG = "ChangeTransform";
     34 
     35     private static final String PROPNAME_CLIP = "android:clipBounds:clip";
     36     private static final String PROPNAME_BOUNDS = "android:clipBounds:bounds";
     37 
     38     private static final String[] sTransitionProperties = {
     39             PROPNAME_CLIP,
     40     };
     41 
     42     public ChangeClipBounds() {}
     43 
     44     public ChangeClipBounds(Context context, AttributeSet attrs) {
     45         super(context, attrs);
     46     }
     47 
     48     @Override
     49     public String[] getTransitionProperties() {
     50         return sTransitionProperties;
     51     }
     52 
     53     private void captureValues(TransitionValues values) {
     54         View view = values.view;
     55         if (view.getVisibility() == View.GONE) {
     56             return;
     57         }
     58 
     59         Rect clip = view.getClipBounds();
     60         values.values.put(PROPNAME_CLIP, clip);
     61         if (clip == null) {
     62             Rect bounds = new Rect(0, 0, view.getWidth(), view.getHeight());
     63             values.values.put(PROPNAME_BOUNDS, bounds);
     64         }
     65     }
     66 
     67     @Override
     68     public void captureStartValues(TransitionValues transitionValues) {
     69         captureValues(transitionValues);
     70     }
     71 
     72     @Override
     73     public void captureEndValues(TransitionValues transitionValues) {
     74         captureValues(transitionValues);
     75     }
     76 
     77     @Override
     78     public Animator createAnimator(final ViewGroup sceneRoot, TransitionValues startValues,
     79             TransitionValues endValues) {
     80         if (startValues == null || endValues == null
     81                 || !startValues.values.containsKey(PROPNAME_CLIP)
     82                 || !endValues.values.containsKey(PROPNAME_CLIP)) {
     83             return null;
     84         }
     85         Rect start = (Rect) startValues.values.get(PROPNAME_CLIP);
     86         Rect end = (Rect) endValues.values.get(PROPNAME_CLIP);
     87         if (start == null && end == null) {
     88             return null; // No animation required since there is no clip.
     89         }
     90 
     91         if (start == null) {
     92             start = (Rect) startValues.values.get(PROPNAME_BOUNDS);
     93         } else if (end == null) {
     94             end = (Rect) endValues.values.get(PROPNAME_BOUNDS);
     95         }
     96         if (start.equals(end)) {
     97             return null;
     98         }
     99 
    100         endValues.view.setClipBounds(start);
    101         RectEvaluator evaluator = new RectEvaluator(new Rect());
    102         return ObjectAnimator.ofObject(endValues.view, "clipBounds", evaluator, start, end);
    103     }
    104 }
    105