ChangeClipBounds.java revision b31c3281d870e9abb673db239234d580dcc4feff
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.AnimatorListenerAdapter; 21import android.animation.ObjectAnimator; 22import android.content.Context; 23import android.graphics.Rect; 24import androidx.annotation.NonNull; 25import androidx.core.view.ViewCompat; 26import android.util.AttributeSet; 27import android.view.View; 28import android.view.ViewGroup; 29 30/** 31 * ChangeClipBounds captures the {@link android.view.View#getClipBounds()} before and after the 32 * scene change and animates those changes during the transition. 33 * 34 * <p>Prior to API 18 this does nothing.</p> 35 */ 36public class ChangeClipBounds extends Transition { 37 38 private static final String PROPNAME_CLIP = "android:clipBounds:clip"; 39 private static final String PROPNAME_BOUNDS = "android:clipBounds:bounds"; 40 41 private static final String[] sTransitionProperties = { 42 PROPNAME_CLIP, 43 }; 44 45 @Override 46 public String[] getTransitionProperties() { 47 return sTransitionProperties; 48 } 49 50 public ChangeClipBounds() { 51 } 52 53 public ChangeClipBounds(Context context, AttributeSet attrs) { 54 super(context, attrs); 55 } 56 57 private void captureValues(TransitionValues values) { 58 View view = values.view; 59 if (view.getVisibility() == View.GONE) { 60 return; 61 } 62 63 Rect clip = ViewCompat.getClipBounds(view); 64 values.values.put(PROPNAME_CLIP, clip); 65 if (clip == null) { 66 Rect bounds = new Rect(0, 0, view.getWidth(), view.getHeight()); 67 values.values.put(PROPNAME_BOUNDS, bounds); 68 } 69 } 70 71 @Override 72 public void captureStartValues(@NonNull TransitionValues transitionValues) { 73 captureValues(transitionValues); 74 } 75 76 @Override 77 public void captureEndValues(@NonNull TransitionValues transitionValues) { 78 captureValues(transitionValues); 79 } 80 81 @Override 82 public Animator createAnimator(@NonNull final ViewGroup sceneRoot, TransitionValues startValues, 83 TransitionValues endValues) { 84 if (startValues == null || endValues == null 85 || !startValues.values.containsKey(PROPNAME_CLIP) 86 || !endValues.values.containsKey(PROPNAME_CLIP)) { 87 return null; 88 } 89 Rect start = (Rect) startValues.values.get(PROPNAME_CLIP); 90 Rect end = (Rect) endValues.values.get(PROPNAME_CLIP); 91 final boolean endIsNull = end == null; 92 if (start == null && end == null) { 93 return null; // No animation required since there is no clip. 94 } 95 96 if (start == null) { 97 start = (Rect) startValues.values.get(PROPNAME_BOUNDS); 98 } else if (end == null) { 99 end = (Rect) endValues.values.get(PROPNAME_BOUNDS); 100 } 101 if (start.equals(end)) { 102 return null; 103 } 104 105 ViewCompat.setClipBounds(endValues.view, start); 106 RectEvaluator evaluator = new RectEvaluator(new Rect()); 107 ObjectAnimator animator = ObjectAnimator.ofObject(endValues.view, ViewUtils.CLIP_BOUNDS, 108 evaluator, start, end); 109 if (endIsNull) { 110 final View endView = endValues.view; 111 animator.addListener(new AnimatorListenerAdapter() { 112 @Override 113 public void onAnimationEnd(Animator animation) { 114 ViewCompat.setClipBounds(endView, null); 115 } 116 }); 117 } 118 return animator; 119 } 120} 121