blob: 73e147a220041b72db0636bedc837a094089e7bd [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
29
30 private static final float BASELINE_FLING_VELOCITY = 2500.f;
31 private static final float FLING_VELOCITY_INFLUENCE = 0.4f;
32
33 private WorkspaceOvershootInterpolator mScrollInterpolator;
34
35 private static class WorkspaceOvershootInterpolator implements Interpolator {
36 private static final float DEFAULT_TENSION = 1.3f;
37 private float mTension;
38
39 public WorkspaceOvershootInterpolator() {
40 mTension = DEFAULT_TENSION;
41 }
42
43 public void setDistance(int distance) {
44 mTension = distance > 0 ? DEFAULT_TENSION / distance : DEFAULT_TENSION;
45 }
46
47 public void disableSettle() {
48 mTension = 0.f;
49 }
50
51 public float getInterpolation(float t) {
52 // _o(t) = t * t * ((tension + 1) * t + tension)
53 // o(t) = _o(t - 1) + 1
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
82 // in computeScroll)
83 mDeferScrollUpdate = true;
84 }
85
86 /**
87 * Initializes various states for this workspace.
88 */
89 @Override
90 protected void init() {
91 super.init();
92 mScrollInterpolator = new WorkspaceOvershootInterpolator();
93 // overwrite the previous mScroller
94 mScroller = new Scroller(getContext(), mScrollInterpolator);
95 }
96
97 @Override
98 protected void snapToDestination() {
99 snapToPageWithVelocity(mCurrentPage, 0);
100 }
101
102 @Override
103 protected void snapToPageWithVelocity(int whichPage, int velocity) {
104 snapToPageWithVelocity(whichPage, 0, true);
105 }
106
107 void snapToPageWithVelocity(int whichPage, int velocity, boolean settle) {
108 // if (!mScroller.isFinished()) return;
109
110 whichPage = Math.max(0, Math.min(whichPage, getChildCount() - 1));
111
112 final int screenDelta = Math.max(1, Math.abs(whichPage - mCurrentPage));
Michael Jurka5f1c5092010-09-03 14:15:02 -0700113 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
Michael Jurka0142d492010-08-25 17:46:15 -0700114 final int delta = newX - mScrollX;
115 int duration = (screenDelta + 1) * 100;
116
117 if (!mScroller.isFinished()) {
118 mScroller.abortAnimation();
119 }
120
121 if (settle) {
122 mScrollInterpolator.setDistance(screenDelta);
123 } else {
124 mScrollInterpolator.disableSettle();
125 }
126
127 velocity = Math.abs(velocity);
128 if (velocity > 0) {
129 duration += (duration / (velocity / BASELINE_FLING_VELOCITY))
130 * FLING_VELOCITY_INFLUENCE;
131 } else {
132 duration += 100;
133 }
134 snapToPage(whichPage, delta, duration);
135 }
136
137 @Override
138 protected void snapToPage(int whichPage) {
139 snapToPageWithVelocity(whichPage, 0, false);
140 }
141
142 @Override
143 public void computeScroll() {
144 boolean scrollComputed = computeScrollHelper();
145
146 if (!scrollComputed && mTouchState == TOUCH_STATE_SCROLLING) {
147 final float now = System.nanoTime() / NANOTIME_DIV;
148 final float e = (float) Math.exp((now - mSmoothingTime) / SMOOTHING_CONSTANT);
149 final float dx = mTouchX - mScrollX;
150 mScrollX += dx * e;
151 mSmoothingTime = now;
152
153 // Keep generating points as long as we're more than 1px away from the target
154 if (dx > 1.f || dx < -1.f) {
155 invalidate();
156 }
157 }
158 }
159}