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