Adding overscroll to Workspace, AllApps and Customize

-Also modified the background alpha interpolation during scrolling

Change-Id: I68db8fbe80f720c507ab3208d8b3dcd8aa4f2081
This commit is contained in:
Adam Cohen
2010-11-15 10:50:58 -08:00
parent c0bd58837a
commit 68d739365b
3 changed files with 101 additions and 41 deletions
+47 -31
View File
@@ -27,7 +27,6 @@ import android.graphics.Rect;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ActionMode;
import android.view.MotionEvent;
import android.view.VelocityTracker;
@@ -36,10 +35,9 @@ import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Checkable;
import android.widget.LinearLayout;
import android.widget.Scroller;
import com.android.launcher.R;
@@ -60,6 +58,8 @@ public abstract class PagedView extends ViewGroup {
private static final int PAGE_SNAP_ANIMATION_DURATION = 750;
protected static final float NANOTIME_DIV = 1000000000.0f;
private static final float OVERSCROLL_DAMP_FACTOR = 0.22f;
// the velocity at which a fling gesture will cause us to snap to the next page
protected int mSnapVelocity = 500;
@@ -70,6 +70,7 @@ public abstract class PagedView extends ViewGroup {
protected int mCurrentPage;
protected int mNextPage = INVALID_PAGE;
protected int mMaxScrollX;
protected Scroller mScroller;
private VelocityTracker mVelocityTracker;
@@ -103,6 +104,8 @@ public abstract class PagedView extends ViewGroup {
protected int mCellCountX;
protected int mCellCountY;
protected boolean mCenterPagesVertically;
protected boolean mAllowOverScroll = true;
protected int mUnboundedScrollX;
protected static final int INVALID_POINTER = -1;
@@ -311,9 +314,29 @@ public abstract class PagedView extends ViewGroup {
}
}
@Override
public void scrollBy(int x, int y) {
scrollTo(mUnboundedScrollX + x, mScrollY + y);
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
mUnboundedScrollX = x;
if (x < 0) {
super.scrollTo(0, y);
if (mAllowOverScroll) {
overScroll(x);
}
} else if (x > mMaxScrollX) {
super.scrollTo(mMaxScrollX, y);
if (mAllowOverScroll) {
overScroll(x - mMaxScrollX);
}
} else {
super.scrollTo(x, y);
}
mTouchX = x;
mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
}
@@ -395,6 +418,7 @@ public abstract class PagedView extends ViewGroup {
heightSize = maxChildHeight + verticalPadding;
}
mMaxScrollX = getChildOffset(childCount - 1) - getRelativeChildOffset(childCount - 1);
setMeasuredDimension(widthSize, heightSize);
}
@@ -799,6 +823,16 @@ public abstract class PagedView extends ViewGroup {
return false;
}
protected void overScroll(float amount) {
int overScrollAmount = (int) Math.round(OVERSCROLL_DAMP_FACTOR * amount);
if (amount < 0) {
mScrollX = overScrollAmount;
} else {
mScrollX = mMaxScrollX + overScrollAmount;
}
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// Skip touch handling if there are no pages to swipe
@@ -834,31 +868,13 @@ public abstract class PagedView extends ViewGroup {
final int deltaX = (int) (mLastMotionX - x);
mLastMotionX = x;
int sx = getScrollX();
if (deltaX < 0) {
if (sx > 0) {
mTouchX += Math.max(-mTouchX, deltaX);
mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
if (!mDeferScrollUpdate) {
scrollBy(Math.max(-sx, deltaX), 0);
} else {
// This will trigger a call to computeScroll() on next drawChild() call
invalidate();
}
}
} else if (deltaX > 0) {
final int lastChildIndex = getChildCount() - 1;
final int availableToScroll = getChildOffset(lastChildIndex) -
getRelativeChildOffset(lastChildIndex) - sx;
if (availableToScroll > 0) {
mTouchX += Math.min(availableToScroll, deltaX);
mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
if (!mDeferScrollUpdate) {
scrollBy(Math.min(availableToScroll, deltaX), 0);
} else {
// This will trigger a call to computeScroll() on next drawChild() call
invalidate();
}
if (deltaX != 0) {
mTouchX += deltaX;
mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
if (!mDeferScrollUpdate) {
scrollBy(deltaX, 0);
} else {
invalidate();
}
} else {
awakenScrollBars();
@@ -1042,7 +1058,7 @@ public abstract class PagedView extends ViewGroup {
whichPage = Math.max(0, Math.min(whichPage, getPageCount() - 1));
int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
final int sX = getScrollX();
final int sX = mUnboundedScrollX;
final int delta = newX - sX;
snapToPage(whichPage, delta, duration);
}
@@ -1063,7 +1079,7 @@ public abstract class PagedView extends ViewGroup {
}
if (!mScroller.isFinished()) mScroller.abortAnimation();
mScroller.startScroll(getScrollX(), 0, delta, 0, duration);
mScroller.startScroll(mUnboundedScrollX, 0, delta, 0, duration);
// only load some associated pages
loadAssociatedPages(mNextPage);
@@ -138,7 +138,7 @@ public abstract class SmoothPagedView extends PagedView {
final int screenDelta = Math.max(1, Math.abs(whichPage - mCurrentPage));
final int newX = getChildOffset(whichPage) - getRelativeChildOffset(whichPage);
final int delta = newX - mScrollX;
final int delta = newX - mUnboundedScrollX;
int duration;
if (mScrollMode == OVERSHOOT_MODE) {
duration = (screenDelta + 1) * 100;
@@ -180,8 +180,9 @@ public abstract class SmoothPagedView extends PagedView {
if (!scrollComputed && mTouchState == TOUCH_STATE_SCROLLING) {
final float now = System.nanoTime() / NANOTIME_DIV;
final float e = (float) Math.exp((now - mSmoothingTime) / SMOOTHING_CONSTANT);
final float dx = mTouchX - mScrollX;
mScrollX += dx * e;
final float dx = mTouchX - mUnboundedScrollX;
scrollTo(Math.round(mUnboundedScrollX + dx * e), mScrollY);
mSmoothingTime = now;
// Keep generating points as long as we're more than 1px away from the target
@@ -189,5 +190,6 @@ public abstract class SmoothPagedView extends PagedView {
invalidate();
}
}
}
}
+49 -7
View File
@@ -20,14 +20,13 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import android.R.integer;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.animation.Animator.AnimatorListener;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.app.AlertDialog;
import android.app.WallpaperManager;
@@ -92,8 +91,8 @@ public class Workspace extends SmoothPagedView
private static final float EXTRA_SCALE_FACTOR_1 = 1.0f;
private static final float EXTRA_SCALE_FACTOR_2 = 1.10f;
private static final int CHILDREN_OUTLINE_FADE_OUT_DELAY = 300;
private static final int CHILDREN_OUTLINE_FADE_OUT_DURATION = 300;
private static final int CHILDREN_OUTLINE_FADE_OUT_DELAY = 0;
private static final int CHILDREN_OUTLINE_FADE_OUT_DURATION = 375;
private static final int CHILDREN_OUTLINE_FADE_IN_DURATION = 100;
private static final int BACKGROUND_FADE_OUT_DURATION = 450;
@@ -109,6 +108,7 @@ public class Workspace extends SmoothPagedView
private ObjectAnimator mBackgroundFadeOutAnimation;
private Drawable mBackground;
private float mBackgroundAlpha = 0;
private float mOverScrollMaxBackgroundAlpha = 0.0f;
private final WallpaperManager mWallpaperManager;
@@ -515,7 +515,7 @@ public class Workspace extends SmoothPagedView
mAnimOnPageEndMoving.start();
mAnimOnPageEndMoving = null;
}
mOverScrollMaxBackgroundAlpha = 0.0f;
mPageMoving = false;
}
@@ -638,9 +638,51 @@ public class Workspace extends SmoothPagedView
return (width - mTempFloat2[0]) * (degrees > 0.0f ? 1.0f : -1.0f);
}
float backgroundAlphaInterpolator(float r) {
float pivotA = 0.1f;
float pivotB = 0.4f;
if (r < pivotA) {
return 0;
} else if (r > pivotB) {
return 1.0f;
} else {
return (r - pivotA)/(pivotB - pivotA);
}
}
float overScrollBackgroundAlphaInterpolator(float r) {
float threshold = 0.1f;
if (r > mOverScrollMaxBackgroundAlpha) {
mOverScrollMaxBackgroundAlpha = r;
} else if (r < mOverScrollMaxBackgroundAlpha) {
r = mOverScrollMaxBackgroundAlpha;
}
return Math.min(r / threshold, 1.0f);
}
protected void overScroll(float amount) {
final int lastChildIndex = getChildCount() - 1;
CellLayout cl;
if (amount < 0) {
cl = (CellLayout) getChildAt(0);
} else {
cl = (CellLayout) getChildAt(lastChildIndex);
}
final int totalDistance = cl.getMeasuredWidth() + mPageSpacing;
float r = 1.0f * amount / totalDistance;
float rotation = -WORKSPACE_ROTATION * r;
cl.setBackgroundAlphaMultiplier(overScrollBackgroundAlphaInterpolator(Math.abs(r)));
cl.setRotationY(rotation);
}
@Override
protected void screenScrolled(int screenCenter) {
final int halfScreenSize = getMeasuredWidth() / 2;
for (int i = 0; i < getChildCount(); i++) {
CellLayout cl = (CellLayout) getChildAt(i);
if (cl != null) {
@@ -652,11 +694,12 @@ public class Workspace extends SmoothPagedView
scrollProgress = Math.min(scrollProgress, 1.0f);
scrollProgress = Math.max(scrollProgress, -1.0f);
cl.setBackgroundAlphaMultiplier(Math.abs(scrollProgress));
cl.setBackgroundAlphaMultiplier(backgroundAlphaInterpolator(Math.abs(scrollProgress)));
float rotation = WORKSPACE_ROTATION * scrollProgress;
float translationX = getOffsetXForRotation(rotation, cl.getWidth(), cl.getHeight());
cl.setTranslationX(translationX);
cl.setRotationY(rotation);
}
}
@@ -1431,7 +1474,6 @@ public class Workspace extends SmoothPagedView
}
});
view.setVisibility(View.INVISIBLE);
if (!mScroller.isFinished()) {