ChangeScroll.java revision ae61b85cb595ad157000279e77475900a3f44e05
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 17package android.transition; 18 19import android.animation.Animator; 20import android.animation.ObjectAnimator; 21import android.content.Context; 22import android.transition.Transition; 23import android.transition.TransitionValues; 24import android.util.AttributeSet; 25import android.view.View; 26import 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 */ 32public 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 42 public ChangeScroll() {} 43 44 public ChangeScroll(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 } 47 48 @Override 49 public void captureStartValues(TransitionValues transitionValues) { 50 captureValues(transitionValues); 51 } 52 53 @Override 54 public void captureEndValues(TransitionValues transitionValues) { 55 captureValues(transitionValues); 56 } 57 58 @Override 59 public String[] getTransitionProperties() { 60 return PROPERTIES; 61 } 62 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 @Override 69 public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, 70 TransitionValues endValues) { 71 if (startValues == null || endValues == null) { 72 return null; 73 } 74 final View view = endValues.view; 75 int startX = (Integer) startValues.values.get(PROPNAME_SCROLL_X); 76 int endX = (Integer) endValues.values.get(PROPNAME_SCROLL_X); 77 int startY = (Integer) startValues.values.get(PROPNAME_SCROLL_Y); 78 int endY = (Integer) endValues.values.get(PROPNAME_SCROLL_Y); 79 Animator scrollXAnimator = null; 80 Animator scrollYAnimator = null; 81 if (startX != endX) { 82 view.setScrollX(startX); 83 scrollXAnimator = ObjectAnimator.ofInt(view, "scrollX", startX, endX); 84 } 85 if (startY != endY) { 86 view.setScrollY(startY); 87 scrollYAnimator = ObjectAnimator.ofInt(view, "scrollY", startY, endY); 88 } 89 return TransitionUtils.mergeAnimators(scrollXAnimator, scrollYAnimator); 90 } 91} 92