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