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.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.content.Context; 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 37 private static final String[] PROPERTIES = { 38 PROPNAME_SCROLL_X, 39 PROPNAME_SCROLL_Y, 40 }; 41 ChangeScroll()42 public ChangeScroll() {} 43 ChangeScroll(Context context, AttributeSet attrs)44 public ChangeScroll(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 } 47 48 @Override captureStartValues(TransitionValues transitionValues)49 public void captureStartValues(TransitionValues transitionValues) { 50 captureValues(transitionValues); 51 } 52 53 @Override captureEndValues(TransitionValues transitionValues)54 public void captureEndValues(TransitionValues transitionValues) { 55 captureValues(transitionValues); 56 } 57 58 @Override getTransitionProperties()59 public String[] getTransitionProperties() { 60 return PROPERTIES; 61 } 62 captureValues(TransitionValues transitionValues)63 private void captureValues(TransitionValues transitionValues) { 64 transitionValues.values.put(PROPNAME_SCROLL_X, transitionValues.view.getScrollX()); 65 transitionValues.values.put(PROPNAME_SCROLL_Y, transitionValues.view.getScrollY()); 66 } 67 68 @Nullable 69 @Override createAnimator(@onNull ViewGroup sceneRoot, @Nullable TransitionValues startValues, @Nullable TransitionValues endValues)70 public Animator createAnimator(@NonNull ViewGroup sceneRoot, 71 @Nullable TransitionValues startValues, 72 @Nullable TransitionValues endValues) { 73 if (startValues == null || endValues == null) { 74 return null; 75 } 76 final View view = endValues.view; 77 int startX = (Integer) startValues.values.get(PROPNAME_SCROLL_X); 78 int endX = (Integer) endValues.values.get(PROPNAME_SCROLL_X); 79 int startY = (Integer) startValues.values.get(PROPNAME_SCROLL_Y); 80 int endY = (Integer) endValues.values.get(PROPNAME_SCROLL_Y); 81 Animator scrollXAnimator = null; 82 Animator scrollYAnimator = null; 83 if (startX != endX) { 84 view.setScrollX(startX); 85 scrollXAnimator = ObjectAnimator.ofInt(view, "scrollX", startX, endX); 86 } 87 if (startY != endY) { 88 view.setScrollY(startY); 89 scrollYAnimator = ObjectAnimator.ofInt(view, "scrollY", startY, endY); 90 } 91 return TransitionUtils.mergeAnimators(scrollXAnimator, scrollYAnimator); 92 } 93 } 94