summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Jurka <mikejurka@google.com>2011-01-05 18:28:32 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2011-01-05 18:28:32 -0800
commit7c2a0841f32a589d0739f6e540705332af32a9ea (patch)
tree50a00e89a15f2ac41ed763e72af332e73ab391ab
parent0499834db3f9dc6fb0f5f57b5876b8503bce5189 (diff)
parentbea15195346bab3c52b0156e92f2b71f0811b210 (diff)
downloadandroid_packages_apps_Trebuchet-7c2a0841f32a589d0739f6e540705332af32a9ea.tar.gz
android_packages_apps_Trebuchet-7c2a0841f32a589d0739f6e540705332af32a9ea.tar.bz2
android_packages_apps_Trebuchet-7c2a0841f32a589d0739f6e540705332af32a9ea.zip
Merge "caching mini screens to bitmaps"
-rw-r--r--res/values-xlarge/config.xml6
-rw-r--r--src/com/android/launcher2/CellLayout.java129
-rw-r--r--src/com/android/launcher2/Workspace.java32
3 files changed, 160 insertions, 7 deletions
diff --git a/res/values-xlarge/config.xml b/res/values-xlarge/config.xml
index d8ed2146f..8a103a36c 100644
--- a/res/values-xlarge/config.xml
+++ b/res/values-xlarge/config.xml
@@ -54,6 +54,12 @@
<integer name="config_screenOnDropAlphaFadeDelay">350</integer>
<integer name="config_screenOnDropAlphaFadeDuration">50</integer>
+ <!-- Workspace screens are cached to bitmaps only when they're smaller than a certain size
+ (maxScaleForUsingWorkspaceScreenBitmapCache), since the bitmap cache itself is smaller
+ than the view itself (workspaceScreenBitmapCacheScale) -->
+ <integer name="config_workspaceScreenBitmapCacheScale">20</integer>
+ <integer name="config_maxScaleForUsingWorkspaceScreenBitmapCache">50</integer>
+
<!-- The slope, in percent, of the drag movement needed to drag an item out of the customization
drawer (y / x * 100%) -->
<integer name="config_customizationDrawerDragSlopeThreshold">150</integer>
diff --git a/src/com/android/launcher2/CellLayout.java b/src/com/android/launcher2/CellLayout.java
index 70fe8f987..53a584e0b 100644
--- a/src/com/android/launcher2/CellLayout.java
+++ b/src/com/android/launcher2/CellLayout.java
@@ -37,6 +37,8 @@ import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
+import android.graphics.Bitmap.Config;
+import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
@@ -97,6 +99,18 @@ public class CellLayout extends ViewGroup implements Dimmable {
private float mGlowBackgroundScale;
private float mGlowBackgroundAlpha;
+ private Bitmap mCache;
+ private Canvas mCacheCanvas;
+ private Rect mCacheRect;
+ private Paint mCachePaint;
+
+ private boolean mIsCacheEnabled = true;
+ private boolean mDisableCacheUpdates = false;
+ private boolean mForceCacheUpdate = false;
+ private boolean mIsCacheDirty = true;
+ private float mBitmapCacheScale;
+ private float mMaxScaleForUsingBitmapCache;
+
private boolean mAcceptsDrops = false;
// If we're actively dragging something over this screen, mIsDragOverlapping is true
private boolean mIsDragOverlapping = false;
@@ -265,8 +279,15 @@ public class CellLayout extends ViewGroup implements Dimmable {
mBackgroundRect = new Rect();
mGlowBackgroundRect = new Rect();
+ mCacheRect = new Rect();
setHoverScale(1.0f);
setHoverAlpha(1.0f);
+
+ mBitmapCacheScale =
+ res.getInteger(R.integer.config_workspaceScreenBitmapCacheScale) / 100.0f;
+ mMaxScaleForUsingBitmapCache =
+ res.getInteger(R.integer.config_maxScaleForUsingWorkspaceScreenBitmapCache) / 100.0f;
+ mCacheCanvas = new Canvas();
}
public void setIsDefaultDropTarget(boolean isDefaultDropTarget) {
@@ -276,13 +297,6 @@ public class CellLayout extends ViewGroup implements Dimmable {
}
}
- public void setAcceptsDrops(boolean acceptsDrops) {
- if (mAcceptsDrops != acceptsDrops) {
- mAcceptsDrops = acceptsDrops;
- invalidate();
- }
- }
-
void setIsDragOccuring(boolean isDragOccuring) {
if (mIsDragOccuring != isDragOccuring) {
mIsDragOccuring = isDragOccuring;
@@ -366,6 +380,93 @@ public class CellLayout extends ViewGroup implements Dimmable {
super.dispatchDraw(canvas);
}
+ private void invalidateIfNeeded() {
+ if (mIsCacheDirty) {
+ // Force a redraw to update the cache if it's dirty
+ invalidate();
+ }
+ }
+
+ public void enableCache() {
+ mIsCacheEnabled = true;
+ invalidateIfNeeded();
+ }
+
+ public void disableCache() {
+ mIsCacheEnabled = false;
+ }
+
+ public void disableCacheUpdates() {
+ mDisableCacheUpdates = true;
+ // Force just one update before we enter a period of no cache updates
+ mForceCacheUpdate = true;
+ }
+
+ public void enableCacheUpdates() {
+ mDisableCacheUpdates = false;
+ invalidateIfNeeded();
+ }
+
+ private void invalidateCache() {
+ mIsCacheDirty = true;
+ invalidateIfNeeded();
+ }
+
+ public void updateCache() {
+ mCacheCanvas.drawColor(0x00000000, Mode.CLEAR);
+
+ float alpha = getAlpha();
+ setAlpha(1.0f);
+ drawChildren(mCacheCanvas);
+ setAlpha(alpha);
+
+ mIsCacheDirty = false;
+ }
+
+ public void dispatchDraw(Canvas canvas) {
+ final int count = getChildCount();
+
+ if (!mIsCacheDirty) {
+ // Check if one of the children (an icon or widget) is dirty
+ for (int i = 0; i < count; i++) {
+ final View child = getChildAt(i);
+ if (child.isDirty()) {
+ mIsCacheDirty = true;
+ break;
+ }
+ }
+ }
+
+ if (mForceCacheUpdate ||
+ (mIsCacheEnabled && !mDisableCacheUpdates)) {
+ // Sometimes we force a cache update-- this is used to make sure the cache will look as
+ // up-to-date as possible right when we disable cache updates
+ if (mIsCacheDirty) {
+ updateCache();
+ }
+ mForceCacheUpdate = false;
+ }
+
+ if (mIsCacheEnabled && getScaleX() < mMaxScaleForUsingBitmapCache) {
+ mCachePaint.setAlpha((int)(255*getAlpha()));
+ canvas.drawBitmap(mCache, mCacheRect, mBackgroundRect, mCachePaint);
+ } else {
+ super.dispatchDraw(canvas);
+ }
+ }
+
+ private void prepareCacheBitmap() {
+ if (mCache == null) {
+ mCache = Bitmap.createBitmap((int) (getWidth() * mBitmapCacheScale),
+ (int) (getHeight() * mBitmapCacheScale), Config.ARGB_8888);
+
+ mCachePaint = new Paint();
+ mCachePaint.setFilterBitmap(true);
+ mCacheCanvas.setBitmap(mCache);
+ mCacheCanvas.scale(mBitmapCacheScale, mBitmapCacheScale);
+ }
+ }
+
@Override
protected void onDraw(Canvas canvas) {
// When we're large, we are either drawn in a "hover" state (ie when dragging an item to
@@ -523,6 +624,8 @@ public class CellLayout extends ViewGroup implements Dimmable {
child.setAlpha(getAlpha());
addView(child, index, lp);
+ // invalidate the cache to have it reflect the new item
+ invalidateCache();
if (markCells) markCellsAsOccupiedForView(child);
return true;
@@ -530,6 +633,13 @@ public class CellLayout extends ViewGroup implements Dimmable {
return false;
}
+ public void setAcceptsDrops(boolean acceptsDrops) {
+ if (mAcceptsDrops != acceptsDrops) {
+ mAcceptsDrops = acceptsDrops;
+ invalidate();
+ }
+ }
+
public boolean getAcceptsDrops() {
return mAcceptsDrops;
}
@@ -841,6 +951,7 @@ public class CellLayout extends ViewGroup implements Dimmable {
}
}
}
+ prepareCacheBitmap();
}
@Override
@@ -848,6 +959,10 @@ public class CellLayout extends ViewGroup implements Dimmable {
super.onSizeChanged(w, h, oldw, oldh);
mBackgroundRect.set(0, 0, w, h);
updateGlowRect();
+ mCacheRect.set(0, 0, (int) (mBitmapCacheScale * w), (int) (mBitmapCacheScale * h));
+ mCache = null;
+ prepareCacheBitmap();
+ invalidateCache();
}
@Override
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index 03521760d..a18047fad 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -170,6 +170,7 @@ public class Workspace extends SmoothPagedView
// in all apps or customize mode)
private boolean mIsSmall = false;
private boolean mIsInUnshrinkAnimation = false;
+ private AnimatorListener mShrinkAnimationListener;
private AnimatorListener mUnshrinkAnimationListener;
enum ShrinkState { TOP, SPRING_LOADED, MIDDLE, BOTTOM_HIDDEN, BOTTOM_VISIBLE };
private ShrinkState mShrinkState;
@@ -266,6 +267,7 @@ public class Workspace extends SmoothPagedView
@Override
public void onAnimationStart(Animator animation) {
mIsInUnshrinkAnimation = true;
+ disableCacheUpdates();
}
@Override
public void onAnimationEnd(Animator animation) {
@@ -273,6 +275,17 @@ public class Workspace extends SmoothPagedView
if (mShrinkState != ShrinkState.SPRING_LOADED) {
mDrawCustomizeTrayBackground = false;
}
+ enableCacheUpdates();
+ }
+ };
+ mShrinkAnimationListener = new AnimatorListenerAdapter() {
+ @Override
+ public void onAnimationStart(Animator animation) {
+ disableCacheUpdates();
+ }
+ @Override
+ public void onAnimationEnd(Animator animation) {
+ enableCacheUpdates();
}
};
mSnapVelocity = 600;
@@ -746,6 +759,22 @@ public class Workspace extends SmoothPagedView
}
}
+ public void enableCacheUpdates() {
+ final int pageCount = getChildCount();
+ for (int i = 0; i < pageCount; i++) {
+ final CellLayout page = (CellLayout) getChildAt(i);
+ page.enableCacheUpdates();
+ }
+ }
+
+ public void disableCacheUpdates() {
+ final int pageCount = getChildCount();
+ for (int i = 0; i < pageCount; i++) {
+ final CellLayout page = (CellLayout) getChildAt(i);
+ page.disableCacheUpdates();
+ }
+ }
+
@Override
protected void onDraw(Canvas canvas) {
// Draw the background gradient if necessary
@@ -1066,12 +1095,14 @@ public class Workspace extends SmoothPagedView
cl.setBackgroundAlpha(finalAlpha);
cl.setAlpha(finalAlpha);
cl.setRotationY(rotation);
+ mShrinkAnimationListener.onAnimationEnd(null);
}
// increment newX for the next screen
newX += scaledPageWidth + extraScaledSpacing;
}
setLayoutScale(1.0f);
if (animated) {
+ mAnimator.addListener(mShrinkAnimationListener);
mAnimator.start();
}
setChildrenDrawnWithCacheEnabled(true);
@@ -1297,6 +1328,7 @@ public class Workspace extends SmoothPagedView
cl.setBackgroundAlpha(0.0f);
cl.setAlpha(finalAlphaValue);
cl.setRotationY(rotation);
+ mUnshrinkAnimationListener.onAnimationEnd(null);
}
}