Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
48be324f
G
glide
项目概览
沉迷打码的小凳子
/
glide
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
glide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
48be324f
编写于
9月 20, 2014
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move get bitmap size bytes into Util.
上级
dc6b7737
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
122 addition
and
40 deletion
+122
-40
library/src/androidTest/java/com/bumptech/glide/util/UtilTest.java
...rc/androidTest/java/com/bumptech/glide/util/UtilTest.java
+71
-0
library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/SizeStrategy.java
...mptech/glide/load/engine/bitmap_recycle/SizeStrategy.java
+25
-40
library/src/main/java/com/bumptech/glide/util/Util.java
library/src/main/java/com/bumptech/glide/util/Util.java
+26
-0
未找到文件。
library/src/androidTest/java/com/bumptech/glide/util/UtilTest.java
0 → 100644
浏览文件 @
48be324f
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
library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/SizeStrategy.java
浏览文件 @
48be324f
...
...
@@ -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
PrettyPrint
TreeMap
<
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
=
get
Size
(
width
,
height
,
config
);
final
int
size
=
Util
.
getBitmapPixel
Size
(
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
)
{
...
...
library/src/main/java/com/bumptech/glide/util/Util.java
浏览文件 @
48be324f
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录