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 | |
| 19 | import java.util.WeakHashMap; |
| 20 | |
| 21 | import android.animation.PropertyAnimator; |
| 22 | import android.content.Context; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
| 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; |
| 30 | import android.util.AttributeSet; |
| 31 | import android.util.Log; |
| 32 | import android.view.MotionEvent; |
| 33 | import android.view.VelocityTracker; |
| 34 | import android.view.View; |
| 35 | import android.view.ViewConfiguration; |
| 36 | import android.view.ViewGroup; |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 37 | import android.view.animation.LinearInterpolator; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 38 | import android.widget.RemoteViews.RemoteView; |
| 39 | |
| 40 | @RemoteView |
| 41 | /** |
| 42 | * A view that displays its children in a stack and allows users to discretely swipe |
| 43 | * through the children. |
| 44 | */ |
| 45 | public class StackView extends AdapterViewAnimator { |
| 46 | private final String TAG = "StackView"; |
| 47 | |
| 48 | /** |
| 49 | * Default animation parameters |
| 50 | */ |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 51 | private final int DEFAULT_ANIMATION_DURATION = 400; |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 52 | private final int MINIMUM_ANIMATION_DURATION = 50; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * These specify the different gesture states |
| 56 | */ |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 57 | private static final int GESTURE_NONE = 0; |
| 58 | private static final int GESTURE_SLIDE_UP = 1; |
| 59 | private static final int GESTURE_SLIDE_DOWN = 2; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Specifies how far you need to swipe (up or down) before it |
| 63 | * will be consider a completed gesture when you lift your finger |
| 64 | */ |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 65 | private static final float SWIPE_THRESHOLD_RATIO = 0.35f; |
| 66 | private static final float SLIDE_UP_RATIO = 0.7f; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 67 | |
| 68 | private final WeakHashMap<View, Float> mRotations = new WeakHashMap<View, Float>(); |
| 69 | private final WeakHashMap<View, Integer> |
| 70 | mChildrenToApplyTransformsTo = new WeakHashMap<View, Integer>(); |
| 71 | |
| 72 | /** |
| 73 | * Sentinel value for no current active pointer. |
| 74 | * Used by {@link #mActivePointerId}. |
| 75 | */ |
| 76 | private static final int INVALID_POINTER = -1; |
| 77 | |
| 78 | /** |
| 79 | * These variables are all related to the current state of touch interaction |
| 80 | * with the stack |
| 81 | */ |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 82 | private float mInitialY; |
| 83 | private float mInitialX; |
| 84 | private int mActivePointerId; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 85 | private int mYVelocity = 0; |
| 86 | private int mSwipeGestureType = GESTURE_NONE; |
| 87 | private int mViewHeight; |
| 88 | private int mSwipeThreshold; |
| 89 | private int mTouchSlop; |
| 90 | private int mMaximumVelocity; |
| 91 | private VelocityTracker mVelocityTracker; |
| 92 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 93 | private ImageView mHighlight; |
| 94 | private StackSlider mStackSlider; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 95 | private boolean mFirstLayoutHappened = false; |
| 96 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 97 | public StackView(Context context) { |
| 98 | super(context); |
| 99 | initStackView(); |
| 100 | } |
| 101 | |
| 102 | public StackView(Context context, AttributeSet attrs) { |
| 103 | super(context, attrs); |
| 104 | initStackView(); |
| 105 | } |
| 106 | |
| 107 | private void initStackView() { |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 108 | configureViewAnimator(4, 2, false); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 109 | setStaticTransformationsEnabled(true); |
| 110 | final ViewConfiguration configuration = ViewConfiguration.get(getContext()); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 111 | mTouchSlop = configuration.getScaledTouchSlop(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 112 | mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); |
| 113 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 114 | |
| 115 | mHighlight = new ImageView(getContext()); |
| 116 | mHighlight.setLayoutParams(new LayoutParams(mHighlight)); |
| 117 | addViewInLayout(mHighlight, -1, new LayoutParams(mHighlight)); |
| 118 | mStackSlider = new StackSlider(); |
| 119 | |
| 120 | if (!sPaintsInitialized) { |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 121 | initializePaints(); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 122 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Animate the views between different relative indexes within the {@link AdapterViewAnimator} |
| 127 | */ |
| 128 | void animateViewForTransition(int fromIndex, int toIndex, View view) { |
| 129 | if (fromIndex == -1 && toIndex == 0) { |
| 130 | // Fade item in |
| 131 | if (view.getAlpha() == 1) { |
| 132 | view.setAlpha(0); |
| 133 | } |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 134 | view.setVisibility(VISIBLE); |
| 135 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 136 | PropertyAnimator fadeIn = new PropertyAnimator(DEFAULT_ANIMATION_DURATION, |
| 137 | view, "alpha", view.getAlpha(), 1.0f); |
| 138 | fadeIn.start(); |
| 139 | } else if (fromIndex == mNumActiveViews - 1 && toIndex == mNumActiveViews - 2) { |
| 140 | // Slide item in |
| 141 | view.setVisibility(VISIBLE); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 142 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 143 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 144 | int largestDuration = |
| 145 | Math.round(mStackSlider.getDurationForNeutralPosition()*DEFAULT_ANIMATION_DURATION); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 146 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 147 | int duration = largestDuration; |
| 148 | if (mYVelocity != 0) { |
| 149 | duration = 1000*(0 - lp.verticalOffset)/Math.abs(mYVelocity); |
| 150 | } |
| 151 | |
| 152 | duration = Math.min(duration, largestDuration); |
| 153 | duration = Math.max(duration, MINIMUM_ANIMATION_DURATION); |
| 154 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 155 | StackSlider animationSlider = new StackSlider(mStackSlider); |
| 156 | PropertyAnimator slideInY = new PropertyAnimator(duration, animationSlider, |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 157 | "YProgress", mStackSlider.getYProgress(), 0); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 158 | slideInY.setInterpolator(new LinearInterpolator()); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 159 | slideInY.start(); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 160 | PropertyAnimator slideInX = new PropertyAnimator(duration, animationSlider, |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 161 | "XProgress", mStackSlider.getXProgress(), 0); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 162 | slideInX.setInterpolator(new LinearInterpolator()); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 163 | slideInX.start(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 164 | } else if (fromIndex == mNumActiveViews - 2 && toIndex == mNumActiveViews - 1) { |
| 165 | // Slide item out |
| 166 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 167 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 168 | int largestDuration = Math.round(mStackSlider.getDurationForOffscreenPosition()* |
| 169 | DEFAULT_ANIMATION_DURATION); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 170 | int duration = largestDuration; |
| 171 | if (mYVelocity != 0) { |
| 172 | duration = 1000*(lp.verticalOffset + mViewHeight)/Math.abs(mYVelocity); |
| 173 | } |
| 174 | |
| 175 | duration = Math.min(duration, largestDuration); |
| 176 | duration = Math.max(duration, MINIMUM_ANIMATION_DURATION); |
| 177 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 178 | StackSlider animationSlider = new StackSlider(mStackSlider); |
| 179 | PropertyAnimator slideOutY = new PropertyAnimator(duration, animationSlider, |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 180 | "YProgress", mStackSlider.getYProgress(), 1); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 181 | slideOutY.setInterpolator(new LinearInterpolator()); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 182 | slideOutY.start(); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 183 | PropertyAnimator slideOutX = new PropertyAnimator(duration, animationSlider, |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 184 | "XProgress", mStackSlider.getXProgress(), 0); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 185 | slideOutX.setInterpolator(new LinearInterpolator()); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 186 | slideOutX.start(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 187 | } else if (fromIndex == -1 && toIndex == mNumActiveViews - 1) { |
| 188 | // Make sure this view that is "waiting in the wings" is invisible |
| 189 | view.setAlpha(0.0f); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 190 | view.setVisibility(INVISIBLE); |
| 191 | LayoutParams lp = (LayoutParams) view.getLayoutParams(); |
| 192 | lp.setVerticalOffset(-mViewHeight); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 193 | } else if (toIndex == -1) { |
| 194 | // Fade item out |
| 195 | PropertyAnimator fadeOut = new PropertyAnimator(DEFAULT_ANIMATION_DURATION, |
| 196 | view, "alpha", view.getAlpha(), 0); |
| 197 | fadeOut.start(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Apply any necessary tranforms for the child that is being added. |
| 203 | */ |
| 204 | void applyTransformForChildAtIndex(View child, int relativeIndex) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 205 | if (!mRotations.containsKey(child)) { |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 206 | float rotation = (float) (Math.random()*26 - 13); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 207 | mRotations.put(child, rotation); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Child has been removed |
| 211 | if (relativeIndex == -1) { |
| 212 | if (mRotations.containsKey(child)) { |
| 213 | mRotations.remove(child); |
| 214 | } |
| 215 | if (mChildrenToApplyTransformsTo.containsKey(child)) { |
| 216 | mChildrenToApplyTransformsTo.remove(child); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // if this view is already in the layout, we need to |
| 221 | // wait until layout has finished in order to set the |
| 222 | // pivot point of the rotation (requiring getMeasuredWidth/Height()) |
| 223 | mChildrenToApplyTransformsTo.put(child, relativeIndex); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 228 | super.onLayout(changed, left, top, right, bottom); |
| 229 | |
| 230 | if (!mChildrenToApplyTransformsTo.isEmpty()) { |
| 231 | for (View child: mChildrenToApplyTransformsTo.keySet()) { |
| 232 | if (mRotations.containsKey(child)) { |
| 233 | child.setPivotX(child.getMeasuredWidth()/2); |
| 234 | child.setPivotY(child.getMeasuredHeight()/2); |
| 235 | child.setRotation(mRotations.get(child)); |
| 236 | } |
| 237 | } |
| 238 | mChildrenToApplyTransformsTo.clear(); |
| 239 | } |
| 240 | |
| 241 | if (!mFirstLayoutHappened) { |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 242 | mViewHeight = Math.round(SLIDE_UP_RATIO*getMeasuredHeight()); |
| 243 | mSwipeThreshold = Math.round(SWIPE_THRESHOLD_RATIO*mViewHeight); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 244 | |
| 245 | // TODO: Right now this walks all the way up the view hierarchy and disables |
| 246 | // ClipChildren and ClipToPadding. We're probably going to want to reset |
| 247 | // these flags as well. |
| 248 | setClipChildren(false); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 249 | setClipToPadding(false); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 250 | ViewGroup view = this; |
| 251 | while (view.getParent() != null && view.getParent() instanceof ViewGroup) { |
| 252 | view = (ViewGroup) view.getParent(); |
| 253 | view.setClipChildren(false); |
| 254 | view.setClipToPadding(false); |
| 255 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 256 | mFirstLayoutHappened = true; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 262 | int action = ev.getAction(); |
| 263 | switch(action & MotionEvent.ACTION_MASK) { |
| 264 | |
| 265 | case MotionEvent.ACTION_DOWN: { |
| 266 | if (mActivePointerId == INVALID_POINTER) { |
| 267 | mInitialX = ev.getX(); |
| 268 | mInitialY = ev.getY(); |
| 269 | mActivePointerId = ev.getPointerId(0); |
| 270 | } |
| 271 | break; |
| 272 | } |
| 273 | case MotionEvent.ACTION_MOVE: { |
| 274 | int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 275 | if (pointerIndex == INVALID_POINTER) { |
| 276 | // no data for our primary pointer, this shouldn't happen, log it |
| 277 | Log.d(TAG, "Error: No data for our primary pointer."); |
| 278 | return false; |
| 279 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 280 | float newY = ev.getY(pointerIndex); |
| 281 | float deltaY = newY - mInitialY; |
| 282 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 283 | beginGestureIfNeeded(deltaY); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | case MotionEvent.ACTION_POINTER_UP: { |
| 287 | onSecondaryPointerUp(ev); |
| 288 | break; |
| 289 | } |
| 290 | case MotionEvent.ACTION_UP: |
| 291 | case MotionEvent.ACTION_CANCEL: { |
| 292 | mActivePointerId = INVALID_POINTER; |
| 293 | mSwipeGestureType = GESTURE_NONE; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
| 297 | return mSwipeGestureType != GESTURE_NONE; |
| 298 | } |
| 299 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 300 | private void beginGestureIfNeeded(float deltaY) { |
| 301 | if ((int) Math.abs(deltaY) > mTouchSlop && mSwipeGestureType == GESTURE_NONE) { |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 302 | int swipeGestureType = deltaY < 0 ? GESTURE_SLIDE_UP : GESTURE_SLIDE_DOWN; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 303 | cancelLongPress(); |
| 304 | requestDisallowInterceptTouchEvent(true); |
| 305 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 306 | int activeIndex = swipeGestureType == GESTURE_SLIDE_DOWN ? mNumActiveViews - 1 |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 307 | : mNumActiveViews - 2; |
| 308 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 309 | if (mAdapter == null) return; |
| 310 | |
| 311 | if (mCurrentWindowStartUnbounded + activeIndex == 0) { |
| 312 | mStackSlider.setMode(StackSlider.BEGINNING_OF_STACK_MODE); |
| 313 | } else if (mCurrentWindowStartUnbounded + activeIndex == mAdapter.getCount()) { |
| 314 | activeIndex--; |
| 315 | mStackSlider.setMode(StackSlider.END_OF_STACK_MODE); |
| 316 | } else { |
| 317 | mStackSlider.setMode(StackSlider.NORMAL_MODE); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 318 | } |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 319 | |
| 320 | View v = getViewAtRelativeIndex(activeIndex); |
| 321 | if (v == null) return; |
| 322 | |
| 323 | mHighlight.setImageBitmap(createOutline(v)); |
| 324 | mHighlight.bringToFront(); |
| 325 | v.bringToFront(); |
| 326 | mStackSlider.setView(v); |
| 327 | |
| 328 | if (swipeGestureType == GESTURE_SLIDE_DOWN) |
| 329 | v.setVisibility(VISIBLE); |
| 330 | |
| 331 | // We only register this gesture if we've made it this far without a problem |
| 332 | mSwipeGestureType = swipeGestureType; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 336 | @Override |
| 337 | public boolean onTouchEvent(MotionEvent ev) { |
| 338 | int action = ev.getAction(); |
| 339 | int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 340 | if (pointerIndex == INVALID_POINTER) { |
| 341 | // no data for our primary pointer, this shouldn't happen, log it |
| 342 | Log.d(TAG, "Error: No data for our primary pointer."); |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | float newY = ev.getY(pointerIndex); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 347 | float newX = ev.getX(pointerIndex); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 348 | float deltaY = newY - mInitialY; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 349 | float deltaX = newX - mInitialX; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 350 | if (mVelocityTracker == null) { |
| 351 | mVelocityTracker = VelocityTracker.obtain(); |
| 352 | } |
| 353 | mVelocityTracker.addMovement(ev); |
| 354 | |
| 355 | switch (action & MotionEvent.ACTION_MASK) { |
| 356 | case MotionEvent.ACTION_MOVE: { |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 357 | beginGestureIfNeeded(deltaY); |
| 358 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 359 | float rx = deltaX/(mViewHeight*1.0f); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 360 | if (mSwipeGestureType == GESTURE_SLIDE_DOWN) { |
| 361 | float r = (deltaY-mTouchSlop*1.0f)/mViewHeight*1.0f; |
| 362 | mStackSlider.setYProgress(1 - r); |
| 363 | mStackSlider.setXProgress(rx); |
| 364 | return true; |
| 365 | } else if (mSwipeGestureType == GESTURE_SLIDE_UP) { |
| 366 | float r = -(deltaY + mTouchSlop*1.0f)/mViewHeight*1.0f; |
| 367 | mStackSlider.setYProgress(r); |
| 368 | mStackSlider.setXProgress(rx); |
| 369 | return true; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 370 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 371 | break; |
| 372 | } |
| 373 | case MotionEvent.ACTION_UP: { |
| 374 | handlePointerUp(ev); |
| 375 | break; |
| 376 | } |
| 377 | case MotionEvent.ACTION_POINTER_UP: { |
| 378 | onSecondaryPointerUp(ev); |
| 379 | break; |
| 380 | } |
| 381 | case MotionEvent.ACTION_CANCEL: { |
| 382 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 383 | mSwipeGestureType = GESTURE_NONE; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | } |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | private final Rect touchRect = new Rect(); |
| 391 | private void onSecondaryPointerUp(MotionEvent ev) { |
| 392 | final int activePointerIndex = ev.getActionIndex(); |
| 393 | final int pointerId = ev.getPointerId(activePointerIndex); |
| 394 | if (pointerId == mActivePointerId) { |
| 395 | |
| 396 | int activeViewIndex = (mSwipeGestureType == GESTURE_SLIDE_DOWN) ? mNumActiveViews - 1 |
| 397 | : mNumActiveViews - 2; |
| 398 | |
| 399 | View v = getViewAtRelativeIndex(activeViewIndex); |
| 400 | if (v == null) return; |
| 401 | |
| 402 | // Our primary pointer has gone up -- let's see if we can find |
| 403 | // another pointer on the view. If so, then we should replace |
| 404 | // our primary pointer with this new pointer and adjust things |
| 405 | // so that the view doesn't jump |
| 406 | for (int index = 0; index < ev.getPointerCount(); index++) { |
| 407 | if (index != activePointerIndex) { |
| 408 | |
| 409 | float x = ev.getX(index); |
| 410 | float y = ev.getY(index); |
| 411 | |
| 412 | touchRect.set(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 413 | if (touchRect.contains(Math.round(x), Math.round(y))) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 414 | float oldX = ev.getX(activePointerIndex); |
| 415 | float oldY = ev.getY(activePointerIndex); |
| 416 | |
| 417 | // adjust our frame of reference to avoid a jump |
| 418 | mInitialY += (y - oldY); |
| 419 | mInitialX += (x - oldX); |
| 420 | |
| 421 | mActivePointerId = ev.getPointerId(index); |
| 422 | if (mVelocityTracker != null) { |
| 423 | mVelocityTracker.clear(); |
| 424 | } |
| 425 | // ok, we're good, we found a new pointer which is touching the active view |
| 426 | return; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | // 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^] | 431 | // so end the gesture |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 432 | handlePointerUp(ev); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | private void handlePointerUp(MotionEvent ev) { |
| 437 | int pointerIndex = ev.findPointerIndex(mActivePointerId); |
| 438 | float newY = ev.getY(pointerIndex); |
| 439 | int deltaY = (int) (newY - mInitialY); |
| 440 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 441 | if (mVelocityTracker != null) { |
| 442 | mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); |
| 443 | mYVelocity = (int) mVelocityTracker.getYVelocity(mActivePointerId); |
| 444 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 445 | |
| 446 | if (mVelocityTracker != null) { |
| 447 | mVelocityTracker.recycle(); |
| 448 | mVelocityTracker = null; |
| 449 | } |
| 450 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 451 | if (deltaY > mSwipeThreshold && mSwipeGestureType == GESTURE_SLIDE_DOWN |
| 452 | && mStackSlider.mMode == StackSlider.NORMAL_MODE) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 453 | // Swipe threshold exceeded, swipe down |
| 454 | showNext(); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 455 | mHighlight.bringToFront(); |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 456 | } else if (deltaY < -mSwipeThreshold && mSwipeGestureType == GESTURE_SLIDE_UP |
| 457 | && mStackSlider.mMode == StackSlider.NORMAL_MODE) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 458 | // Swipe threshold exceeded, swipe up |
| 459 | showPrevious(); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 460 | mHighlight.bringToFront(); |
| 461 | } else if (mSwipeGestureType == GESTURE_SLIDE_UP) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 462 | // Didn't swipe up far enough, snap back down |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 463 | int duration = |
| 464 | Math.round(mStackSlider.getDurationForNeutralPosition()*DEFAULT_ANIMATION_DURATION); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 465 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 466 | StackSlider animationSlider = new StackSlider(mStackSlider); |
| 467 | PropertyAnimator snapBackY = new PropertyAnimator(duration, animationSlider, |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 468 | "YProgress", mStackSlider.getYProgress(), 0); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 469 | snapBackY.setInterpolator(new LinearInterpolator()); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 470 | snapBackY.start(); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 471 | PropertyAnimator snapBackX = new PropertyAnimator(duration, animationSlider, |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 472 | "XProgress", mStackSlider.getXProgress(), 0); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 473 | snapBackX.setInterpolator(new LinearInterpolator()); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 474 | snapBackX.start(); |
| 475 | } else if (mSwipeGestureType == GESTURE_SLIDE_DOWN) { |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 476 | // Didn't swipe down far enough, snap back up |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 477 | int duration = Math.round(mStackSlider.getDurationForOffscreenPosition()* |
| 478 | DEFAULT_ANIMATION_DURATION); |
| 479 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 480 | StackSlider animationSlider = new StackSlider(mStackSlider); |
| 481 | PropertyAnimator snapBackY = new PropertyAnimator(duration, animationSlider, |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 482 | "YProgress", mStackSlider.getYProgress(), 1); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 483 | snapBackY.setInterpolator(new LinearInterpolator()); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 484 | snapBackY.start(); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 485 | PropertyAnimator snapBackX = new PropertyAnimator(duration, animationSlider, |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 486 | "XProgress", mStackSlider.getXProgress(), 0); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 487 | snapBackX.setInterpolator(new LinearInterpolator()); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 488 | snapBackX.start(); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | mActivePointerId = INVALID_POINTER; |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 492 | mSwipeGestureType = GESTURE_NONE; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | private class StackSlider { |
| 496 | View mView; |
| 497 | float mYProgress; |
| 498 | float mXProgress; |
| 499 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 500 | static final int NORMAL_MODE = 0; |
| 501 | static final int BEGINNING_OF_STACK_MODE = 1; |
| 502 | static final int END_OF_STACK_MODE = 2; |
| 503 | |
| 504 | int mMode = NORMAL_MODE; |
| 505 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 506 | public StackSlider() { |
| 507 | } |
| 508 | |
| 509 | public StackSlider(StackSlider copy) { |
| 510 | mView = copy.mView; |
| 511 | mYProgress = copy.mYProgress; |
| 512 | mXProgress = copy.mXProgress; |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 513 | mMode = copy.mMode; |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 516 | private float cubic(float r) { |
| 517 | return (float) (Math.pow(2*r-1, 3) + 1)/2.0f; |
| 518 | } |
| 519 | |
| 520 | private float highlightAlphaInterpolator(float r) { |
| 521 | float pivot = 0.4f; |
| 522 | if (r < pivot) { |
| 523 | return 0.85f*cubic(r/pivot); |
| 524 | } else { |
| 525 | return 0.85f*cubic(1 - (r-pivot)/(1-pivot)); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | private float viewAlphaInterpolator(float r) { |
| 530 | float pivot = 0.3f; |
| 531 | if (r > pivot) { |
| 532 | return (r - pivot)/(1 - pivot); |
| 533 | } else { |
| 534 | return 0; |
| 535 | } |
| 536 | } |
| 537 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 538 | private float rotationInterpolator(float r) { |
| 539 | float pivot = 0.2f; |
| 540 | if (r < pivot) { |
| 541 | return 0; |
| 542 | } else { |
| 543 | return (r-pivot)/(1-pivot); |
| 544 | } |
| 545 | } |
| 546 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 547 | void setView(View v) { |
| 548 | mView = v; |
| 549 | } |
| 550 | |
| 551 | public void setYProgress(float r) { |
| 552 | // enforce r between 0 and 1 |
| 553 | r = Math.min(1.0f, r); |
| 554 | r = Math.max(0, r); |
| 555 | |
| 556 | mYProgress = r; |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 557 | final LayoutParams viewLp = (LayoutParams) mView.getLayoutParams(); |
| 558 | final LayoutParams highlightLp = (LayoutParams) mHighlight.getLayoutParams(); |
| 559 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 560 | switch (mMode) { |
| 561 | case NORMAL_MODE: |
| 562 | viewLp.setVerticalOffset(Math.round(-r*mViewHeight)); |
| 563 | highlightLp.setVerticalOffset(Math.round(-r*mViewHeight)); |
| 564 | mHighlight.setAlpha(highlightAlphaInterpolator(r)); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 565 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 566 | float alpha = viewAlphaInterpolator(1-r); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 567 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 568 | // We make sure that views which can't be seen (have 0 alpha) are also invisible |
| 569 | // so that they don't interfere with click events. |
| 570 | if (mView.getAlpha() == 0 && alpha != 0 && mView.getVisibility() != VISIBLE) { |
| 571 | mView.setVisibility(VISIBLE); |
| 572 | } else if (alpha == 0 && mView.getAlpha() != 0 |
| 573 | && mView.getVisibility() == VISIBLE) { |
| 574 | mView.setVisibility(INVISIBLE); |
| 575 | } |
| 576 | |
| 577 | mView.setAlpha(alpha); |
| 578 | mView.setRotationX(90.0f*rotationInterpolator(r)); |
| 579 | mHighlight.setRotationX(90.0f*rotationInterpolator(r)); |
| 580 | break; |
| 581 | case BEGINNING_OF_STACK_MODE: |
| 582 | r = r*0.2f; |
| 583 | viewLp.setVerticalOffset(Math.round(-r*mViewHeight)); |
| 584 | highlightLp.setVerticalOffset(Math.round(-r*mViewHeight)); |
| 585 | mHighlight.setAlpha(highlightAlphaInterpolator(r)); |
| 586 | break; |
| 587 | case END_OF_STACK_MODE: |
| 588 | r = (1-r)*0.2f; |
| 589 | viewLp.setVerticalOffset(Math.round(r*mViewHeight)); |
| 590 | highlightLp.setVerticalOffset(Math.round(r*mViewHeight)); |
| 591 | mHighlight.setAlpha(highlightAlphaInterpolator(r)); |
| 592 | break; |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 593 | } |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | public void setXProgress(float r) { |
| 597 | // enforce r between 0 and 1 |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 598 | r = Math.min(2.0f, r); |
| 599 | r = Math.max(-2.0f, r); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 600 | |
| 601 | mXProgress = r; |
| 602 | |
| 603 | final LayoutParams viewLp = (LayoutParams) mView.getLayoutParams(); |
| 604 | final LayoutParams highlightLp = (LayoutParams) mHighlight.getLayoutParams(); |
| 605 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 606 | r *= 0.2f; |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 607 | viewLp.setHorizontalOffset(Math.round(r*mViewHeight)); |
| 608 | highlightLp.setHorizontalOffset(Math.round(r*mViewHeight)); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 611 | void setMode(int mode) { |
| 612 | mMode = mode; |
| 613 | } |
| 614 | |
| 615 | float getDurationForNeutralPosition() { |
| 616 | return getDuration(false); |
| 617 | } |
| 618 | |
| 619 | float getDurationForOffscreenPosition() { |
| 620 | return getDuration(mMode == END_OF_STACK_MODE ? false : true); |
| 621 | } |
| 622 | |
| 623 | private float getDuration(boolean invert) { |
| 624 | if (mView != null) { |
| 625 | final LayoutParams viewLp = (LayoutParams) mView.getLayoutParams(); |
| 626 | |
| 627 | float d = (float) Math.sqrt(Math.pow(viewLp.horizontalOffset,2) + |
| 628 | Math.pow(viewLp.verticalOffset,2)); |
| 629 | float maxd = (float) Math.sqrt(Math.pow(mViewHeight, 2) + |
| 630 | Math.pow(0.4f*mViewHeight, 2)); |
| 631 | return invert ? (1-d/maxd) : d/maxd; |
| 632 | } |
| 633 | return 0; |
| 634 | } |
| 635 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 636 | float getYProgress() { |
| 637 | return mYProgress; |
| 638 | } |
| 639 | |
| 640 | float getXProgress() { |
| 641 | return mXProgress; |
| 642 | } |
Adam Cohen | 3d07af0 | 2010-08-18 17:46:23 -0700 | [diff] [blame^] | 643 | |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | @Override |
| 647 | public void onRemoteAdapterConnected() { |
| 648 | super.onRemoteAdapterConnected(); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 649 | setDisplayedChild(mWhichChild); |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 650 | } |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 651 | |
| 652 | private static final Paint sHolographicPaint = new Paint(); |
| 653 | private static final Paint sErasePaint = new Paint(); |
| 654 | private static boolean sPaintsInitialized = false; |
| 655 | private static final float STROKE_WIDTH = 3.0f; |
| 656 | |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 657 | static void initializePaints() { |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 658 | sHolographicPaint.setColor(0xff6699ff); |
| 659 | sHolographicPaint.setFilterBitmap(true); |
| 660 | sErasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); |
| 661 | sErasePaint.setFilterBitmap(true); |
| 662 | sPaintsInitialized = true; |
| 663 | } |
| 664 | |
| 665 | static Bitmap createOutline(View v) { |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 666 | if (v.getMeasuredWidth() == 0 || v.getMeasuredHeight() == 0) { |
| 667 | return null; |
| 668 | } |
| 669 | |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 670 | Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), |
| 671 | Bitmap.Config.ARGB_8888); |
| 672 | Canvas canvas = new Canvas(bitmap); |
| 673 | |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 674 | float rotationX = v.getRotationX(); |
| 675 | v.setRotationX(0); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 676 | canvas.concat(v.getMatrix()); |
| 677 | v.draw(canvas); |
Adam Cohen | b04f7ad | 2010-08-15 13:22:42 -0700 | [diff] [blame] | 678 | v.setRotationX(rotationX); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 679 | |
| 680 | Bitmap outlineBitmap = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), |
| 681 | Bitmap.Config.ARGB_8888); |
| 682 | Canvas outlineCanvas = new Canvas(outlineBitmap); |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 683 | drawOutline(outlineCanvas, bitmap); |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 684 | bitmap.recycle(); |
| 685 | return outlineBitmap; |
| 686 | } |
| 687 | |
Romain Guy | 5b53f91 | 2010-08-16 18:24:33 -0700 | [diff] [blame] | 688 | static void drawOutline(Canvas dest, Bitmap src) { |
Adam Cohen | 32a42f1 | 2010-08-11 19:34:30 -0700 | [diff] [blame] | 689 | dest.drawColor(0, PorterDuff.Mode.CLEAR); |
| 690 | |
| 691 | Bitmap mask = src.extractAlpha(); |
| 692 | Matrix id = new Matrix(); |
| 693 | |
| 694 | Matrix m = new Matrix(); |
| 695 | float xScale = STROKE_WIDTH*2/(src.getWidth()); |
| 696 | float yScale = STROKE_WIDTH*2/(src.getHeight()); |
| 697 | m.preScale(1+xScale, 1+yScale, src.getWidth()/2, src.getHeight()/2); |
| 698 | dest.drawBitmap(mask, m, sHolographicPaint); |
| 699 | |
| 700 | dest.drawBitmap(src, id, sErasePaint); |
| 701 | mask.recycle(); |
| 702 | } |
Adam Cohen | 44729e3 | 2010-07-22 16:00:07 -0700 | [diff] [blame] | 703 | } |