Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 1 | /* Copyright (C) 2010 The Android Open Source Project |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 2 | * |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | package android.widget; |
| 17 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 18 | import android.animation.ObjectAnimator; |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 19 | import android.animation.PropertyValuesHolder; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 21 | import android.content.res.TypedArray; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 23 | import android.graphics.BlurMaskFilter; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.Matrix; |
| 26 | import android.graphics.Paint; |
| 27 | import android.graphics.PorterDuff; |
| 28 | import android.graphics.PorterDuffXfermode; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 29 | import android.graphics.Rect; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 30 | import android.graphics.RectF; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 31 | import android.graphics.TableMaskFilter; |
Svetoslav Ganov | 48d1586 | 2012-05-15 10:10:00 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 33 | import android.util.AttributeSet; |
| 34 | import android.util.Log; |
Adam Cohen | a8a7c92 | 2011-02-28 17:20:44 -0800 | [diff] [blame] | 35 | import android.view.InputDevice; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 36 | import android.view.MotionEvent; |
| 37 | import android.view.VelocityTracker; |
| 38 | import android.view.View; |
| 39 | import android.view.ViewConfiguration; |
| 40 | import android.view.ViewGroup; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 41 | import android.view.accessibility.AccessibilityNodeInfo; |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 42 | import android.view.animation.LinearInterpolator; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 43 | import android.widget.RemoteViews.RemoteView; |
| 44 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 45 | import java.lang.ref.WeakReference; |
| 46 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 47 | @RemoteView |
| 48 | /** |
| 49 | * A view that displays its children in a stack and allows users to discretely swipe |
| 50 | * through the children. |
| 51 | */ |
| 52 | public class StackView extends AdapterViewAnimator { |
| 53 | private final String TAG = "StackView"; |
| 54 | |
| 55 | /** |
| 56 | * Default animation parameters |
| 57 | */ |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 58 | private static final int DEFAULT_ANIMATION_DURATION = 400; |
| 59 | private static final int MINIMUM_ANIMATION_DURATION = 50; |
Adam Cohen | c0b53be | 2010-12-17 19:23:41 -0800 | [diff] [blame] | 60 | private static final int STACK_RELAYOUT_DURATION = 100; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 63 | * Parameters effecting the perspective visuals |
| 64 | */ |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 65 | private static final float PERSPECTIVE_SHIFT_FACTOR_Y = 0.1f; |
| 66 | private static final float PERSPECTIVE_SHIFT_FACTOR_X = 0.1f; |
| 67 | |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 68 | private float mPerspectiveShiftX; |
| 69 | private float mPerspectiveShiftY; |
| 70 | private float mNewPerspectiveShiftX; |
| 71 | private float mNewPerspectiveShiftY; |
| 72 | |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 73 | @SuppressWarnings({"FieldCanBeLocal"}) |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 74 | private static final float PERSPECTIVE_SCALE_FACTOR = 0f; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Represent the two possible stack modes, one where items slide up, and the other |
| 78 | * where items slide down. The perspective is also inverted between these two modes. |
| 79 | */ |
| 80 | private static final int ITEMS_SLIDE_UP = 0; |
| 81 | private static final int ITEMS_SLIDE_DOWN = 1; |
| 82 | |
| 83 | /** |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 84 | * These specify the different gesture states |
| 85 | */ |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 86 | private static final int GESTURE_NONE = 0; |
| 87 | private static final int GESTURE_SLIDE_UP = 1; |
| 88 | private static final int GESTURE_SLIDE_DOWN = 2; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 89 | |
| 90 | /** |
| 91 | * Specifies how far you need to swipe (up or down) before it |
| 92 | * will be consider a completed gesture when you lift your finger |
| 93 | */ |
Adam Cohen | a9238c8 | 2010-10-25 14:01:29 -0700 | [diff] [blame] | 94 | private static final float SWIPE_THRESHOLD_RATIO = 0.2f; |
| 95 | |
| 96 | /** |
| 97 | * Specifies the total distance, relative to the size of the stack, |
| 98 | * that views will be slid, either up or down |
| 99 | */ |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 100 | private static final float SLIDE_UP_RATIO = 0.7f; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 101 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Sentinel value for no current active pointer. |
| 104 | * Used by {@link #mActivePointerId}. |
| 105 | */ |
| 106 | private static final int INVALID_POINTER = -1; |
| 107 | |
| 108 | /** |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 109 | * Number of active views in the stack. One fewer view is actually visible, as one is hidden. |
| 110 | */ |
| 111 | private static final int NUM_ACTIVE_VIEWS = 5; |
| 112 | |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 113 | private static final int FRAME_PADDING = 4; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 114 | |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 115 | private final Rect mTouchRect = new Rect(); |
| 116 | |
Adam Cohen | 26e30bb | 2010-12-03 18:16:12 -0800 | [diff] [blame] | 117 | private static final int MIN_TIME_BETWEEN_INTERACTION_AND_AUTOADVANCE = 5000; |
| 118 | |
Romain Guy | 4725a90 | 2011-05-25 10:54:29 -0700 | [diff] [blame] | 119 | private static final long MIN_TIME_BETWEEN_SCROLLS = 100; |
Adam Cohen | a8a7c92 | 2011-02-28 17:20:44 -0800 | [diff] [blame] | 120 | |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 121 | /** |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 122 | * These variables are all related to the current state of touch interaction |
| 123 | * with the stack |
| 124 | */ |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 125 | private float mInitialY; |
| 126 | private float mInitialX; |
| 127 | private int mActivePointerId; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 128 | private int mYVelocity = 0; |
| 129 | private int mSwipeGestureType = GESTURE_NONE; |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 130 | private int mSlideAmount; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 131 | private int mSwipeThreshold; |
| 132 | private int mTouchSlop; |
| 133 | private int mMaximumVelocity; |
| 134 | private VelocityTracker mVelocityTracker; |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 135 | private boolean mTransitionIsSetup = false; |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 136 | private int mResOutColor; |
| 137 | private int mClickColor; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 138 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 139 | private static HolographicHelper sHolographicHelper; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 140 | private ImageView mHighlight; |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 141 | private ImageView mClickFeedback; |
| 142 | private boolean mClickFeedbackIsValid = false; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 143 | private StackSlider mStackSlider; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 144 | private boolean mFirstLayoutHappened = false; |
Adam Cohen | 26e30bb | 2010-12-03 18:16:12 -0800 | [diff] [blame] | 145 | private long mLastInteractionTime = 0; |
Adam Cohen | a8a7c92 | 2011-02-28 17:20:44 -0800 | [diff] [blame] | 146 | private long mLastScrollTime; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 147 | private int mStackMode; |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 148 | private int mFramePadding; |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 149 | private final Rect stackInvalidateRect = new Rect(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 150 | |
Winson Chung | abc8b50a | 2011-09-06 18:38:50 -0700 | [diff] [blame] | 151 | /** |
| 152 | * {@inheritDoc} |
| 153 | */ |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 154 | public StackView(Context context) { |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 155 | this(context, null); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Winson Chung | abc8b50a | 2011-09-06 18:38:50 -0700 | [diff] [blame] | 158 | /** |
| 159 | * {@inheritDoc} |
| 160 | */ |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 161 | public StackView(Context context, AttributeSet attrs) { |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 162 | this(context, attrs, com.android.internal.R.attr.stackViewStyle); |
| 163 | } |
| 164 | |
Winson Chung | abc8b50a | 2011-09-06 18:38:50 -0700 | [diff] [blame] | 165 | /** |
| 166 | * {@inheritDoc} |
| 167 | */ |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 168 | public StackView(Context context, AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 169 | this(context, attrs, defStyleAttr, 0); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * {@inheritDoc} |
| 174 | */ |
| 175 | public StackView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 176 | super(context, attrs, defStyleAttr, defStyleRes); |
| 177 | final TypedArray a = context.obtainStyledAttributes( |
| 178 | attrs, com.android.internal.R.styleable.StackView, defStyleAttr, defStyleRes); |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 179 | |
| 180 | mResOutColor = a.getColor( |
| 181 | com.android.internal.R.styleable.StackView_resOutColor, 0); |
| 182 | mClickColor = a.getColor( |
| 183 | com.android.internal.R.styleable.StackView_clickColor, 0); |
| 184 | |
| 185 | a.recycle(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 186 | initStackView(); |
| 187 | } |
| 188 | |
| 189 | private void initStackView() { |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 190 | configureViewAnimator(NUM_ACTIVE_VIEWS, 1); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 191 | setStaticTransformationsEnabled(true); |
| 192 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 193 | mTouchSlop = configuration.getScaledTouchSlop(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 194 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
| 195 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 196 | |
| 197 | mHighlight = new ImageView(getContext()); |
| 198 | mHighlight.setLayoutParams(new LayoutParams(mHighlight)); |
| 199 | addViewInLayout(mHighlight, -1, new LayoutParams(mHighlight)); |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 200 | |
| 201 | mClickFeedback = new ImageView(getContext()); |
| 202 | mClickFeedback.setLayoutParams(new LayoutParams(mClickFeedback)); |
| 203 | addViewInLayout(mClickFeedback, -1, new LayoutParams(mClickFeedback)); |
| 204 | mClickFeedback.setVisibility(INVISIBLE); |
| 205 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 206 | mStackSlider = new StackSlider(); |
| 207 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 208 | if (sHolographicHelper == null) { |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 209 | sHolographicHelper = new HolographicHelper(mContext); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 210 | } |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 211 | setClipChildren(false); |
| 212 | setClipToPadding(false); |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 213 | |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 214 | // This sets the form of the StackView, which is currently to have the perspective-shifted |
| 215 | // views above the active view, and have items slide down when sliding out. The opposite is |
| 216 | // available by using ITEMS_SLIDE_UP. |
| 217 | mStackMode = ITEMS_SLIDE_DOWN; |
| 218 | |
Adam Cohen | 1480fdd | 2010-08-25 17:24:53 -0700 | [diff] [blame] | 219 | // This is a flag to indicate the the stack is loading for the first time |
| 220 | mWhichChild = -1; |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 221 | |
| 222 | // Adjust the frame padding based on the density, since the highlight changes based |
| 223 | // on the density |
| 224 | final float density = mContext.getResources().getDisplayMetrics().density; |
| 225 | mFramePadding = (int) Math.ceil(density * FRAME_PADDING); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Animate the views between different relative indexes within the {@link AdapterViewAnimator} |
| 230 | */ |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 231 | void transformViewForTransition(int fromIndex, int toIndex, final View view, boolean animate) { |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 232 | if (!animate) { |
| 233 | ((StackFrame) view).cancelSliderAnimator(); |
| 234 | view.setRotationX(0f); |
| 235 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 236 | lp.setVerticalOffset(0); |
| 237 | lp.setHorizontalOffset(0); |
Adam Cohen | 69d66e0 | 2011-01-12 14:39:13 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Adam Cohen | 5020458 | 2011-01-16 17:28:25 -0800 | [diff] [blame] | 240 | if (fromIndex == -1 && toIndex == getNumActiveViews() -1) { |
Adam Cohen | 5020458 | 2011-01-16 17:28:25 -0800 | [diff] [blame] | 241 | transformViewAtIndex(toIndex, view, false); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 242 | view.setVisibility(VISIBLE); |
Adam Cohen | c798b66 | 2011-10-21 12:07:38 -0700 | [diff] [blame] | 243 | view.setAlpha(1.0f); |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 244 | } else if (fromIndex == 0 && toIndex == 1) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 245 | // Slide item in |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 246 | ((StackFrame) view).cancelSliderAnimator(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 247 | view.setVisibility(VISIBLE); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 248 | |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 249 | int duration = Math.round(mStackSlider.getDurationForNeutralPosition(mYVelocity)); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 250 | StackSlider animationSlider = new StackSlider(mStackSlider); |
Adam Cohen | 69d66e0 | 2011-01-12 14:39:13 -0800 | [diff] [blame] | 251 | animationSlider.setView(view); |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 252 | |
| 253 | if (animate) { |
| 254 | PropertyValuesHolder slideInY = PropertyValuesHolder.ofFloat("YProgress", 0.0f); |
| 255 | PropertyValuesHolder slideInX = PropertyValuesHolder.ofFloat("XProgress", 0.0f); |
| 256 | ObjectAnimator slideIn = ObjectAnimator.ofPropertyValuesHolder(animationSlider, |
| 257 | slideInX, slideInY); |
| 258 | slideIn.setDuration(duration); |
| 259 | slideIn.setInterpolator(new LinearInterpolator()); |
| 260 | ((StackFrame) view).setSliderAnimator(slideIn); |
| 261 | slideIn.start(); |
| 262 | } else { |
| 263 | animationSlider.setYProgress(0f); |
| 264 | animationSlider.setXProgress(0f); |
| 265 | } |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 266 | } else if (fromIndex == 1 && toIndex == 0) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 267 | // Slide item out |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 268 | ((StackFrame) view).cancelSliderAnimator(); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 269 | int duration = Math.round(mStackSlider.getDurationForOffscreenPosition(mYVelocity)); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 270 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 271 | StackSlider animationSlider = new StackSlider(mStackSlider); |
Adam Cohen | 69d66e0 | 2011-01-12 14:39:13 -0800 | [diff] [blame] | 272 | animationSlider.setView(view); |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 273 | if (animate) { |
| 274 | PropertyValuesHolder slideOutY = PropertyValuesHolder.ofFloat("YProgress", 1.0f); |
| 275 | PropertyValuesHolder slideOutX = PropertyValuesHolder.ofFloat("XProgress", 0.0f); |
| 276 | ObjectAnimator slideOut = ObjectAnimator.ofPropertyValuesHolder(animationSlider, |
| 277 | slideOutX, slideOutY); |
| 278 | slideOut.setDuration(duration); |
| 279 | slideOut.setInterpolator(new LinearInterpolator()); |
| 280 | ((StackFrame) view).setSliderAnimator(slideOut); |
| 281 | slideOut.start(); |
| 282 | } else { |
| 283 | animationSlider.setYProgress(1.0f); |
| 284 | animationSlider.setXProgress(0f); |
| 285 | } |
Adam Cohen | 69d66e0 | 2011-01-12 14:39:13 -0800 | [diff] [blame] | 286 | } else if (toIndex == 0) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 287 | // Make sure this view that is "waiting in the wings" is invisible |
| 288 | view.setAlpha(0.0f); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 289 | view.setVisibility(INVISIBLE); |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 290 | } else if ((fromIndex == 0 || fromIndex == 1) && toIndex > 1) { |
Adam Cohen | 69d66e0 | 2011-01-12 14:39:13 -0800 | [diff] [blame] | 291 | view.setVisibility(VISIBLE); |
| 292 | view.setAlpha(1.0f); |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 293 | view.setRotationX(0f); |
| 294 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 295 | lp.setVerticalOffset(0); |
| 296 | lp.setHorizontalOffset(0); |
Adam Cohen | c0b53be | 2010-12-17 19:23:41 -0800 | [diff] [blame] | 297 | } else if (fromIndex == -1) { |
| 298 | view.setAlpha(1.0f); |
| 299 | view.setVisibility(VISIBLE); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 300 | } else if (toIndex == -1) { |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 301 | if (animate) { |
Adam Cohen | c798b66 | 2011-10-21 12:07:38 -0700 | [diff] [blame] | 302 | postDelayed(new Runnable() { |
| 303 | public void run() { |
| 304 | view.setAlpha(0); |
| 305 | } |
| 306 | }, STACK_RELAYOUT_DURATION); |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 307 | } else { |
| 308 | view.setAlpha(0f); |
| 309 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 310 | } |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 311 | |
| 312 | // Implement the faked perspective |
| 313 | if (toIndex != -1) { |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 314 | transformViewAtIndex(toIndex, view, animate); |
Adam Cohen | f04e225 | 2010-09-09 18:41:20 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 317 | |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 318 | private void transformViewAtIndex(int index, final View view, boolean animate) { |
| 319 | final float maxPerspectiveShiftY = mPerspectiveShiftY; |
| 320 | final float maxPerspectiveShiftX = mPerspectiveShiftX; |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 321 | |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 322 | if (mStackMode == ITEMS_SLIDE_DOWN) { |
| 323 | index = mMaxNumActiveViews - index - 1; |
| 324 | if (index == mMaxNumActiveViews - 1) index--; |
| 325 | } else { |
| 326 | index--; |
| 327 | if (index < 0) index++; |
| 328 | } |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 329 | |
| 330 | float r = (index * 1.0f) / (mMaxNumActiveViews - 2); |
| 331 | |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 332 | final float scale = 1 - PERSPECTIVE_SCALE_FACTOR * (1 - r); |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 333 | |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 334 | float perspectiveTranslationY = r * maxPerspectiveShiftY; |
| 335 | float scaleShiftCorrectionY = (scale - 1) * |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 336 | (getMeasuredHeight() * (1 - PERSPECTIVE_SHIFT_FACTOR_Y) / 2.0f); |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 337 | final float transY = perspectiveTranslationY + scaleShiftCorrectionY; |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 338 | |
| 339 | float perspectiveTranslationX = (1 - r) * maxPerspectiveShiftX; |
| 340 | float scaleShiftCorrectionX = (1 - scale) * |
| 341 | (getMeasuredWidth() * (1 - PERSPECTIVE_SHIFT_FACTOR_X) / 2.0f); |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 342 | final float transX = perspectiveTranslationX + scaleShiftCorrectionX; |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 343 | |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 344 | // If this view is currently being animated for a certain position, we need to cancel |
Adam Cohen | 69d66e0 | 2011-01-12 14:39:13 -0800 | [diff] [blame] | 345 | // this animation so as not to interfere with the new transformation. |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 346 | if (view instanceof StackFrame) { |
| 347 | ((StackFrame) view).cancelTransformAnimator(); |
Adam Cohen | 69d66e0 | 2011-01-12 14:39:13 -0800 | [diff] [blame] | 348 | } |
| 349 | |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 350 | if (animate) { |
| 351 | PropertyValuesHolder translationX = PropertyValuesHolder.ofFloat("translationX", transX); |
| 352 | PropertyValuesHolder translationY = PropertyValuesHolder.ofFloat("translationY", transY); |
| 353 | PropertyValuesHolder scalePropX = PropertyValuesHolder.ofFloat("scaleX", scale); |
| 354 | PropertyValuesHolder scalePropY = PropertyValuesHolder.ofFloat("scaleY", scale); |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 355 | |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 356 | ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(view, scalePropX, scalePropY, |
| 357 | translationY, translationX); |
Adam Cohen | c0b53be | 2010-12-17 19:23:41 -0800 | [diff] [blame] | 358 | oa.setDuration(STACK_RELAYOUT_DURATION); |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 359 | if (view instanceof StackFrame) { |
| 360 | ((StackFrame) view).setTransformAnimator(oa); |
| 361 | } |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 362 | oa.start(); |
| 363 | } else { |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 364 | view.setTranslationX(transX); |
| 365 | view.setTranslationY(transY); |
| 366 | view.setScaleX(scale); |
| 367 | view.setScaleY(scale); |
| 368 | } |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 369 | } |
| 370 | |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 371 | private void setupStackSlider(View v, int mode) { |
| 372 | mStackSlider.setMode(mode); |
| 373 | if (v != null) { |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 374 | mHighlight.setImageBitmap(sHolographicHelper.createResOutline(v, mResOutColor)); |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 375 | mHighlight.setRotation(v.getRotation()); |
| 376 | mHighlight.setTranslationY(v.getTranslationY()); |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 377 | mHighlight.setTranslationX(v.getTranslationX()); |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 378 | mHighlight.bringToFront(); |
| 379 | v.bringToFront(); |
| 380 | mStackSlider.setView(v); |
| 381 | |
| 382 | v.setVisibility(VISIBLE); |
| 383 | } |
| 384 | } |
| 385 | |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 386 | /** |
| 387 | * {@inheritDoc} |
| 388 | */ |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 389 | @Override |
| 390 | @android.view.RemotableViewMethod |
| 391 | public void showNext() { |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 392 | if (mSwipeGestureType != GESTURE_NONE) return; |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 393 | if (!mTransitionIsSetup) { |
| 394 | View v = getViewAtRelativeIndex(1); |
| 395 | if (v != null) { |
| 396 | setupStackSlider(v, StackSlider.NORMAL_MODE); |
| 397 | mStackSlider.setYProgress(0); |
| 398 | mStackSlider.setXProgress(0); |
| 399 | } |
| 400 | } |
| 401 | super.showNext(); |
| 402 | } |
| 403 | |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 404 | /** |
| 405 | * {@inheritDoc} |
| 406 | */ |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 407 | @Override |
| 408 | @android.view.RemotableViewMethod |
| 409 | public void showPrevious() { |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 410 | if (mSwipeGestureType != GESTURE_NONE) return; |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 411 | if (!mTransitionIsSetup) { |
| 412 | View v = getViewAtRelativeIndex(0); |
| 413 | if (v != null) { |
| 414 | setupStackSlider(v, StackSlider.NORMAL_MODE); |
| 415 | mStackSlider.setYProgress(1); |
| 416 | mStackSlider.setXProgress(0); |
| 417 | } |
| 418 | } |
| 419 | super.showPrevious(); |
| 420 | } |
| 421 | |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 422 | @Override |
Adam Cohen | ef17dd4 | 2011-01-20 17:20:57 -0800 | [diff] [blame] | 423 | void showOnly(int childIndex, boolean animate) { |
| 424 | super.showOnly(childIndex, animate); |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 425 | |
| 426 | // Here we need to make sure that the z-order of the children is correct |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 427 | for (int i = mCurrentWindowEnd; i >= mCurrentWindowStart; i--) { |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 428 | int index = modulo(i, getWindowSize()); |
Adam Cohen | d38a0ce | 2011-04-06 13:20:42 -0700 | [diff] [blame] | 429 | ViewAndMetaData vm = mViewsMap.get(index); |
| 430 | if (vm != null) { |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 431 | View v = mViewsMap.get(index).view; |
| 432 | if (v != null) v.bringToFront(); |
| 433 | } |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 434 | } |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 435 | if (mHighlight != null) { |
| 436 | mHighlight.bringToFront(); |
| 437 | } |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 438 | mTransitionIsSetup = false; |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 439 | mClickFeedbackIsValid = false; |
| 440 | } |
| 441 | |
| 442 | void updateClickFeedback() { |
| 443 | if (!mClickFeedbackIsValid) { |
Adam Cohen | 9c29548 | 2010-11-18 15:19:48 -0800 | [diff] [blame] | 444 | View v = getViewAtRelativeIndex(1); |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 445 | if (v != null) { |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 446 | mClickFeedback.setImageBitmap( |
| 447 | sHolographicHelper.createClickOutline(v, mClickColor)); |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 448 | mClickFeedback.setTranslationX(v.getTranslationX()); |
| 449 | mClickFeedback.setTranslationY(v.getTranslationY()); |
| 450 | } |
| 451 | mClickFeedbackIsValid = true; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | void showTapFeedback(View v) { |
| 457 | updateClickFeedback(); |
| 458 | mClickFeedback.setVisibility(VISIBLE); |
| 459 | mClickFeedback.bringToFront(); |
| 460 | invalidate(); |
| 461 | } |
| 462 | |
| 463 | @Override |
| 464 | void hideTapFeedback(View v) { |
| 465 | mClickFeedback.setVisibility(INVISIBLE); |
| 466 | invalidate(); |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Adam Cohen | f04e225 | 2010-09-09 18:41:20 -0700 | [diff] [blame] | 469 | private void updateChildTransforms() { |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 470 | for (int i = 0; i < getNumActiveViews(); i++) { |
Adam Cohen | f04e225 | 2010-09-09 18:41:20 -0700 | [diff] [blame] | 471 | View v = getViewAtRelativeIndex(i); |
| 472 | if (v != null) { |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 473 | transformViewAtIndex(i, v, false); |
Adam Cohen | f04e225 | 2010-09-09 18:41:20 -0700 | [diff] [blame] | 474 | } |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 475 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 478 | private static class StackFrame extends FrameLayout { |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 479 | WeakReference<ObjectAnimator> transformAnimator; |
| 480 | WeakReference<ObjectAnimator> sliderAnimator; |
| 481 | |
| 482 | public StackFrame(Context context) { |
| 483 | super(context); |
| 484 | } |
| 485 | |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 486 | void setTransformAnimator(ObjectAnimator oa) { |
| 487 | transformAnimator = new WeakReference<ObjectAnimator>(oa); |
| 488 | } |
| 489 | |
| 490 | void setSliderAnimator(ObjectAnimator oa) { |
| 491 | sliderAnimator = new WeakReference<ObjectAnimator>(oa); |
| 492 | } |
| 493 | |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 494 | boolean cancelTransformAnimator() { |
| 495 | if (transformAnimator != null) { |
| 496 | ObjectAnimator oa = transformAnimator.get(); |
| 497 | if (oa != null) { |
| 498 | oa.cancel(); |
| 499 | return true; |
| 500 | } |
| 501 | } |
| 502 | return false; |
| 503 | } |
| 504 | |
| 505 | boolean cancelSliderAnimator() { |
| 506 | if (sliderAnimator != null) { |
| 507 | ObjectAnimator oa = sliderAnimator.get(); |
| 508 | if (oa != null) { |
| 509 | oa.cancel(); |
| 510 | return true; |
| 511 | } |
| 512 | } |
| 513 | return false; |
| 514 | } |
| 515 | } |
| 516 | |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 517 | @Override |
| 518 | FrameLayout getFrameForChild() { |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 519 | StackFrame fl = new StackFrame(mContext); |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 520 | fl.setPadding(mFramePadding, mFramePadding, mFramePadding, mFramePadding); |
| 521 | return fl; |
| 522 | } |
| 523 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 524 | /** |
| 525 | * Apply any necessary tranforms for the child that is being added. |
| 526 | */ |
| 527 | void applyTransformForChildAtIndex(View child, int relativeIndex) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | @Override |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 531 | protected void dispatchDraw(Canvas canvas) { |
Adam Cohen | 321aa2b | 2011-03-08 13:33:29 -0800 | [diff] [blame] | 532 | boolean expandClipRegion = false; |
| 533 | |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 534 | canvas.getClipBounds(stackInvalidateRect); |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 535 | final int childCount = getChildCount(); |
| 536 | for (int i = 0; i < childCount; i++) { |
Adam Cohen | e86ff4d | 2011-01-21 17:46:11 -0800 | [diff] [blame] | 537 | final View child = getChildAt(i); |
| 538 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 539 | if ((lp.horizontalOffset == 0 && lp.verticalOffset == 0) || |
| 540 | child.getAlpha() == 0f || child.getVisibility() != VISIBLE) { |
| 541 | lp.resetInvalidateRect(); |
| 542 | } |
Adam Cohen | 321aa2b | 2011-03-08 13:33:29 -0800 | [diff] [blame] | 543 | Rect childInvalidateRect = lp.getInvalidateRect(); |
| 544 | if (!childInvalidateRect.isEmpty()) { |
| 545 | expandClipRegion = true; |
| 546 | stackInvalidateRect.union(childInvalidateRect); |
| 547 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 548 | } |
Adam Cohen | 321aa2b | 2011-03-08 13:33:29 -0800 | [diff] [blame] | 549 | |
| 550 | // We only expand the clip bounds if necessary. |
| 551 | if (expandClipRegion) { |
Derek Sollenberger | 2ad19e5 | 2018-04-18 15:03:09 -0400 | [diff] [blame] | 552 | canvas.save(); |
| 553 | canvas.clipRectUnion(stackInvalidateRect); |
Adam Cohen | 321aa2b | 2011-03-08 13:33:29 -0800 | [diff] [blame] | 554 | super.dispatchDraw(canvas); |
| 555 | canvas.restore(); |
| 556 | } else { |
| 557 | super.dispatchDraw(canvas); |
| 558 | } |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 559 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 560 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 561 | private void onLayout() { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 562 | if (!mFirstLayoutHappened) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 563 | mFirstLayoutHappened = true; |
Adam Cohen | 53838d2 | 2011-01-26 21:32:33 -0800 | [diff] [blame] | 564 | updateChildTransforms(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 565 | } |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 566 | |
Adam Cohen | c6a4716 | 2011-01-27 18:05:02 -0800 | [diff] [blame] | 567 | final int newSlideAmount = Math.round(SLIDE_UP_RATIO * getMeasuredHeight()); |
| 568 | if (mSlideAmount != newSlideAmount) { |
| 569 | mSlideAmount = newSlideAmount; |
| 570 | mSwipeThreshold = Math.round(SWIPE_THRESHOLD_RATIO * newSlideAmount); |
| 571 | } |
| 572 | |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 573 | if (Float.compare(mPerspectiveShiftY, mNewPerspectiveShiftY) != 0 || |
| 574 | Float.compare(mPerspectiveShiftX, mNewPerspectiveShiftX) != 0) { |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 575 | |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 576 | mPerspectiveShiftY = mNewPerspectiveShiftY; |
| 577 | mPerspectiveShiftX = mNewPerspectiveShiftX; |
Adam Cohen | 53838d2 | 2011-01-26 21:32:33 -0800 | [diff] [blame] | 578 | updateChildTransforms(); |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 579 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Adam Cohen | a8a7c92 | 2011-02-28 17:20:44 -0800 | [diff] [blame] | 582 | @Override |
| 583 | public boolean onGenericMotionEvent(MotionEvent event) { |
| 584 | if ((event.getSource() & InputDevice.SOURCE_CLASS_POINTER) != 0) { |
| 585 | switch (event.getAction()) { |
| 586 | case MotionEvent.ACTION_SCROLL: { |
| 587 | final float vscroll = event.getAxisValue(MotionEvent.AXIS_VSCROLL); |
| 588 | if (vscroll < 0) { |
| 589 | pacedScroll(false); |
| 590 | return true; |
| 591 | } else if (vscroll > 0) { |
| 592 | pacedScroll(true); |
| 593 | return true; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | return super.onGenericMotionEvent(event); |
| 599 | } |
| 600 | |
| 601 | // This ensures that the frequency of stack flips caused by scrolls is capped |
| 602 | private void pacedScroll(boolean up) { |
| 603 | long timeSinceLastScroll = System.currentTimeMillis() - mLastScrollTime; |
| 604 | if (timeSinceLastScroll > MIN_TIME_BETWEEN_SCROLLS) { |
| 605 | if (up) { |
| 606 | showPrevious(); |
| 607 | } else { |
| 608 | showNext(); |
| 609 | } |
| 610 | mLastScrollTime = System.currentTimeMillis(); |
| 611 | } |
| 612 | } |
| 613 | |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 614 | /** |
| 615 | * {@inheritDoc} |
| 616 | */ |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 617 | @Override |
| 618 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 619 | int action = ev.getAction(); |
| 620 | switch(action & MotionEvent.ACTION_MASK) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 621 | case MotionEvent.ACTION_DOWN: { |
| 622 | if (mActivePointerId == INVALID_POINTER) { |
| 623 | mInitialX = ev.getX(); |
| 624 | mInitialY = ev.getY(); |
| 625 | mActivePointerId = ev.getPointerId(0); |
| 626 | } |
| 627 | break; |
| 628 | } |
| 629 | case MotionEvent.ACTION_MOVE: { |
| 630 | int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 631 | if (pointerIndex == INVALID_POINTER) { |
| 632 | // no data for our primary pointer, this shouldn't happen, log it |
| 633 | Log.d(TAG, "Error: No data for our primary pointer."); |
| 634 | return false; |
| 635 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 636 | float newY = ev.getY(pointerIndex); |
| 637 | float deltaY = newY - mInitialY; |
| 638 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 639 | beginGestureIfNeeded(deltaY); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | case MotionEvent.ACTION_POINTER_UP: { |
| 643 | onSecondaryPointerUp(ev); |
| 644 | break; |
| 645 | } |
| 646 | case MotionEvent.ACTION_UP: |
| 647 | case MotionEvent.ACTION_CANCEL: { |
| 648 | mActivePointerId = INVALID_POINTER; |
| 649 | mSwipeGestureType = GESTURE_NONE; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| 653 | return mSwipeGestureType != GESTURE_NONE; |
| 654 | } |
| 655 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 656 | private void beginGestureIfNeeded(float deltaY) { |
| 657 | if ((int) Math.abs(deltaY) > mTouchSlop && mSwipeGestureType == GESTURE_NONE) { |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 658 | final int swipeGestureType = deltaY < 0 ? GESTURE_SLIDE_UP : GESTURE_SLIDE_DOWN; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 659 | cancelLongPress(); |
| 660 | requestDisallowInterceptTouchEvent(true); |
| 661 | |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 662 | if (mAdapter == null) return; |
Adam Cohen | ef17dd4 | 2011-01-20 17:20:57 -0800 | [diff] [blame] | 663 | final int adapterCount = getCount(); |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 664 | |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 665 | int activeIndex; |
| 666 | if (mStackMode == ITEMS_SLIDE_UP) { |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 667 | activeIndex = (swipeGestureType == GESTURE_SLIDE_DOWN) ? 0 : 1; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 668 | } else { |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 669 | activeIndex = (swipeGestureType == GESTURE_SLIDE_DOWN) ? 1 : 0; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 670 | } |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 671 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 672 | boolean endOfStack = mLoopViews && adapterCount == 1 |
| 673 | && ((mStackMode == ITEMS_SLIDE_UP && swipeGestureType == GESTURE_SLIDE_UP) |
| 674 | || (mStackMode == ITEMS_SLIDE_DOWN && swipeGestureType == GESTURE_SLIDE_DOWN)); |
| 675 | boolean beginningOfStack = mLoopViews && adapterCount == 1 |
| 676 | && ((mStackMode == ITEMS_SLIDE_DOWN && swipeGestureType == GESTURE_SLIDE_UP) |
| 677 | || (mStackMode == ITEMS_SLIDE_UP && swipeGestureType == GESTURE_SLIDE_DOWN)); |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 678 | |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 679 | int stackMode; |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 680 | if (mLoopViews && !beginningOfStack && !endOfStack) { |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 681 | stackMode = StackSlider.NORMAL_MODE; |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 682 | } else if (mCurrentWindowStartUnbounded + activeIndex == -1 || beginningOfStack) { |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 683 | activeIndex++; |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 684 | stackMode = StackSlider.BEGINNING_OF_STACK_MODE; |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 685 | } else if (mCurrentWindowStartUnbounded + activeIndex == adapterCount - 1 || endOfStack) { |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 686 | stackMode = StackSlider.END_OF_STACK_MODE; |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 687 | } else { |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 688 | stackMode = StackSlider.NORMAL_MODE; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 689 | } |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 690 | |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 691 | mTransitionIsSetup = stackMode == StackSlider.NORMAL_MODE; |
| 692 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 693 | View v = getViewAtRelativeIndex(activeIndex); |
| 694 | if (v == null) return; |
| 695 | |
Adam Cohen | 3352b68 | 2010-10-26 13:54:00 -0700 | [diff] [blame] | 696 | setupStackSlider(v, stackMode); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 697 | |
| 698 | // We only register this gesture if we've made it this far without a problem |
| 699 | mSwipeGestureType = swipeGestureType; |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 700 | cancelHandleClick(); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Adam Cohen | 0e2de6d | 2011-01-19 17:16:34 -0800 | [diff] [blame] | 704 | /** |
| 705 | * {@inheritDoc} |
| 706 | */ |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 707 | @Override |
| 708 | public boolean onTouchEvent(MotionEvent ev) { |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 709 | super.onTouchEvent(ev); |
| 710 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 711 | int action = ev.getAction(); |
| 712 | int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 713 | if (pointerIndex == INVALID_POINTER) { |
| 714 | // no data for our primary pointer, this shouldn't happen, log it |
| 715 | Log.d(TAG, "Error: No data for our primary pointer."); |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | float newY = ev.getY(pointerIndex); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 720 | float newX = ev.getX(pointerIndex); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 721 | float deltaY = newY - mInitialY; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 722 | float deltaX = newX - mInitialX; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 723 | if (mVelocityTracker == null) { |
| 724 | mVelocityTracker = VelocityTracker.obtain(); |
| 725 | } |
| 726 | mVelocityTracker.addMovement(ev); |
| 727 | |
| 728 | switch (action & MotionEvent.ACTION_MASK) { |
| 729 | case MotionEvent.ACTION_MOVE: { |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 730 | beginGestureIfNeeded(deltaY); |
| 731 | |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 732 | float rx = deltaX / (mSlideAmount * 1.0f); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 733 | if (mSwipeGestureType == GESTURE_SLIDE_DOWN) { |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 734 | float r = (deltaY - mTouchSlop * 1.0f) / mSlideAmount * 1.0f; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 735 | if (mStackMode == ITEMS_SLIDE_DOWN) r = 1 - r; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 736 | mStackSlider.setYProgress(1 - r); |
| 737 | mStackSlider.setXProgress(rx); |
| 738 | return true; |
| 739 | } else if (mSwipeGestureType == GESTURE_SLIDE_UP) { |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 740 | float r = -(deltaY + mTouchSlop * 1.0f) / mSlideAmount * 1.0f; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 741 | if (mStackMode == ITEMS_SLIDE_DOWN) r = 1 - r; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 742 | mStackSlider.setYProgress(r); |
| 743 | mStackSlider.setXProgress(rx); |
| 744 | return true; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 745 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 746 | break; |
| 747 | } |
| 748 | case MotionEvent.ACTION_UP: { |
| 749 | handlePointerUp(ev); |
| 750 | break; |
| 751 | } |
| 752 | case MotionEvent.ACTION_POINTER_UP: { |
| 753 | onSecondaryPointerUp(ev); |
| 754 | break; |
| 755 | } |
| 756 | case MotionEvent.ACTION_CANCEL: { |
| 757 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 758 | mSwipeGestureType = GESTURE_NONE; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 759 | break; |
| 760 | } |
| 761 | } |
| 762 | return true; |
| 763 | } |
| 764 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 765 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 766 | final int activePointerIndex = ev.getActionIndex(); |
| 767 | final int pointerId = ev.getPointerId(activePointerIndex); |
| 768 | if (pointerId == mActivePointerId) { |
| 769 | |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 770 | int activeViewIndex = (mSwipeGestureType == GESTURE_SLIDE_DOWN) ? 0 : 1; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 771 | |
| 772 | View v = getViewAtRelativeIndex(activeViewIndex); |
| 773 | if (v == null) return; |
| 774 | |
| 775 | // Our primary pointer has gone up -- let's see if we can find |
| 776 | // another pointer on the view. If so, then we should replace |
| 777 | // our primary pointer with this new pointer and adjust things |
| 778 | // so that the view doesn't jump |
| 779 | for (int index = 0; index < ev.getPointerCount(); index++) { |
| 780 | if (index != activePointerIndex) { |
| 781 | |
| 782 | float x = ev.getX(index); |
| 783 | float y = ev.getY(index); |
| 784 | |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 785 | mTouchRect.set(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); |
| 786 | if (mTouchRect.contains(Math.round(x), Math.round(y))) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 787 | float oldX = ev.getX(activePointerIndex); |
| 788 | float oldY = ev.getY(activePointerIndex); |
| 789 | |
| 790 | // adjust our frame of reference to avoid a jump |
| 791 | mInitialY += (y - oldY); |
| 792 | mInitialX += (x - oldX); |
| 793 | |
| 794 | mActivePointerId = ev.getPointerId(index); |
| 795 | if (mVelocityTracker != null) { |
| 796 | mVelocityTracker.clear(); |
| 797 | } |
| 798 | // ok, we're good, we found a new pointer which is touching the active view |
| 799 | return; |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | // if we made it this far, it means we didn't find a satisfactory new pointer :(, |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 804 | // so end the gesture |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 805 | handlePointerUp(ev); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | private void handlePointerUp(MotionEvent ev) { |
| 810 | int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 811 | float newY = ev.getY(pointerIndex); |
| 812 | int deltaY = (int) (newY - mInitialY); |
Adam Cohen | 26e30bb | 2010-12-03 18:16:12 -0800 | [diff] [blame] | 813 | mLastInteractionTime = System.currentTimeMillis(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 814 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 815 | if (mVelocityTracker != null) { |
| 816 | mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 817 | mYVelocity = (int) mVelocityTracker.getYVelocity(mActivePointerId); |
| 818 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 819 | |
| 820 | if (mVelocityTracker != null) { |
| 821 | mVelocityTracker.recycle(); |
| 822 | mVelocityTracker = null; |
| 823 | } |
| 824 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 825 | if (deltaY > mSwipeThreshold && mSwipeGestureType == GESTURE_SLIDE_DOWN |
| 826 | && mStackSlider.mMode == StackSlider.NORMAL_MODE) { |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 827 | // We reset the gesture variable, because otherwise we will ignore showPrevious() / |
| 828 | // showNext(); |
| 829 | mSwipeGestureType = GESTURE_NONE; |
| 830 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 831 | // Swipe threshold exceeded, swipe down |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 832 | if (mStackMode == ITEMS_SLIDE_UP) { |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 833 | showPrevious(); |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 834 | } else { |
| 835 | showNext(); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 836 | } |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 837 | mHighlight.bringToFront(); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 838 | } else if (deltaY < -mSwipeThreshold && mSwipeGestureType == GESTURE_SLIDE_UP |
| 839 | && mStackSlider.mMode == StackSlider.NORMAL_MODE) { |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 840 | // We reset the gesture variable, because otherwise we will ignore showPrevious() / |
| 841 | // showNext(); |
| 842 | mSwipeGestureType = GESTURE_NONE; |
| 843 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 844 | // Swipe threshold exceeded, swipe up |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 845 | if (mStackMode == ITEMS_SLIDE_UP) { |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 846 | showNext(); |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 847 | } else { |
| 848 | showPrevious(); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 851 | mHighlight.bringToFront(); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 852 | } else if (mSwipeGestureType == GESTURE_SLIDE_UP ) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 853 | // Didn't swipe up far enough, snap back down |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 854 | int duration; |
| 855 | float finalYProgress = (mStackMode == ITEMS_SLIDE_DOWN) ? 1 : 0; |
| 856 | if (mStackMode == ITEMS_SLIDE_UP || mStackSlider.mMode != StackSlider.NORMAL_MODE) { |
| 857 | duration = Math.round(mStackSlider.getDurationForNeutralPosition()); |
| 858 | } else { |
| 859 | duration = Math.round(mStackSlider.getDurationForOffscreenPosition()); |
| 860 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 861 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 862 | StackSlider animationSlider = new StackSlider(mStackSlider); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 863 | PropertyValuesHolder snapBackY = PropertyValuesHolder.ofFloat("YProgress", finalYProgress); |
| 864 | PropertyValuesHolder snapBackX = PropertyValuesHolder.ofFloat("XProgress", 0.0f); |
| 865 | ObjectAnimator pa = ObjectAnimator.ofPropertyValuesHolder(animationSlider, |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 866 | snapBackX, snapBackY); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 867 | pa.setDuration(duration); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 868 | pa.setInterpolator(new LinearInterpolator()); |
| 869 | pa.start(); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 870 | } else if (mSwipeGestureType == GESTURE_SLIDE_DOWN) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 871 | // Didn't swipe down far enough, snap back up |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 872 | float finalYProgress = (mStackMode == ITEMS_SLIDE_DOWN) ? 0 : 1; |
| 873 | int duration; |
| 874 | if (mStackMode == ITEMS_SLIDE_DOWN || mStackSlider.mMode != StackSlider.NORMAL_MODE) { |
| 875 | duration = Math.round(mStackSlider.getDurationForNeutralPosition()); |
| 876 | } else { |
| 877 | duration = Math.round(mStackSlider.getDurationForOffscreenPosition()); |
| 878 | } |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 879 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 880 | StackSlider animationSlider = new StackSlider(mStackSlider); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 881 | PropertyValuesHolder snapBackY = |
| 882 | PropertyValuesHolder.ofFloat("YProgress",finalYProgress); |
| 883 | PropertyValuesHolder snapBackX = PropertyValuesHolder.ofFloat("XProgress", 0.0f); |
| 884 | ObjectAnimator pa = ObjectAnimator.ofPropertyValuesHolder(animationSlider, |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 885 | snapBackX, snapBackY); |
Chet Haase | 2794eb3 | 2010-10-12 16:29:28 -0700 | [diff] [blame] | 886 | pa.setDuration(duration); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 887 | pa.start(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 891 | mSwipeGestureType = GESTURE_NONE; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | private class StackSlider { |
| 895 | View mView; |
| 896 | float mYProgress; |
| 897 | float mXProgress; |
| 898 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 899 | static final int NORMAL_MODE = 0; |
| 900 | static final int BEGINNING_OF_STACK_MODE = 1; |
| 901 | static final int END_OF_STACK_MODE = 2; |
| 902 | |
| 903 | int mMode = NORMAL_MODE; |
| 904 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 905 | public StackSlider() { |
| 906 | } |
| 907 | |
| 908 | public StackSlider(StackSlider copy) { |
| 909 | mView = copy.mView; |
| 910 | mYProgress = copy.mYProgress; |
| 911 | mXProgress = copy.mXProgress; |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 912 | mMode = copy.mMode; |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 915 | private float cubic(float r) { |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 916 | return (float) (Math.pow(2 * r - 1, 3) + 1) / 2.0f; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | private float highlightAlphaInterpolator(float r) { |
| 920 | float pivot = 0.4f; |
| 921 | if (r < pivot) { |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 922 | return 0.85f * cubic(r / pivot); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 923 | } else { |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 924 | return 0.85f * cubic(1 - (r - pivot) / (1 - pivot)); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | private float viewAlphaInterpolator(float r) { |
| 929 | float pivot = 0.3f; |
| 930 | if (r > pivot) { |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 931 | return (r - pivot) / (1 - pivot); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 932 | } else { |
| 933 | return 0; |
| 934 | } |
| 935 | } |
| 936 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 937 | private float rotationInterpolator(float r) { |
| 938 | float pivot = 0.2f; |
| 939 | if (r < pivot) { |
| 940 | return 0; |
| 941 | } else { |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 942 | return (r - pivot) / (1 - pivot); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 946 | void setView(View v) { |
| 947 | mView = v; |
| 948 | } |
| 949 | |
| 950 | public void setYProgress(float r) { |
| 951 | // enforce r between 0 and 1 |
| 952 | r = Math.min(1.0f, r); |
| 953 | r = Math.max(0, r); |
| 954 | |
| 955 | mYProgress = r; |
Adam Cohen | a02fdf1 | 2010-11-03 13:27:40 -0700 | [diff] [blame] | 956 | if (mView == null) return; |
| 957 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 958 | final LayoutParams viewLp = (LayoutParams) mView.getLayoutParams(); |
| 959 | final LayoutParams highlightLp = (LayoutParams) mHighlight.getLayoutParams(); |
| 960 | |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 961 | int stackDirection = (mStackMode == ITEMS_SLIDE_UP) ? 1 : -1; |
| 962 | |
Adam Cohen | 4886721 | 2011-01-09 13:49:40 -0800 | [diff] [blame] | 963 | // We need to prevent any clipping issues which may arise by setting a layer type. |
| 964 | // This doesn't come for free however, so we only want to enable it when required. |
| 965 | if (Float.compare(0f, mYProgress) != 0 && Float.compare(1.0f, mYProgress) != 0) { |
| 966 | if (mView.getLayerType() == LAYER_TYPE_NONE) { |
| 967 | mView.setLayerType(LAYER_TYPE_HARDWARE, null); |
| 968 | } |
| 969 | } else { |
| 970 | if (mView.getLayerType() != LAYER_TYPE_NONE) { |
| 971 | mView.setLayerType(LAYER_TYPE_NONE, null); |
| 972 | } |
| 973 | } |
| 974 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 975 | switch (mMode) { |
| 976 | case NORMAL_MODE: |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 977 | viewLp.setVerticalOffset(Math.round(-r * stackDirection * mSlideAmount)); |
| 978 | highlightLp.setVerticalOffset(Math.round(-r * stackDirection * mSlideAmount)); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 979 | mHighlight.setAlpha(highlightAlphaInterpolator(r)); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 980 | |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 981 | float alpha = viewAlphaInterpolator(1 - r); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 982 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 983 | // We make sure that views which can't be seen (have 0 alpha) are also invisible |
| 984 | // so that they don't interfere with click events. |
| 985 | if (mView.getAlpha() == 0 && alpha != 0 && mView.getVisibility() != VISIBLE) { |
| 986 | mView.setVisibility(VISIBLE); |
| 987 | } else if (alpha == 0 && mView.getAlpha() != 0 |
| 988 | && mView.getVisibility() == VISIBLE) { |
| 989 | mView.setVisibility(INVISIBLE); |
| 990 | } |
| 991 | |
| 992 | mView.setAlpha(alpha); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 993 | mView.setRotationX(stackDirection * 90.0f * rotationInterpolator(r)); |
| 994 | mHighlight.setRotationX(stackDirection * 90.0f * rotationInterpolator(r)); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 995 | break; |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 996 | case END_OF_STACK_MODE: |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 997 | r = r * 0.2f; |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 998 | viewLp.setVerticalOffset(Math.round(-stackDirection * r * mSlideAmount)); |
| 999 | highlightLp.setVerticalOffset(Math.round(-stackDirection * r * mSlideAmount)); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 1000 | mHighlight.setAlpha(highlightAlphaInterpolator(r)); |
| 1001 | break; |
Adam Cohen | 96d8d56 | 2010-10-24 11:12:18 -0700 | [diff] [blame] | 1002 | case BEGINNING_OF_STACK_MODE: |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1003 | r = (1-r) * 0.2f; |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 1004 | viewLp.setVerticalOffset(Math.round(stackDirection * r * mSlideAmount)); |
| 1005 | highlightLp.setVerticalOffset(Math.round(stackDirection * r * mSlideAmount)); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 1006 | mHighlight.setAlpha(highlightAlphaInterpolator(r)); |
| 1007 | break; |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 1008 | } |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | public void setXProgress(float r) { |
| 1012 | // enforce r between 0 and 1 |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 1013 | r = Math.min(2.0f, r); |
| 1014 | r = Math.max(-2.0f, r); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1015 | |
| 1016 | mXProgress = r; |
| 1017 | |
Adam Cohen | a02fdf1 | 2010-11-03 13:27:40 -0700 | [diff] [blame] | 1018 | if (mView == null) return; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1019 | final LayoutParams viewLp = (LayoutParams) mView.getLayoutParams(); |
| 1020 | final LayoutParams highlightLp = (LayoutParams) mHighlight.getLayoutParams(); |
| 1021 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 1022 | r *= 0.2f; |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 1023 | viewLp.setHorizontalOffset(Math.round(r * mSlideAmount)); |
| 1024 | highlightLp.setHorizontalOffset(Math.round(r * mSlideAmount)); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 1027 | void setMode(int mode) { |
| 1028 | mMode = mode; |
| 1029 | } |
| 1030 | |
| 1031 | float getDurationForNeutralPosition() { |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1032 | return getDuration(false, 0); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | float getDurationForOffscreenPosition() { |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1036 | return getDuration(true, 0); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1039 | float getDurationForNeutralPosition(float velocity) { |
| 1040 | return getDuration(false, velocity); |
| 1041 | } |
| 1042 | |
| 1043 | float getDurationForOffscreenPosition(float velocity) { |
| 1044 | return getDuration(true, velocity); |
| 1045 | } |
| 1046 | |
| 1047 | private float getDuration(boolean invert, float velocity) { |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 1048 | if (mView != null) { |
| 1049 | final LayoutParams viewLp = (LayoutParams) mView.getLayoutParams(); |
| 1050 | |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 1051 | float d = (float) Math.hypot(viewLp.horizontalOffset, viewLp.verticalOffset); |
| 1052 | float maxd = (float) Math.hypot(mSlideAmount, 0.4f * mSlideAmount); |
Tony Wickham | 400ef79 | 2016-11-07 10:18:55 -0800 | [diff] [blame] | 1053 | if (d > maxd) { |
| 1054 | // Because mSlideAmount is updated in onLayout(), it is possible that d > maxd |
| 1055 | // if we get onLayout() right before this method is called. |
| 1056 | d = maxd; |
| 1057 | } |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1058 | |
| 1059 | if (velocity == 0) { |
| 1060 | return (invert ? (1 - d / maxd) : d / maxd) * DEFAULT_ANIMATION_DURATION; |
| 1061 | } else { |
| 1062 | float duration = invert ? d / Math.abs(velocity) : |
| 1063 | (maxd - d) / Math.abs(velocity); |
| 1064 | if (duration < MINIMUM_ANIMATION_DURATION || |
| 1065 | duration > DEFAULT_ANIMATION_DURATION) { |
| 1066 | return getDuration(invert, 0); |
| 1067 | } else { |
| 1068 | return duration; |
| 1069 | } |
| 1070 | } |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame] | 1071 | } |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 1075 | // Used for animations |
| 1076 | @SuppressWarnings({"UnusedDeclaration"}) |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1077 | public float getYProgress() { |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1078 | return mYProgress; |
| 1079 | } |
| 1080 | |
Romain Guy | e5ebcb0 | 2010-10-15 13:57:28 -0700 | [diff] [blame] | 1081 | // Used for animations |
| 1082 | @SuppressWarnings({"UnusedDeclaration"}) |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1083 | public float getXProgress() { |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1084 | return mXProgress; |
| 1085 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1088 | LayoutParams createOrReuseLayoutParams(View v) { |
| 1089 | final ViewGroup.LayoutParams currentLp = v.getLayoutParams(); |
| 1090 | if (currentLp instanceof LayoutParams) { |
| 1091 | LayoutParams lp = (LayoutParams) currentLp; |
| 1092 | lp.setHorizontalOffset(0); |
| 1093 | lp.setVerticalOffset(0); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1094 | lp.width = 0; |
| 1095 | lp.width = 0; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1096 | return lp; |
| 1097 | } |
| 1098 | return new LayoutParams(v); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1101 | @Override |
| 1102 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
Adam Cohen | ef17dd4 | 2011-01-20 17:20:57 -0800 | [diff] [blame] | 1103 | checkForAndHandleDataChanged(); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 1104 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1105 | final int childCount = getChildCount(); |
| 1106 | for (int i = 0; i < childCount; i++) { |
| 1107 | final View child = getChildAt(i); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1108 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1109 | int childRight = mPaddingLeft + child.getMeasuredWidth(); |
| 1110 | int childBottom = mPaddingTop + child.getMeasuredHeight(); |
| 1111 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1112 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1113 | child.layout(mPaddingLeft + lp.horizontalOffset, mPaddingTop + lp.verticalOffset, |
| 1114 | childRight + lp.horizontalOffset, childBottom + lp.verticalOffset); |
| 1115 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1116 | } |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1117 | onLayout(); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Adam Cohen | 26e30bb | 2010-12-03 18:16:12 -0800 | [diff] [blame] | 1120 | @Override |
| 1121 | public void advance() { |
| 1122 | long timeSinceLastInteraction = System.currentTimeMillis() - mLastInteractionTime; |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 1123 | |
| 1124 | if (mAdapter == null) return; |
Adam Cohen | ef17dd4 | 2011-01-20 17:20:57 -0800 | [diff] [blame] | 1125 | final int adapterCount = getCount(); |
Adam Cohen | c99ff73 | 2011-01-16 16:11:41 -0800 | [diff] [blame] | 1126 | if (adapterCount == 1 && mLoopViews) return; |
| 1127 | |
Adam Cohen | 26e30bb | 2010-12-03 18:16:12 -0800 | [diff] [blame] | 1128 | if (mSwipeGestureType == GESTURE_NONE && |
| 1129 | timeSinceLastInteraction > MIN_TIME_BETWEEN_INTERACTION_AND_AUTOADVANCE) { |
| 1130 | showNext(); |
| 1131 | } |
| 1132 | } |
| 1133 | |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1134 | private void measureChildren() { |
| 1135 | final int count = getChildCount(); |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 1136 | |
| 1137 | final int measuredWidth = getMeasuredWidth(); |
| 1138 | final int measuredHeight = getMeasuredHeight(); |
| 1139 | |
| 1140 | final int childWidth = Math.round(measuredWidth*(1-PERSPECTIVE_SHIFT_FACTOR_X)) |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 1141 | - mPaddingLeft - mPaddingRight; |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 1142 | final int childHeight = Math.round(measuredHeight*(1-PERSPECTIVE_SHIFT_FACTOR_Y)) |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1143 | - mPaddingTop - mPaddingBottom; |
| 1144 | |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 1145 | int maxWidth = 0; |
| 1146 | int maxHeight = 0; |
| 1147 | |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1148 | for (int i = 0; i < count; i++) { |
| 1149 | final View child = getChildAt(i); |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 1150 | child.measure(MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.AT_MOST), |
| 1151 | MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.AT_MOST)); |
| 1152 | |
| 1153 | if (child != mHighlight && child != mClickFeedback) { |
| 1154 | final int childMeasuredWidth = child.getMeasuredWidth(); |
| 1155 | final int childMeasuredHeight = child.getMeasuredHeight(); |
| 1156 | if (childMeasuredWidth > maxWidth) { |
| 1157 | maxWidth = childMeasuredWidth; |
| 1158 | } |
| 1159 | if (childMeasuredHeight > maxHeight) { |
| 1160 | maxHeight = childMeasuredHeight; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | mNewPerspectiveShiftX = PERSPECTIVE_SHIFT_FACTOR_X * measuredWidth; |
| 1166 | mNewPerspectiveShiftY = PERSPECTIVE_SHIFT_FACTOR_Y * measuredHeight; |
Adam Cohen | 53838d2 | 2011-01-26 21:32:33 -0800 | [diff] [blame] | 1167 | |
| 1168 | // If we have extra space, we try and spread the items out |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 1169 | if (maxWidth > 0 && count > 0 && maxWidth < childWidth) { |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 1170 | mNewPerspectiveShiftX = measuredWidth - maxWidth; |
| 1171 | } |
| 1172 | |
Adam Cohen | 78db1aa | 2011-01-25 12:24:23 -0800 | [diff] [blame] | 1173 | if (maxHeight > 0 && count > 0 && maxHeight < childHeight) { |
Adam Cohen | 36f4390 | 2010-12-15 21:17:33 -0800 | [diff] [blame] | 1174 | mNewPerspectiveShiftY = measuredHeight - maxHeight; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | @Override |
| 1179 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 1180 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); |
| 1181 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); |
| 1182 | final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); |
| 1183 | final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); |
| 1184 | |
| 1185 | boolean haveChildRefSize = (mReferenceChildWidth != -1 && mReferenceChildHeight != -1); |
| 1186 | |
| 1187 | // We need to deal with the case where our parent hasn't told us how |
| 1188 | // big we should be. In this case we should |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 1189 | float factorY = 1/(1 - PERSPECTIVE_SHIFT_FACTOR_Y); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1190 | if (heightSpecMode == MeasureSpec.UNSPECIFIED) { |
| 1191 | heightSpecSize = haveChildRefSize ? |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 1192 | Math.round(mReferenceChildHeight * (1 + factorY)) + |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1193 | mPaddingTop + mPaddingBottom : 0; |
| 1194 | } else if (heightSpecMode == MeasureSpec.AT_MOST) { |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1195 | if (haveChildRefSize) { |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 1196 | int height = Math.round(mReferenceChildHeight * (1 + factorY)) |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1197 | + mPaddingTop + mPaddingBottom; |
| 1198 | if (height <= heightSpecSize) { |
| 1199 | heightSpecSize = height; |
| 1200 | } else { |
| 1201 | heightSpecSize |= MEASURED_STATE_TOO_SMALL; |
Adam Cohen | ef17dd4 | 2011-01-20 17:20:57 -0800 | [diff] [blame] | 1202 | |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1203 | } |
| 1204 | } else { |
| 1205 | heightSpecSize = 0; |
| 1206 | } |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 1209 | float factorX = 1/(1 - PERSPECTIVE_SHIFT_FACTOR_X); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1210 | if (widthSpecMode == MeasureSpec.UNSPECIFIED) { |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 1211 | widthSpecSize = haveChildRefSize ? |
| 1212 | Math.round(mReferenceChildWidth * (1 + factorX)) + |
| 1213 | mPaddingLeft + mPaddingRight : 0; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1214 | } else if (heightSpecMode == MeasureSpec.AT_MOST) { |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 1215 | if (haveChildRefSize) { |
| 1216 | int width = mReferenceChildWidth + mPaddingLeft + mPaddingRight; |
| 1217 | if (width <= widthSpecSize) { |
| 1218 | widthSpecSize = width; |
| 1219 | } else { |
| 1220 | widthSpecSize |= MEASURED_STATE_TOO_SMALL; |
| 1221 | } |
| 1222 | } else { |
| 1223 | widthSpecSize = 0; |
| 1224 | } |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1225 | } |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1226 | setMeasuredDimension(widthSpecSize, heightSpecSize); |
| 1227 | measureChildren(); |
| 1228 | } |
| 1229 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1230 | @Override |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 1231 | public CharSequence getAccessibilityClassName() { |
| 1232 | return StackView.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1233 | } |
| 1234 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 1235 | /** @hide */ |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1236 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 1237 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 1238 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Svetoslav Ganov | 48d1586 | 2012-05-15 10:10:00 -0700 | [diff] [blame] | 1239 | info.setScrollable(getChildCount() > 1); |
Svetoslav Ganov | fb1e80a | 2012-05-16 17:33:19 -0700 | [diff] [blame] | 1240 | if (isEnabled()) { |
| 1241 | if (getDisplayedChild() < getChildCount() - 1) { |
| 1242 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD); |
| 1243 | } |
| 1244 | if (getDisplayedChild() > 0) { |
| 1245 | info.addAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD); |
| 1246 | } |
Svetoslav Ganov | 48d1586 | 2012-05-15 10:10:00 -0700 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 1250 | /** @hide */ |
Svetoslav Ganov | 48d1586 | 2012-05-15 10:10:00 -0700 | [diff] [blame] | 1251 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 1252 | public boolean performAccessibilityActionInternal(int action, Bundle arguments) { |
| 1253 | if (super.performAccessibilityActionInternal(action, arguments)) { |
Svetoslav Ganov | 48d1586 | 2012-05-15 10:10:00 -0700 | [diff] [blame] | 1254 | return true; |
| 1255 | } |
Svetoslav Ganov | fb1e80a | 2012-05-16 17:33:19 -0700 | [diff] [blame] | 1256 | if (!isEnabled()) { |
| 1257 | return false; |
| 1258 | } |
Svetoslav Ganov | 48d1586 | 2012-05-15 10:10:00 -0700 | [diff] [blame] | 1259 | switch (action) { |
| 1260 | case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD: { |
| 1261 | if (getDisplayedChild() < getChildCount() - 1) { |
| 1262 | showNext(); |
| 1263 | return true; |
| 1264 | } |
| 1265 | } return false; |
| 1266 | case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: { |
| 1267 | if (getDisplayedChild() > 0) { |
| 1268 | showPrevious(); |
| 1269 | return true; |
| 1270 | } |
| 1271 | } return false; |
| 1272 | } |
| 1273 | return false; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 1274 | } |
| 1275 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1276 | class LayoutParams extends ViewGroup.LayoutParams { |
| 1277 | int horizontalOffset; |
| 1278 | int verticalOffset; |
| 1279 | View mView; |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 1280 | private final Rect parentRect = new Rect(); |
| 1281 | private final Rect invalidateRect = new Rect(); |
| 1282 | private final RectF invalidateRectf = new RectF(); |
| 1283 | private final Rect globalInvalidateRect = new Rect(); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1284 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1285 | LayoutParams(View view) { |
| 1286 | super(0, 0); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1287 | width = 0; |
| 1288 | height = 0; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1289 | horizontalOffset = 0; |
| 1290 | verticalOffset = 0; |
| 1291 | mView = view; |
| 1292 | } |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1293 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1294 | LayoutParams(Context c, AttributeSet attrs) { |
| 1295 | super(c, attrs); |
| 1296 | horizontalOffset = 0; |
| 1297 | verticalOffset = 0; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1298 | width = 0; |
| 1299 | height = 0; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1300 | } |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1301 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1302 | void invalidateGlobalRegion(View v, Rect r) { |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 1303 | // We need to make a new rect here, so as not to modify the one passed |
| 1304 | globalInvalidateRect.set(r); |
Adam Cohen | 1ff65d1 | 2011-02-16 14:04:09 -0800 | [diff] [blame] | 1305 | globalInvalidateRect.union(0, 0, getWidth(), getHeight()); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1306 | View p = v; |
| 1307 | if (!(v.getParent() != null && v.getParent() instanceof View)) return; |
| 1308 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1309 | boolean firstPass = true; |
| 1310 | parentRect.set(0, 0, 0, 0); |
Adam Cohen | b7f4d03 | 2010-09-16 15:25:54 -0700 | [diff] [blame] | 1311 | while (p.getParent() != null && p.getParent() instanceof View |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 1312 | && !parentRect.contains(globalInvalidateRect)) { |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1313 | if (!firstPass) { |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 1314 | globalInvalidateRect.offset(p.getLeft() - p.getScrollX(), p.getTop() |
| 1315 | - p.getScrollY()); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1316 | } |
| 1317 | firstPass = false; |
| 1318 | p = (View) p.getParent(); |
Adam Cohen | b7f4d03 | 2010-09-16 15:25:54 -0700 | [diff] [blame] | 1319 | parentRect.set(p.getScrollX(), p.getScrollY(), |
Adam Cohen | 1ff65d1 | 2011-02-16 14:04:09 -0800 | [diff] [blame] | 1320 | p.getWidth() + p.getScrollX(), p.getHeight() + p.getScrollY()); |
| 1321 | p.invalidate(globalInvalidateRect.left, globalInvalidateRect.top, |
| 1322 | globalInvalidateRect.right, globalInvalidateRect.bottom); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 1325 | p.invalidate(globalInvalidateRect.left, globalInvalidateRect.top, |
| 1326 | globalInvalidateRect.right, globalInvalidateRect.bottom); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 1329 | Rect getInvalidateRect() { |
| 1330 | return invalidateRect; |
| 1331 | } |
| 1332 | |
| 1333 | void resetInvalidateRect() { |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 1334 | invalidateRect.set(0, 0, 0, 0); |
Adam Cohen | d51bbb5 | 2010-10-18 10:59:49 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Chet Haase | a18a86b | 2010-09-07 13:20:00 -0700 | [diff] [blame] | 1337 | // This is public so that ObjectAnimator can access it |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1338 | public void setVerticalOffset(int newVerticalOffset) { |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 1339 | setOffsets(horizontalOffset, newVerticalOffset); |
| 1340 | } |
| 1341 | |
| 1342 | public void setHorizontalOffset(int newHorizontalOffset) { |
| 1343 | setOffsets(newHorizontalOffset, verticalOffset); |
| 1344 | } |
| 1345 | |
| 1346 | public void setOffsets(int newHorizontalOffset, int newVerticalOffset) { |
| 1347 | int horizontalOffsetDelta = newHorizontalOffset - horizontalOffset; |
| 1348 | horizontalOffset = newHorizontalOffset; |
| 1349 | int verticalOffsetDelta = newVerticalOffset - verticalOffset; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1350 | verticalOffset = newVerticalOffset; |
| 1351 | |
| 1352 | if (mView != null) { |
| 1353 | mView.requestLayout(); |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 1354 | int left = Math.min(mView.getLeft() + horizontalOffsetDelta, mView.getLeft()); |
| 1355 | int right = Math.max(mView.getRight() + horizontalOffsetDelta, mView.getRight()); |
| 1356 | int top = Math.min(mView.getTop() + verticalOffsetDelta, mView.getTop()); |
| 1357 | int bottom = Math.max(mView.getBottom() + verticalOffsetDelta, mView.getBottom()); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1358 | |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 1359 | invalidateRectf.set(left, top, right, bottom); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1360 | |
| 1361 | float xoffset = -invalidateRectf.left; |
| 1362 | float yoffset = -invalidateRectf.top; |
| 1363 | invalidateRectf.offset(xoffset, yoffset); |
| 1364 | mView.getMatrix().mapRect(invalidateRectf); |
| 1365 | invalidateRectf.offset(-xoffset, -yoffset); |
| 1366 | |
Adam Cohen | 0ac116b | 2010-11-11 11:39:53 -0800 | [diff] [blame] | 1367 | invalidateRect.set((int) Math.floor(invalidateRectf.left), |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1368 | (int) Math.floor(invalidateRectf.top), |
| 1369 | (int) Math.ceil(invalidateRectf.right), |
| 1370 | (int) Math.ceil(invalidateRectf.bottom)); |
| 1371 | |
| 1372 | invalidateGlobalRegion(mView, invalidateRect); |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | private static class HolographicHelper { |
| 1378 | private final Paint mHolographicPaint = new Paint(); |
| 1379 | private final Paint mErasePaint = new Paint(); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1380 | private final Paint mBlurPaint = new Paint(); |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 1381 | private static final int RES_OUT = 0; |
| 1382 | private static final int CLICK_FEEDBACK = 1; |
| 1383 | private float mDensity; |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1384 | private BlurMaskFilter mSmallBlurMaskFilter; |
| 1385 | private BlurMaskFilter mLargeBlurMaskFilter; |
| 1386 | private final Canvas mCanvas = new Canvas(); |
| 1387 | private final Canvas mMaskCanvas = new Canvas(); |
| 1388 | private final int[] mTmpXY = new int[2]; |
| 1389 | private final Matrix mIdentityMatrix = new Matrix(); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1390 | |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 1391 | HolographicHelper(Context context) { |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 1392 | mDensity = context.getResources().getDisplayMetrics().density; |
Adam Cohen | dfcdddd | 2010-09-10 14:38:40 -0700 | [diff] [blame] | 1393 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1394 | mHolographicPaint.setFilterBitmap(true); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1395 | mHolographicPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30)); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1396 | mErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); |
| 1397 | mErasePaint.setFilterBitmap(true); |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1398 | |
| 1399 | mSmallBlurMaskFilter = new BlurMaskFilter(2 * mDensity, BlurMaskFilter.Blur.NORMAL); |
| 1400 | mLargeBlurMaskFilter = new BlurMaskFilter(4 * mDensity, BlurMaskFilter.Blur.NORMAL); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1401 | } |
| 1402 | |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 1403 | Bitmap createClickOutline(View v, int color) { |
| 1404 | return createOutline(v, CLICK_FEEDBACK, color); |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
Adam Cohen | 26f072c | 2011-04-01 16:23:18 -0700 | [diff] [blame] | 1407 | Bitmap createResOutline(View v, int color) { |
| 1408 | return createOutline(v, RES_OUT, color); |
| 1409 | } |
| 1410 | |
| 1411 | Bitmap createOutline(View v, int type, int color) { |
| 1412 | mHolographicPaint.setColor(color); |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 1413 | if (type == RES_OUT) { |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1414 | mBlurPaint.setMaskFilter(mSmallBlurMaskFilter); |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 1415 | } else if (type == CLICK_FEEDBACK) { |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1416 | mBlurPaint.setMaskFilter(mLargeBlurMaskFilter); |
Adam Cohen | 8baf5df | 2010-11-11 15:23:41 -0800 | [diff] [blame] | 1417 | } |
| 1418 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1419 | if (v.getMeasuredWidth() == 0 || v.getMeasuredHeight() == 0) { |
| 1420 | return null; |
| 1421 | } |
| 1422 | |
Dianne Hackborn | dde331c | 2012-08-03 14:01:57 -0700 | [diff] [blame] | 1423 | Bitmap bitmap = Bitmap.createBitmap(v.getResources().getDisplayMetrics(), |
| 1424 | v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888); |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1425 | mCanvas.setBitmap(bitmap); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1426 | |
| 1427 | float rotationX = v.getRotationX(); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1428 | float rotation = v.getRotation(); |
| 1429 | float translationY = v.getTranslationY(); |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 1430 | float translationX = v.getTranslationX(); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1431 | v.setRotationX(0); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1432 | v.setRotation(0); |
| 1433 | v.setTranslationY(0); |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 1434 | v.setTranslationX(0); |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1435 | v.draw(mCanvas); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1436 | v.setRotationX(rotationX); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1437 | v.setRotation(rotation); |
| 1438 | v.setTranslationY(translationY); |
Adam Cohen | 026e121 | 2010-12-13 12:29:17 -0800 | [diff] [blame] | 1439 | v.setTranslationX(translationX); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1440 | |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1441 | drawOutline(mCanvas, bitmap); |
Dianne Hackborn | 6311d0a | 2011-08-02 16:37:58 -0700 | [diff] [blame] | 1442 | mCanvas.setBitmap(null); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1443 | return bitmap; |
| 1444 | } |
| 1445 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1446 | void drawOutline(Canvas dest, Bitmap src) { |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1447 | final int[] xy = mTmpXY; |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1448 | Bitmap mask = src.extractAlpha(mBlurPaint, xy); |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1449 | mMaskCanvas.setBitmap(mask); |
| 1450 | mMaskCanvas.drawBitmap(src, -xy[0], -xy[1], mErasePaint); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1451 | dest.drawColor(0, PorterDuff.Mode.CLEAR); |
Patrick Dubroy | e80202d | 2010-11-16 15:41:08 -0800 | [diff] [blame] | 1452 | dest.setMatrix(mIdentityMatrix); |
Adam Cohen | 839f4a5 | 2010-08-26 17:36:48 -0700 | [diff] [blame] | 1453 | dest.drawBitmap(mask, xy[0], xy[1], mHolographicPaint); |
Dianne Hackborn | 6311d0a | 2011-08-02 16:37:58 -0700 | [diff] [blame] | 1454 | mMaskCanvas.setBitmap(null); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1455 | mask.recycle(); |
| 1456 | } |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 1457 | } |
Adam Cohen | ef17dd4 | 2011-01-20 17:20:57 -0800 | [diff] [blame] | 1458 | } |