Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.view.animation.Interpolator; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 22 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 23 | public abstract class SmoothPagedView extends PagedView { |
| 24 | private static final float SMOOTHING_SPEED = 0.75f; |
| 25 | private static final float SMOOTHING_CONSTANT = (float) (0.016 / Math.log(SMOOTHING_SPEED)); |
| 26 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 27 | private float mBaseLineFlingVelocity; |
| 28 | private float mFlingVelocityInfluence; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 29 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 30 | static final int DEFAULT_MODE = 0; |
| 31 | static final int X_LARGE_MODE = 1; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 32 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 33 | int mScrollMode; |
| 34 | |
| 35 | private Interpolator mScrollInterpolator; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 36 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 37 | public static class OvershootInterpolator implements Interpolator { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 38 | private static final float DEFAULT_TENSION = 1.3f; |
| 39 | private float mTension; |
| 40 | |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 41 | public OvershootInterpolator() { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 42 | mTension = DEFAULT_TENSION; |
| 43 | } |
| 44 | |
| 45 | public void setDistance(int distance) { |
| 46 | mTension = distance > 0 ? DEFAULT_TENSION / distance : DEFAULT_TENSION; |
| 47 | } |
| 48 | |
| 49 | public void disableSettle() { |
| 50 | mTension = 0.f; |
| 51 | } |
| 52 | |
| 53 | public float getInterpolation(float t) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 54 | t -= 1.0f; |
| 55 | return t * t * ((mTension + 1) * t + mTension) + 1.0f; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Used to inflate the Workspace from XML. |
| 61 | * |
| 62 | * @param context The application's context. |
| 63 | * @param attrs The attributes set containing the Workspace's customization values. |
| 64 | */ |
| 65 | public SmoothPagedView(Context context, AttributeSet attrs) { |
| 66 | this(context, attrs, 0); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Used to inflate the Workspace from XML. |
| 71 | * |
| 72 | * @param context The application's context. |
| 73 | * @param attrs The attributes set containing the Workspace's customization values. |
| 74 | * @param defStyle Unused. |
| 75 | */ |
| 76 | public SmoothPagedView(Context context, AttributeSet attrs, int defStyle) { |
| 77 | super(context, attrs, defStyle); |
| 78 | |
| 79 | mUsePagingTouchSlop = false; |
| 80 | |
| 81 | // This means that we'll take care of updating the scroll parameter ourselves (we do it |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 82 | // in computeScroll), we only do this in the OVERSHOOT_MODE, ie. on phones |
| 83 | mDeferScrollUpdate = mScrollMode != X_LARGE_MODE; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 86 | protected int getScrollMode() { |
Winson Chung | b26f3d6 | 2011-06-02 10:49:29 -0700 | [diff] [blame] | 87 | return X_LARGE_MODE; |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Initializes various states for this workspace. |
| 92 | */ |
| 93 | @Override |
| 94 | protected void init() { |
| 95 | super.init(); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 96 | |
| 97 | mScrollMode = getScrollMode(); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 98 | if (mScrollMode == DEFAULT_MODE) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 99 | mBaseLineFlingVelocity = 2500.0f; |
| 100 | mFlingVelocityInfluence = 0.4f; |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 101 | mScrollInterpolator = new OvershootInterpolator(); |
Adam Cohen | f961885 | 2013-11-08 06:45:03 -0800 | [diff] [blame] | 102 | setDefaultInterpolator(mScrollInterpolator); |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 103 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Override |
| 107 | protected void snapToDestination() { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 108 | if (mScrollMode == X_LARGE_MODE) { |
| 109 | super.snapToDestination(); |
| 110 | } else { |
| 111 | snapToPageWithVelocity(getPageNearestToCenterOfScreen(), 0); |
| 112 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | protected void snapToPageWithVelocity(int whichPage, int velocity) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 117 | if (mScrollMode == X_LARGE_MODE) { |
| 118 | super.snapToPageWithVelocity(whichPage, velocity); |
| 119 | } else { |
| 120 | snapToPageWithVelocity(whichPage, 0, true); |
| 121 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Patrick Dubroy | 54fa3b9 | 2010-11-17 12:18:45 -0800 | [diff] [blame] | 124 | private void snapToPageWithVelocity(int whichPage, int velocity, boolean settle) { |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 125 | // if (!mScroller.isFinished()) return; |
| 126 | |
| 127 | whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1)); |
| 128 | |
| 129 | final int screenDelta = Math.max(1, Math.abs(whichPage - mCurrentPage)); |
Adam Cohen | edb4076 | 2013-07-18 16:45:45 -0700 | [diff] [blame] | 130 | final int newX = getScrollForPage(whichPage); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 131 | final int delta = newX - mUnboundedScrollX; |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 132 | int duration = (screenDelta + 1) * 100; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 133 | |
| 134 | if (!mScroller.isFinished()) { |
| 135 | mScroller.abortAnimation(); |
| 136 | } |
| 137 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 138 | if (settle) { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 139 | ((OvershootInterpolator) mScrollInterpolator).setDistance(screenDelta); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 140 | } else { |
Winson Chung | f0c6ae0 | 2012-03-21 16:10:31 -0700 | [diff] [blame] | 141 | ((OvershootInterpolator) mScrollInterpolator).disableSettle(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | velocity = Math.abs(velocity); |
| 145 | if (velocity > 0) { |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 146 | duration += (duration / (velocity / mBaseLineFlingVelocity)) * mFlingVelocityInfluence; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 147 | } else { |
| 148 | duration += 100; |
| 149 | } |
Adam Cohen | f34bab5 | 2010-09-30 14:11:56 -0700 | [diff] [blame] | 150 | |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 151 | snapToPage(whichPage, delta, duration); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected void snapToPage(int whichPage) { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 156 | if (mScrollMode == X_LARGE_MODE) { |
| 157 | super.snapToPage(whichPage); |
| 158 | } else { |
| 159 | snapToPageWithVelocity(whichPage, 0, false); |
| 160 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void computeScroll() { |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 165 | if (mScrollMode == X_LARGE_MODE) { |
| 166 | super.computeScroll(); |
| 167 | } else { |
| 168 | boolean scrollComputed = computeScrollHelper(); |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 169 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 170 | if (!scrollComputed && mTouchState == TOUCH_STATE_SCROLLING) { |
| 171 | final float now = System.nanoTime() / NANOTIME_DIV; |
| 172 | final float e = (float) Math.exp((now - mSmoothingTime) / SMOOTHING_CONSTANT); |
Adam Cohen | 68d7393 | 2010-11-15 10:50:58 -0800 | [diff] [blame] | 173 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 174 | final float dx = mTouchX - mUnboundedScrollX; |
Michael Jurka | 8b805b1 | 2012-04-18 14:23:14 -0700 | [diff] [blame] | 175 | scrollTo(Math.round(mUnboundedScrollX + dx * e), getScrollY()); |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 176 | mSmoothingTime = now; |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 177 | |
Adam Cohen | e0f66b5 | 2010-11-23 15:06:07 -0800 | [diff] [blame] | 178 | // Keep generating points as long as we're more than 1px away from the target |
| 179 | if (dx > 1.f || dx < -1.f) { |
| 180 | invalidate(); |
| 181 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
Michael Jurka | 0142d49 | 2010-08-25 17:46:15 -0700 | [diff] [blame] | 184 | } |
| 185 | } |