summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlinus_lee <llee@cyngn.com>2014-12-22 19:15:31 -0800
committerDanny Baumann <dannybaumann@web.de>2014-12-23 13:55:27 +0100
commit04f294bf3896f10c85a46c998be03856e2a68073 (patch)
tree213aee8e26738f6bb3af41f463b17aba6c5c20c5
parent0107486cd17d8372355d5ad6b28f569bc6b2cb94 (diff)
downloadandroid_packages_apps_Eleven-04f294bf3896f10c85a46c998be03856e2a68073.tar.gz
android_packages_apps_Eleven-04f294bf3896f10c85a46c998be03856e2a68073.tar.bz2
android_packages_apps_Eleven-04f294bf3896f10c85a46c998be03856e2a68073.zip
Eleven: Add some caching logic to BitmapWithColors for perf optimization
Change-Id: I9fe5e83b9b1b5bb8ca24978436ed371eec2db399
-rw-r--r--src/com/cyanogenmod/eleven/cache/ImageFetcher.java2
-rw-r--r--src/com/cyanogenmod/eleven/utils/BitmapWithColors.java90
-rw-r--r--src/com/cyanogenmod/eleven/widgets/LetterTileDrawable.java2
3 files changed, 55 insertions, 39 deletions
diff --git a/src/com/cyanogenmod/eleven/cache/ImageFetcher.java b/src/com/cyanogenmod/eleven/cache/ImageFetcher.java
index fe50c6f..0eb8ec6 100644
--- a/src/com/cyanogenmod/eleven/cache/ImageFetcher.java
+++ b/src/com/cyanogenmod/eleven/cache/ImageFetcher.java
@@ -202,7 +202,7 @@ public class ImageFetcher extends ImageWorker {
artwork = mImageCache.getArtworkFromFile(mContext, albumId);
}
if (artwork != null) {
- return new BitmapWithColors(artwork);
+ return new BitmapWithColors(artwork, key.hashCode());
}
return LetterTileDrawable.createDefaultBitmap(mContext, key, ImageType.ALBUM, false,
diff --git a/src/com/cyanogenmod/eleven/utils/BitmapWithColors.java b/src/com/cyanogenmod/eleven/utils/BitmapWithColors.java
index 7c76d0a..77b3573 100644
--- a/src/com/cyanogenmod/eleven/utils/BitmapWithColors.java
+++ b/src/com/cyanogenmod/eleven/utils/BitmapWithColors.java
@@ -18,25 +18,36 @@ package com.cyanogenmod.eleven.utils;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.graphics.Palette;
+import android.util.LruCache;
public class BitmapWithColors {
+ private static final class BitmapColors {
+ public int mVibrantColor;
+ public int mVibrantDarkColor;
+
+ public BitmapColors(int vibrantColor, int vibrantDarkColor) {
+ mVibrantColor = vibrantColor;
+ mVibrantDarkColor = vibrantDarkColor;
+ }
+ }
+
+ private static final int CACHE_SIZE_MAX = 20;
+ private static final LruCache<Integer, BitmapColors> sCachedColors =
+ new LruCache<Integer, BitmapColors>(CACHE_SIZE_MAX);
+
private Bitmap mBitmap;
- private int mVibrantColor;
- private int mVibrantDarkColor;
- private boolean mColorsLoaded = false;
+ private int mBitmapKey;
+ private BitmapColors mColors;
- public BitmapWithColors(Bitmap bitmap) {
+ public BitmapWithColors(Bitmap bitmap, int bitmapKey) {
mBitmap = bitmap;
- mVibrantColor = Color.TRANSPARENT;
- mVibrantDarkColor = Color.TRANSPARENT;
- mColorsLoaded = false;
+ mBitmapKey = bitmapKey;
}
- public BitmapWithColors(Bitmap bitmap, int vibrantColor, int vibrantDarkColor) {
+ public BitmapWithColors(Bitmap bitmap, int bitmapKey, int vibrantColor, int vibrantDarkColor) {
mBitmap = bitmap;
- mVibrantColor = vibrantColor;
- mVibrantDarkColor = vibrantDarkColor;
- mColorsLoaded = true;
+ mBitmapKey = bitmapKey;
+ mColors = new BitmapColors(vibrantColor, vibrantDarkColor);
}
public Bitmap getBitmap() {
@@ -45,47 +56,52 @@ public class BitmapWithColors {
public int getVibrantColor() {
loadColorsIfNeeded();
- return mVibrantColor;
+ if (mColors.mVibrantColor == Color.TRANSPARENT) {
+ return mColors.mVibrantDarkColor;
+ }
+ return mColors.mVibrantColor;
}
public int getVibrantDarkColor() {
loadColorsIfNeeded();
- return mVibrantDarkColor;
+ if (mColors.mVibrantDarkColor == Color.TRANSPARENT) {
+ return mColors.mVibrantColor;
+ }
+ return mColors.mVibrantDarkColor;
}
- private void loadColorsIfNeeded() {
- synchronized (this) {
- if (mColorsLoaded) {
- return;
- }
+ private synchronized void loadColorsIfNeeded() {
+ if (mColors != null) {
+ return;
+ }
+
+ synchronized (sCachedColors) {
+ mColors = sCachedColors.get(mBitmapKey);
+ }
+ if (mColors != null) {
+ return;
}
final Palette p = Palette.generate(mBitmap);
+ if (p == null) {
+ return;
+ }
+
int vibrantColor = Color.TRANSPARENT;
int vibrantDarkColor = Color.TRANSPARENT;
- if (p != null) {
- Palette.Swatch swatch = p.getDarkVibrantSwatch();
- if (swatch != null) {
- vibrantDarkColor = swatch.getRgb();
- }
- swatch = p.getVibrantSwatch();
- if (swatch != null) {
- vibrantColor = swatch.getRgb();
- }
+ Palette.Swatch swatch = p.getDarkVibrantSwatch();
+ if (swatch != null) {
+ vibrantDarkColor = swatch.getRgb();
}
-
- if (vibrantColor == Color.TRANSPARENT && vibrantDarkColor != Color.TRANSPARENT) {
- vibrantColor = vibrantDarkColor;
- }
- if (vibrantColor != Color.TRANSPARENT && vibrantDarkColor == Color.TRANSPARENT) {
- vibrantDarkColor = vibrantColor;
+ swatch = p.getVibrantSwatch();
+ if (swatch != null) {
+ vibrantColor = swatch.getRgb();
}
- synchronized (this) {
- mColorsLoaded = true;
- mVibrantColor = vibrantColor;
- mVibrantDarkColor = vibrantDarkColor;
+ mColors = new BitmapColors(vibrantColor, vibrantDarkColor);
+ synchronized (sCachedColors) {
+ sCachedColors.put(mBitmapKey, mColors);
}
}
}
diff --git a/src/com/cyanogenmod/eleven/widgets/LetterTileDrawable.java b/src/com/cyanogenmod/eleven/widgets/LetterTileDrawable.java
index 306e998..3b7971d 100644
--- a/src/com/cyanogenmod/eleven/widgets/LetterTileDrawable.java
+++ b/src/com/cyanogenmod/eleven/widgets/LetterTileDrawable.java
@@ -366,6 +366,6 @@ public class LetterTileDrawable extends Drawable {
drawBitmap(defaultBitmap, defaultBitmap.getWidth(), defaultBitmap.getHeight(), canvas,
bounds, 1, 0, paint);
- return new BitmapWithColors(createdBitmap, color, vibrantDarkColor);
+ return new BitmapWithColors(createdBitmap, identifier.hashCode(), color, vibrantDarkColor);
}
}