blob: 8e729e431ddbeda686429ed1ae81de6802395311 [file] [log] [blame]
Michael Jurka0142d492010-08-25 17:46:15 -07001/*
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
17package com.android.launcher2;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.animation.Interpolator;
22import android.widget.Scroller;
23
24
25public abstract class SmoothPagedView extends PagedView {
26 private static final float SMOOTHING_SPEED = 0.75f;
27 private static final float SMOOTHING_CONSTANT = (float) (0.016 / Math.log(SMOOTHING_SPEED));
28
Adam Cohenf34bab52010-09-30 14:11:56 -070029 private float mBaseLineFlingVelocity;
30 private float mFlingVelocityInfluence;
Michael Jurka0142d492010-08-25 17:46:15 -070031
Adam Cohenf34bab52010-09-30 14:11:56 -070032 static final int OVERSHOOT_MODE = 0;
33 static final int QUINTIC_MODE = 1;
Michael Jurka0142d492010-08-25 17:46:15 -070034
Adam Cohenf34bab52010-09-30 14:11:56 -070035 int mScrollMode;
36
37 private Interpolator mScrollInterpolator;
Michael Jurka0142d492010-08-25 17:46:15 -070038
39 private static class WorkspaceOvershootInterpolator implements Interpolator {
40 private static final float DEFAULT_TENSION = 1.3f;
41 private float mTension;
42
43 public WorkspaceOvershootInterpolator() {
44 mTension = DEFAULT_TENSION;
45 }
46
47 public void setDistance(int distance) {
48 mTension = distance > 0 ? DEFAULT_TENSION / distance : DEFAULT_TENSION;
49 }
50
51 public void disableSettle() {
52 mTension = 0.f;
53 }
54
55 public float getInterpolation(float t) {
56 // _o(t) = t * t * ((tension + 1) * t + tension)
57 // o(t) = _o(t - 1) + 1
58 t -= 1.0f;
59 return t * t * ((mTension + 1) * t + mTension) + 1.0f;
60 }
61 }
62
Adam Cohenf34bab52010-09-30 14:11:56 -070063 private static class QuinticInterpolator implements Interpolator {
64 public QuinticInterpolator() {
65 }
66
67 public float getInterpolation(float t) {
68 t -= 1.0f;
69 return t*t*t*t*t + 1;
70 }
71 }
72
Michael Jurka0142d492010-08-25 17:46:15 -070073 /**
74 * Used to inflate the Workspace from XML.
75 *
76 * @param context The application's context.
77 * @param attrs The attributes set containing the Workspace's customization values.
78 */
79 public SmoothPagedView(Context context, AttributeSet attrs) {
80 this(context, attrs, 0);
81 }
82
83 /**
84 * Used to inflate the Workspace from XML.
85 *
86 * @param context The application's context.
87 * @param attrs The attributes set containing the Workspace's customization values.
88 * @param defStyle Unused.
89 */
90 public SmoothPagedView(Context context, AttributeSet attrs, int defStyle) {
91 super(context, attrs, defStyle);
92
93 mUsePagingTouchSlop = false;
94
95 // This means that we'll take care of updating the scroll parameter ourselves (we do it
96 // in computeScroll)
97 mDeferScrollUpdate = true;
98 }
99
Adam Cohenf34bab52010-09-30 14:11:56 -0700100 protected int getScrollMode() {
101 return OVERSHOOT_MODE;
102 }
103
Michael Jurka0142d492010-08-25 17:46:15 -0700104 /**
105 * Initializes various states for this workspace.
106 */
107 @Override
108 protected void init() {
109 super.init();
Adam Cohenf34bab52010-09-30 14:11:56 -0700110
111 mScrollMode = getScrollMode();
112 if (mScrollMode == QUINTIC_MODE) {
113 mBaseLineFlingVelocity = 700.0f;
114 mFlingVelocityInfluence = 0.8f;
115 mScrollInterpolator = new QuinticInterpolator();
116 } else { // QUINTIC_MODE
117 mBaseLineFlingVelocity = 2500.0f;
118 mFlingVelocityInfluence = 0.4f;
119 mScrollInterpolator = new WorkspaceOvershootInterpolator();
120 }
Michael Jurka0142d492010-08-25 17:46:15 -0700121 mScroller = new Scroller(getContext(), mScrollInterpolator);
122 }
123
124 @Override
125 protected void snapToDestination() {
Adam Cohend19d3ca2010-09-15 14:43:42 -0700126 snapToPageWithVelocity(getPageNearestToCenterOfScreen(), 0);
Michael Jurka0142d492010-08-25 17:46:15 -0700127 }
128
129 @Override
130 protected void snapToPageWithVelocity(int whichPage, int velocity) {
131 snapToPageWithVelocity(whichPage, 0, true);
132 }
133
Patrick Dubroy54fa3b92010-11-17 12:18:45 -0800134 private void snapToPageWithVelocity(int whichPage, int velocity, boolean settle) {
Michael Jurka0142d492010-08-25 17:46:15 -0700135 // if (!mScroller.isFinished()) return;
136
137 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
138
139 final int screenDelta = Math.max(1, Math.abs(whichPage - mCurrentPage));
Michael Jurka5f1c5092010-09-03 14:15:02 -0700140 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700141 final int delta = newX - mScrollX;
Adam Cohenf34bab52010-09-30 14:11:56 -0700142 int duration;
143 if (mScrollMode == OVERSHOOT_MODE) {
144 duration = (screenDelta + 1) * 100;
145 } else { // QUINTIC_MODE
146 duration = Math.round(Math.abs(delta) * 0.6f);
147 }
Michael Jurka0142d492010-08-25 17:46:15 -0700148
149 if (!mScroller.isFinished()) {
150 mScroller.abortAnimation();
151 }
152
Adam Cohenf34bab52010-09-30 14:11:56 -0700153 if (mScrollMode == OVERSHOOT_MODE) {
154 if (settle) {
155 ((WorkspaceOvershootInterpolator) mScrollInterpolator).setDistance(screenDelta);
156 } else {
157 ((WorkspaceOvershootInterpolator) mScrollInterpolator).disableSettle();
158 }
Michael Jurka0142d492010-08-25 17:46:15 -0700159 }
160
161 velocity = Math.abs(velocity);
162 if (velocity > 0) {
Adam Cohenf34bab52010-09-30 14:11:56 -0700163 duration += (duration / (velocity / mBaseLineFlingVelocity)) * mFlingVelocityInfluence;
Michael Jurka0142d492010-08-25 17:46:15 -0700164 } else {
165 duration += 100;
166 }
Adam Cohenf34bab52010-09-30 14:11:56 -0700167
Michael Jurka0142d492010-08-25 17:46:15 -0700168 snapToPage(whichPage, delta, duration);
169 }
170
171 @Override
172 protected void snapToPage(int whichPage) {
173 snapToPageWithVelocity(whichPage, 0, false);
174 }
175
176 @Override
177 public void computeScroll() {
178 boolean scrollComputed = computeScrollHelper();
179
180 if (!scrollComputed && mTouchState == TOUCH_STATE_SCROLLING) {
181 final float now = System.nanoTime() / NANOTIME_DIV;
182 final float e = (float) Math.exp((now - mSmoothingTime) / SMOOTHING_CONSTANT);
183 final float dx = mTouchX - mScrollX;
184 mScrollX += dx * e;
185 mSmoothingTime = now;
186
187 // Keep generating points as long as we're more than 1px away from the target
188 if (dx > 1.f || dx < -1.f) {
189 invalidate();
190 }
191 }
192 }
193}