Make Song.generateBitmap more clear
This commit is contained in:
parent
91b8d627a8
commit
9de092f7fb
@ -284,12 +284,17 @@ public final class CoverView extends View {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a bitmap for the given Song and stores it in the cache.
|
* Generates a bitmap for the given Song and stores it in the cache.
|
||||||
|
*
|
||||||
|
* Prunes old bitmaps if the timeline becomes full.
|
||||||
*/
|
*/
|
||||||
void generateBitmap(Song song)
|
void generateBitmap(Song song)
|
||||||
{
|
{
|
||||||
int id = (int)song.id;
|
int id = (int)song.id;
|
||||||
|
boolean created = false;
|
||||||
|
|
||||||
if (mBitmapCache.get(id) == null) {
|
if (mBitmapCache.get(id) == null) {
|
||||||
|
created = true;
|
||||||
|
|
||||||
Bitmap bitmap;
|
Bitmap bitmap;
|
||||||
if (mSeparateInfo)
|
if (mSeparateInfo)
|
||||||
bitmap = CoverBitmap.createSeparatedBitmap(song, getWidth(), getHeight());
|
bitmap = CoverBitmap.createSeparatedBitmap(song, getWidth(), getHeight());
|
||||||
@ -301,25 +306,34 @@ public final class CoverView extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int[] timeline = mCacheTimeline;
|
int[] timeline = mCacheTimeline;
|
||||||
int i = timeline.length;
|
int end = timeline.length;
|
||||||
while (--i != -1 && timeline[i] == 0);
|
while (end > 0 && timeline[end - 1] == 0)
|
||||||
int j = i + 1;
|
--end;
|
||||||
while (--j != -1 && timeline[j] != id);
|
|
||||||
if (j != -1) {
|
if (!created) {
|
||||||
System.arraycopy(timeline, j + 1, timeline, j, timeline.length - j - 1);
|
// If we already have a bitmap for the given song, erase the old
|
||||||
} else if (i == timeline.length - 1) {
|
// id and make room for it at the end of the timeline.
|
||||||
|
int i = end;
|
||||||
|
while (--i != -1 && timeline[i] != id);
|
||||||
|
if (i != -1) {
|
||||||
|
System.arraycopy(timeline, i + 1, timeline, i, timeline.length - i - 1);
|
||||||
|
--end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end == timeline.length) {
|
||||||
|
// If the timeline is full, erase the bitmap for the oldest song
|
||||||
|
// and make room for the new bitmap at the end of the timeline.
|
||||||
int toRemove = timeline[0];
|
int toRemove = timeline[0];
|
||||||
System.arraycopy(timeline, 1, timeline, 0, timeline.length - 1);
|
System.arraycopy(timeline, 1, timeline, 0, timeline.length - 1);
|
||||||
Bitmap bitmap = mBitmapCache.get(toRemove);
|
Bitmap bitmap = mBitmapCache.get(toRemove);
|
||||||
mBitmapCache.remove(toRemove);
|
mBitmapCache.remove(toRemove);
|
||||||
if (bitmap != null)
|
if (bitmap != null)
|
||||||
bitmap.recycle();
|
bitmap.recycle();
|
||||||
|
--end;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i < timeline.length - 1)
|
timeline[end] = id;
|
||||||
++i;
|
|
||||||
|
|
||||||
timeline[i] = id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user