SmoothPagedView.java revision 9415d87eda0cf28b8df1eccde39b0ca1646be3b9
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 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 private float mBaseLineFlingVelocity; 30 private float mFlingVelocityInfluence; 31 32 static final int OVERSHOOT_MODE = 0; 33 static final int QUINTIC_MODE = 1; 34 35 int mScrollMode; 36 37 private Interpolator mScrollInterpolator; 38 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 63 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 73 /** 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 100 protected int getScrollMode() { 101 return OVERSHOOT_MODE; 102 } 103 104 /** 105 * Initializes various states for this workspace. 106 */ 107 @Override 108 protected void init() { 109 super.init(); 110 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 } 121 mScroller = new Scroller(getContext(), mScrollInterpolator); 122 } 123 124 @Override 125 protected void snapToDestination() { 126 snapToPageWithVelocity(getPageNearestToCenterOfScreen(), 0); 127 } 128 129 @Override 130 protected void snapToPageWithVelocity(int whichPage, int velocity) { 131 snapToPageWithVelocity(whichPage, 0, true); 132 } 133 134 void snapToPageWithVelocity(int whichPage, int velocity, boolean settle) { 135 // 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)); 140 final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage); 141 final int delta = newX - mScrollX; 142 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 } 148 149 if (!mScroller.isFinished()) { 150 mScroller.abortAnimation(); 151 } 152 153 if (mScrollMode == OVERSHOOT_MODE) { 154 if (settle) { 155 ((WorkspaceOvershootInterpolator) mScrollInterpolator).setDistance(screenDelta); 156 } else { 157 ((WorkspaceOvershootInterpolator) mScrollInterpolator).disableSettle(); 158 } 159 } 160 161 velocity = Math.abs(velocity); 162 if (velocity > 0) { 163 duration += (duration / (velocity / mBaseLineFlingVelocity)) * mFlingVelocityInfluence; 164 } else { 165 duration += 100; 166 } 167 168 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} 194