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 
     17 package android.transition;
     18 
     19 import android.animation.Animator;
     20 import android.animation.ObjectAnimator;
     21 import android.content.Context;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 
     26 /**
     27  * This transition captures the scroll properties of targets before and after
     28  * the scene change and animates any changes.
     29  */
     30 public class ChangeScroll extends Transition {
     31 
     32     private static final String PROPNAME_SCROLL_X = "android:changeScroll:x";
     33     private static final String PROPNAME_SCROLL_Y = "android:changeScroll:y";
     34 
     35     private static final String[] PROPERTIES = {
     36             PROPNAME_SCROLL_X,
     37             PROPNAME_SCROLL_Y,
     38     };
     39 
     40     public ChangeScroll() {}
     41 
     42     public ChangeScroll(Context context, AttributeSet attrs) {
     43         super(context, attrs);
     44     }
     45 
     46     @Override
     47     public void captureStartValues(TransitionValues transitionValues) {
     48         captureValues(transitionValues);
     49     }
     50 
     51     @Override
     52     public void captureEndValues(TransitionValues transitionValues) {
     53         captureValues(transitionValues);
     54     }
     55 
     56     @Override
     57     public String[] getTransitionProperties() {
     58         return PROPERTIES;
     59     }
     60 
     61     private void captureValues(TransitionValues transitionValues) {
     62         transitionValues.values.put(PROPNAME_SCROLL_X, transitionValues.view.getScrollX());
     63         transitionValues.values.put(PROPNAME_SCROLL_Y, transitionValues.view.getScrollY());
     64     }
     65 
     66     @Override
     67     public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
     68             TransitionValues endValues) {
     69         if (startValues == null || endValues == null) {
     70             return null;
     71         }
     72         final View view = endValues.view;
     73         int startX = (Integer) startValues.values.get(PROPNAME_SCROLL_X);
     74         int endX = (Integer) endValues.values.get(PROPNAME_SCROLL_X);
     75         int startY = (Integer) startValues.values.get(PROPNAME_SCROLL_Y);
     76         int endY = (Integer) endValues.values.get(PROPNAME_SCROLL_Y);
     77         Animator scrollXAnimator = null;
     78         Animator scrollYAnimator = null;
     79         if (startX != endX) {
     80             view.setScrollX(startX);
     81             scrollXAnimator = ObjectAnimator.ofInt(view, "scrollX", startX, endX);
     82         }
     83         if (startY != endY) {
     84             view.setScrollY(startY);
     85             scrollYAnimator = ObjectAnimator.ofInt(view, "scrollY", startY, endY);
     86         }
     87         return TransitionUtils.mergeAnimators(scrollXAnimator, scrollYAnimator);
     88     }
     89 }
     90