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