提交 48be324f 编写于 作者: S Sam Judd

Move get bitmap size bytes into Util.

上级 dc6b7737
package com.bumptech.glide.util;
import android.graphics.Bitmap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import static org.junit.Assert.assertEquals;
@RunWith(RobolectricTestRunner.class)
public class UtilTest {
@Test
public void testReturnsCorrectBitmapSizeForDifferentDimensions() {
int width = 100;
int height = 100;
Bitmap.Config config = Bitmap.Config.ARGB_8888;
int initialSize = Util.getBitmapPixelSize(width, height, config);
int sizeOne = Util.getBitmapPixelSize(width * 2, height, config);
int sizeTwo = Util.getBitmapPixelSize(width, height * 2, config);
assertEquals(4 * width * height, initialSize);
assertEquals(2 * initialSize, sizeOne);
assertEquals(2 * initialSize, sizeTwo);
}
@Test
public void testReturnsCorrectBitmapSizeForAlpha8Bitmap() {
int width = 110;
int height = 43;
int size = Util.getBitmapPixelSize(width, height, Bitmap.Config.ALPHA_8);
assertEquals(width * height, size);
}
@Test
public void testReturnsCorrectBitmapSizeForRgb565() {
int width = 34;
int height = 1444;
int size = Util.getBitmapPixelSize(width, height, Bitmap.Config.RGB_565);
assertEquals(width * height * 2, size);
}
@Test
public void testReturnsCorrectBitmapSizeForARGB4444() {
int width = 4454;
int height = 1235;
int size = Util.getBitmapPixelSize(width, height, Bitmap.Config.ARGB_4444);
assertEquals(width * height * 2, size);
}
@Test
public void testReturnsCorrectBitmapSizeForARGB8888() {
int width = 943;
int height = 3584;
int size = Util.getBitmapPixelSize(width, height, Bitmap.Config.ARGB_8888);
assertEquals(width * height * 4, size);
}
@Test
public void testReturnsLargestSizeForNullConfig() {
int width = 999;
int height = 41324;
int size = Util.getBitmapPixelSize(width, height, null);
assertEquals(width * height * 4, size);
}
}
\ No newline at end of file
......@@ -13,10 +13,10 @@ import java.util.TreeMap;
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
class SizeStrategy implements LruPoolStrategy {
private static final int MAX_SIZE_MULTIPLE = 4;
private static final int MAX_SIZE_MULTIPLE = 8;
private final KeyPool keyPool = new KeyPool();
private final GroupedLinkedMap<Key, Bitmap> groupedMap = new GroupedLinkedMap<Key, Bitmap>();
private final TreeMap<Integer, Integer> sortedSizes = new TreeMap<Integer, Integer>();
private final TreeMap<Integer, Integer> sortedSizes = new PrettyPrintTreeMap<Integer, Integer>();
@Override
public void put(Bitmap bitmap) {
......@@ -31,7 +31,7 @@ class SizeStrategy implements LruPoolStrategy {
@Override
public Bitmap get(int width, int height, Bitmap.Config config) {
final int size = getSize(width, height, config);
final int size = Util.getBitmapPixelSize(width, height, config);
Key key = keyPool.get(size);
Integer possibleSize = sortedSizes.ceilingKey(size);
......@@ -76,7 +76,8 @@ class SizeStrategy implements LruPoolStrategy {
@Override
public String logBitmap(int width, int height, Bitmap.Config config) {
return getBitmapString(getSize(width, height, config));
int size = Util.getBitmapPixelSize(width, height, config);
return getBitmapString(size);
}
@Override
......@@ -86,16 +87,27 @@ class SizeStrategy implements LruPoolStrategy {
@Override
public String toString() {
String result = "SizeStrategy:\n " + groupedMap + "\n SortedSizes( ";
boolean hadAtLeastOneKey = false;
for (Integer size : sortedSizes.keySet()) {
hadAtLeastOneKey = true;
result += "{" + getBitmapString(size) + ":" + sortedSizes.get(size) + "}, ";
}
if (hadAtLeastOneKey) {
result = result.substring(0, result.length() - 2);
return "SizeStrategy:\n "
+ groupedMap + "\n"
+ " SortedSizes" + sortedSizes;
}
private static class PrettyPrintTreeMap<K, V> extends TreeMap<K, V> {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("( ");
for (Entry<K, V> entry : entrySet()) {
sb.append("{").append(entry.getKey()).append(":").append(entry.getValue()).append("}, ");
}
final String result;
if (!isEmpty()) {
result = sb.substring(0, sb.length() - 2);
} else {
result = sb.toString();
}
return result + " )";
}
return result + " )";
}
private static String getBitmapString(Bitmap bitmap) {
......@@ -107,33 +119,6 @@ class SizeStrategy implements LruPoolStrategy {
return "[" + size + "]";
}
private static int getSize(int width, int height, Bitmap.Config config) {
return width * height * getBytesPerPixel(config);
}
private static int getBytesPerPixel(Bitmap.Config config) {
// a bitmap by decoding a gif has null "config" in certain environments.
if (config == null) {
return 4;
}
switch (config) {
case ARGB_8888:
return 4;
case RGB_565:
return 2;
case ARGB_4444:
return 2;
case ALPHA_8:
return 1;
default:
// We only use this to calculate sizes to get, so choosing 4 bytes per pixel is conservative and
// probably forces us to get a larger bitmap than we really need. Since we can't tell for sure, probably
// better safe than sorry.
return 4;
}
}
private static class KeyPool extends BaseKeyPool<Key> {
public Key get(int size) {
......
......@@ -55,6 +55,32 @@ public class Util {
return bitmap.getHeight() * bitmap.getRowBytes();
}
public static int getBitmapPixelSize(int width, int height, Bitmap.Config config) {
return width * height * getBytesPerPixel(config);
}
private static int getBytesPerPixel(Bitmap.Config config) {
// A bitmap by decoding a gif has null "config" in certain environments.
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
int bytesPerPixel;
switch (config) {
case ALPHA_8:
bytesPerPixel = 1;
break;
case RGB_565:
case ARGB_4444:
bytesPerPixel = 2;
break;
case ARGB_8888:
default:
bytesPerPixel = 4;
}
return bytesPerPixel;
}
public static void assertMainThread() {
if (!isOnMainThread()) {
throw new IllegalArgumentException("You must call this method on the main thread");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册