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