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