| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package com.android.test.uibench.leanback; |
| 17 |
|
| 18 |
import android.content.Context; |
| 19 |
import android.graphics.Bitmap; |
| 20 |
import android.graphics.Canvas; |
| 21 |
import android.graphics.Color; |
| 22 |
import android.graphics.Paint; |
| 23 |
import android.os.AsyncTask; |
| 24 |
import androidx.collection.LruCache; |
| 25 |
import android.util.DisplayMetrics; |
| 26 |
import android.widget.ImageView; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class BitmapLoader { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static final float CACHE_SIZE_TO_SCREEN = 1.5f; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static final int BYTES_PER_PIXEL = 4; |
| 49 |
|
| 50 |
static LruCache<Long, Bitmap> sLruCache; |
| 51 |
static Paint sTextPaint = new Paint(); |
| 52 |
|
| 53 |
static { |
| 54 |
sTextPaint.setColor(Color.BLACK); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static LruCache<Long, Bitmap> getLruCache(Context context) { |
| 61 |
if (sLruCache == null) { |
| 62 |
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); |
| 63 |
int width = metrics.widthPixels; |
| 64 |
int height = metrics.heightPixels; |
| 65 |
int maxBytes = (int) (width * height * BYTES_PER_PIXEL * CACHE_SIZE_TO_SCREEN); |
| 66 |
sLruCache = new LruCache<Long, Bitmap>(maxBytes) { |
| 67 |
@Override |
| 68 |
protected int sizeOf(Long key, Bitmap value) { |
| 69 |
return value.getByteCount(); |
| 70 |
} |
| 71 |
}; |
| 72 |
} |
| 73 |
return sLruCache; |
| 74 |
} |
| 75 |
|
| 76 |
static class BitmapAsyncTask extends AsyncTask<Void, Void, Bitmap> { |
| 77 |
|
| 78 |
ImageView mImageView; |
| 79 |
long mId; |
| 80 |
int mWidth; |
| 81 |
int mHeight; |
| 82 |
|
| 83 |
BitmapAsyncTask(ImageView view, long id, int width, int height) { |
| 84 |
mImageView = view; |
| 85 |
mId = id; |
| 86 |
mImageView.setTag(this); |
| 87 |
mWidth = width; |
| 88 |
mHeight = height; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
protected Bitmap doInBackground(Void... voids) { |
| 93 |
|
| 94 |
Bitmap bitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888); |
| 95 |
Canvas canvas = new Canvas(bitmap); |
| 96 |
canvas.drawARGB(0xff, 0xff, 0xff, 0xff); |
| 97 |
canvas.drawText(Long.toString(mId), 0f, mHeight / 2, sTextPaint); |
| 98 |
canvas.setBitmap(null); |
| 99 |
bitmap.prepareToDraw(); |
| 100 |
return bitmap; |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
protected void onCancelled() { |
| 105 |
if (mImageView.getTag() == this) { |
| 106 |
mImageView.setTag(null); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
protected void onPostExecute(Bitmap bitmap) { |
| 112 |
if (mImageView.getTag() == this) { |
| 113 |
mImageView.setTag(null); |
| 114 |
sLruCache.put(mId, bitmap); |
| 115 |
mImageView.setImageBitmap(bitmap); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public static void loadBitmap(ImageView view, long id, int width, int height) { |
| 121 |
Context context = view.getContext(); |
| 122 |
Bitmap bitmap = getLruCache(context).get(id); |
| 123 |
if (bitmap != null) { |
| 124 |
view.setImageBitmap(bitmap); |
| 125 |
return; |
| 126 |
} |
| 127 |
new BitmapAsyncTask(view, id, width, height) |
| 128 |
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 129 |
} |
| 130 |
|
| 131 |
public static void cancel(ImageView view) { |
| 132 |
BitmapAsyncTask task = (BitmapAsyncTask) view.getTag(); |
| 133 |
if (task != null && task.mImageView == view) { |
| 134 |
task.mImageView.setTag(null); |
| 135 |
task.cancel(false); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
public static void clear() { |
| 140 |
if (sLruCache != null) { |
| 141 |
sLruCache.evictAll(); |
| 142 |
} |
| 143 |
} |
| 144 |
} |