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