diff options
Diffstat (limited to 'src/github/daneren2005')
-rw-r--r-- | src/github/daneren2005/dsub/util/ImageLoader.java | 111 |
1 files changed, 71 insertions, 40 deletions
diff --git a/src/github/daneren2005/dsub/util/ImageLoader.java b/src/github/daneren2005/dsub/util/ImageLoader.java index ccdb3432..526c8106 100644 --- a/src/github/daneren2005/dsub/util/ImageLoader.java +++ b/src/github/daneren2005/dsub/util/ImageLoader.java @@ -18,8 +18,15 @@ */ package github.daneren2005.dsub.util; +import android.annotation.TargetApi; import android.content.Context; import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.LinearGradient; +import android.graphics.Paint; +import android.graphics.Rect; +import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.TransitionDrawable; @@ -55,7 +62,8 @@ public class ImageLoader { private final int imageSizeDefault; private final int imageSizeLarge; private final int avatarSizeDefault; - private Drawable largeUnknownImage; + + private final static int[] COLORS = {0xFF33B5E5, 0xFFAA66CC, 0xFF99CC00, 0xFFFFBB33, 0xFFFF4444}; public ImageLoader(Context context) { this.context = context; @@ -87,8 +95,6 @@ public class ImageLoader { DisplayMetrics metrics = context.getResources().getDisplayMetrics(); imageSizeLarge = Math.round(Math.min(metrics.widthPixels, metrics.heightPixels)); avatarSizeDefault = context.getResources().getDrawable(R.drawable.ic_social_person).getIntrinsicHeight(); - - createLargeUnknownImage(context); } public void clearCache() { @@ -96,18 +102,59 @@ public class ImageLoader { cache.evictAll(); } - private void createLargeUnknownImage(Context context) { - BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.unknown_album_large); - Bitmap bitmap = Bitmap.createScaledBitmap(drawable.getBitmap(), imageSizeLarge, imageSizeLarge, true); - largeUnknownImage = Util.createDrawableFromBitmap(context, bitmap); + private Bitmap getUnknownImage(MusicDirectory.Entry entry, int size) { + String key; + int color; + if(entry == null) { + key = getKey("unknown", size); + color = COLORS[0]; + } else { + key = getKey(entry.getId() + "unknown", size); + color = COLORS[Math.abs(entry.getAlbum().hashCode()) % COLORS.length]; + } + Bitmap bitmap = cache.get(key); + if(bitmap == null) { + bitmap = createUnknownImage(entry, size, color); + cache.put(key, bitmap); + } + + return bitmap; + } + private Bitmap createUnknownImage(MusicDirectory.Entry entry, int size, int primaryColor) { + Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + + Paint color = new Paint(); + color.setColor(primaryColor); + canvas.drawRect(0, 0, size, size * 2.0f / 3.0f, color); + + color.setShader(new LinearGradient(0, 0, 0, size / 3.0f, Color.rgb(82, 82, 82), Color.BLACK, Shader.TileMode.MIRROR)); + canvas.drawRect(0, size * 2.0f / 3.0f, size, size, color); + + if(entry != null) { + Paint font = new Paint(); + font.setFlags(Paint.ANTI_ALIAS_FLAG); + font.setColor(Color.WHITE); + font.setTextSize(3.0f + size * 0.07f); + + if(entry.getAlbum() != null) { + canvas.drawText(entry.getAlbum(), size * 0.05f, size * 0.6f, font); + } + + if(entry.getArtist() != null) { + canvas.drawText(entry.getArtist(), size * 0.05f, size * 0.8f, font); + } + } + + return bitmap; } public Bitmap getCachedImage(Context context, MusicDirectory.Entry entry, boolean large) { + int size = large ? imageSizeLarge : imageSizeDefault; if(entry == null || entry.getCoverArt() == null) { - return null; + return getUnknownImage(entry, size); } - int size = large ? imageSizeLarge : imageSizeDefault; Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), size)); if(bitmap == null || bitmap.isRecycled()) { bitmap = FileUtil.getAlbumArtBitmap(context, entry, size); @@ -120,10 +167,6 @@ public class ImageLoader { } public ImageTask loadImage(View view, MusicDirectory.Entry entry, boolean large, boolean crossfade) { - if (largeUnknownImage != null && ((BitmapDrawable)largeUnknownImage).getBitmap().isRecycled()) { - createLargeUnknownImage(view.getContext()); - } - if(entry != null && entry.getCoverArt() == null && entry.isDirectory() && !Util.isOffline(context)) { // Try to lookup child cover art MusicDirectory.Entry firstChild = FileUtil.lookupChild(context, entry, true); @@ -131,13 +174,16 @@ public class ImageLoader { entry.setCoverArt(firstChild.getCoverArt()); } } + + Bitmap bitmap; + int size = large ? imageSizeLarge : imageSizeDefault; if (entry == null || entry.getCoverArt() == null) { - setUnknownImage(view, large); + bitmap = getUnknownImage(entry, size); + setImage(view, Util.createDrawableFromBitmap(context, bitmap), crossfade); return null; } - int size = large ? imageSizeLarge : imageSizeDefault; - Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), size)); + bitmap = cache.get(getKey(entry.getCoverArt(), size)); if (bitmap != null && !bitmap.isRecycled()) { final Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap); setImage(view, drawable, crossfade); @@ -148,7 +194,8 @@ public class ImageLoader { } if (!large) { - setUnknownImage(view, large); + bitmap = getUnknownImage(entry, size); + setImage(view, Util.createDrawableFromBitmap(context, bitmap), crossfade); } ImageTask task = new ViewImageTask(view.getContext(), entry, size, imageSizeLarge, large, view, crossfade); task.execute(); @@ -156,23 +203,22 @@ public class ImageLoader { } public SilentBackgroundTask<Void> loadImage(Context context, RemoteControlClient remoteControl, MusicDirectory.Entry entry) { - if (largeUnknownImage != null && ((BitmapDrawable)largeUnknownImage).getBitmap().isRecycled()) { - createLargeUnknownImage(context); - } - + Bitmap bitmap; if (entry == null || entry.getCoverArt() == null) { - setUnknownImage(remoteControl); + bitmap = getUnknownImage(entry, imageSizeLarge); + setImage(remoteControl, Util.createDrawableFromBitmap(context, bitmap)); return null; } - Bitmap bitmap = cache.get(getKey(entry.getCoverArt(), imageSizeLarge)); + bitmap = cache.get(getKey(entry.getCoverArt(), imageSizeLarge)); if (bitmap != null && !bitmap.isRecycled()) { Drawable drawable = Util.createDrawableFromBitmap(this.context, bitmap); setImage(remoteControl, drawable); return null; } - setUnknownImage(remoteControl); + bitmap = getUnknownImage(entry, imageSizeLarge); + setImage(remoteControl, Util.createDrawableFromBitmap(context, bitmap)); ImageTask task = new RemoteControlClientImageTask(context, entry, imageSizeLarge, imageSizeLarge, false, remoteControl); task.execute(); return task; @@ -239,6 +285,7 @@ public class ImageLoader { } } + @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH) private void setImage(RemoteControlClient remoteControl, Drawable drawable) { if(remoteControl != null && drawable != null) { Bitmap origBitmap = ((BitmapDrawable)drawable).getBitmap(); @@ -256,22 +303,6 @@ public class ImageLoader { } } - private void setUnknownImage(View view, boolean large) { - if (large) { - setImage(view, largeUnknownImage, false); - } else { - if (view instanceof TextView) { - ((TextView) view).setCompoundDrawablesWithIntrinsicBounds(R.drawable.unknown_album, 0, 0, 0); - } else if (view instanceof ImageView) { - ((ImageView) view).setImageResource(R.drawable.unknown_album); - } - } - } - - private void setUnknownImage(RemoteControlClient remoteControl) { - setImage(remoteControl, largeUnknownImage); - } - public abstract class ImageTask extends SilentBackgroundTask<Void> { private final Context mContext; private final MusicDirectory.Entry mEntry; |