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