Alan Viverette | 3da604b | 2020-06-10 18:34:39 +0000 | [diff] [blame^] | 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.view.animation; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.graphics.Rect; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.util.DisplayMetrics; |
| 24 | |
| 25 | /** |
| 26 | * An animation that controls the clip of an object. See the |
| 27 | * {@link android.view.animation full package} description for details and |
| 28 | * sample code. |
| 29 | * |
| 30 | * @hide |
| 31 | */ |
| 32 | public class ClipRectAnimation extends Animation { |
| 33 | protected final Rect mFromRect = new Rect(); |
| 34 | protected final Rect mToRect = new Rect(); |
| 35 | |
| 36 | private int mFromLeftType = ABSOLUTE; |
| 37 | private int mFromTopType = ABSOLUTE; |
| 38 | private int mFromRightType = ABSOLUTE; |
| 39 | private int mFromBottomType = ABSOLUTE; |
| 40 | |
| 41 | private int mToLeftType = ABSOLUTE; |
| 42 | private int mToTopType = ABSOLUTE; |
| 43 | private int mToRightType = ABSOLUTE; |
| 44 | private int mToBottomType = ABSOLUTE; |
| 45 | |
| 46 | private float mFromLeftValue; |
| 47 | private float mFromTopValue; |
| 48 | private float mFromRightValue; |
| 49 | private float mFromBottomValue; |
| 50 | |
| 51 | private float mToLeftValue; |
| 52 | private float mToTopValue; |
| 53 | private float mToRightValue; |
| 54 | private float mToBottomValue; |
| 55 | |
| 56 | /** |
| 57 | * Constructor used when a ClipRectAnimation is loaded from a resource. |
| 58 | * |
| 59 | * @param context Application context to use |
| 60 | * @param attrs Attribute set from which to read values |
| 61 | */ |
| 62 | public ClipRectAnimation(Context context, AttributeSet attrs) { |
| 63 | super(context, attrs); |
| 64 | |
| 65 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 66 | com.android.internal.R.styleable.ClipRectAnimation); |
| 67 | |
| 68 | Description d = Description.parseValue(a.peekValue( |
| 69 | com.android.internal.R.styleable.ClipRectAnimation_fromLeft)); |
| 70 | mFromLeftType = d.type; |
| 71 | mFromLeftValue = d.value; |
| 72 | |
| 73 | d = Description.parseValue(a.peekValue( |
| 74 | com.android.internal.R.styleable.ClipRectAnimation_fromTop)); |
| 75 | mFromTopType = d.type; |
| 76 | mFromTopValue = d.value; |
| 77 | |
| 78 | d = Description.parseValue(a.peekValue( |
| 79 | com.android.internal.R.styleable.ClipRectAnimation_fromRight)); |
| 80 | mFromRightType = d.type; |
| 81 | mFromRightValue = d.value; |
| 82 | |
| 83 | d = Description.parseValue(a.peekValue( |
| 84 | com.android.internal.R.styleable.ClipRectAnimation_fromBottom)); |
| 85 | mFromBottomType = d.type; |
| 86 | mFromBottomValue = d.value; |
| 87 | |
| 88 | |
| 89 | d = Description.parseValue(a.peekValue( |
| 90 | com.android.internal.R.styleable.ClipRectAnimation_toLeft)); |
| 91 | mToLeftType = d.type; |
| 92 | mToLeftValue = d.value; |
| 93 | |
| 94 | d = Description.parseValue(a.peekValue( |
| 95 | com.android.internal.R.styleable.ClipRectAnimation_toTop)); |
| 96 | mToTopType = d.type; |
| 97 | mToTopValue = d.value; |
| 98 | |
| 99 | d = Description.parseValue(a.peekValue( |
| 100 | com.android.internal.R.styleable.ClipRectAnimation_toRight)); |
| 101 | mToRightType = d.type; |
| 102 | mToRightValue = d.value; |
| 103 | |
| 104 | d = Description.parseValue(a.peekValue( |
| 105 | com.android.internal.R.styleable.ClipRectAnimation_toBottom)); |
| 106 | mToBottomType = d.type; |
| 107 | mToBottomValue = d.value; |
| 108 | |
| 109 | a.recycle(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Constructor to use when building a ClipRectAnimation from code |
| 114 | * |
| 115 | * @param fromClip the clip rect to animate from |
| 116 | * @param toClip the clip rect to animate to |
| 117 | */ |
| 118 | public ClipRectAnimation(Rect fromClip, Rect toClip) { |
| 119 | if (fromClip == null || toClip == null) { |
| 120 | throw new RuntimeException("Expected non-null animation clip rects"); |
| 121 | } |
| 122 | mFromLeftValue = fromClip.left; |
| 123 | mFromTopValue = fromClip.top; |
| 124 | mFromRightValue= fromClip.right; |
| 125 | mFromBottomValue = fromClip.bottom; |
| 126 | |
| 127 | mToLeftValue = toClip.left; |
| 128 | mToTopValue = toClip.top; |
| 129 | mToRightValue= toClip.right; |
| 130 | mToBottomValue = toClip.bottom; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Constructor to use when building a ClipRectAnimation from code |
| 135 | */ |
| 136 | public ClipRectAnimation(int fromL, int fromT, int fromR, int fromB, |
| 137 | int toL, int toT, int toR, int toB) { |
| 138 | this(new Rect(fromL, fromT, fromR, fromB), new Rect(toL, toT, toR, toB)); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | protected void applyTransformation(float it, Transformation tr) { |
| 143 | int l = mFromRect.left + (int) ((mToRect.left - mFromRect.left) * it); |
| 144 | int t = mFromRect.top + (int) ((mToRect.top - mFromRect.top) * it); |
| 145 | int r = mFromRect.right + (int) ((mToRect.right - mFromRect.right) * it); |
| 146 | int b = mFromRect.bottom + (int) ((mToRect.bottom - mFromRect.bottom) * it); |
| 147 | tr.setClipRect(l, t, r, b); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean willChangeTransformationMatrix() { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void initialize(int width, int height, int parentWidth, int parentHeight) { |
| 157 | super.initialize(width, height, parentWidth, parentHeight); |
| 158 | mFromRect.set((int) resolveSize(mFromLeftType, mFromLeftValue, width, parentWidth), |
| 159 | (int) resolveSize(mFromTopType, mFromTopValue, height, parentHeight), |
| 160 | (int) resolveSize(mFromRightType, mFromRightValue, width, parentWidth), |
| 161 | (int) resolveSize(mFromBottomType, mFromBottomValue, height, parentHeight)); |
| 162 | mToRect.set((int) resolveSize(mToLeftType, mToLeftValue, width, parentWidth), |
| 163 | (int) resolveSize(mToTopType, mToTopValue, height, parentHeight), |
| 164 | (int) resolveSize(mToRightType, mToRightValue, width, parentWidth), |
| 165 | (int) resolveSize(mToBottomType, mToBottomValue, height, parentHeight)); |
| 166 | } |
| 167 | } |