Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
a37592fb
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,发现更多精彩内容 >>
提交
a37592fb
编写于
6月 14, 2014
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix GIF drawable transformations.
上级
da7bc32c
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
276 addition
and
74 deletion
+276
-74
library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java
.../main/java/com/bumptech/glide/DrawableRequestBuilder.java
+1
-1
library/src/main/java/com/bumptech/glide/load/resource/gif/GifData.java
...in/java/com/bumptech/glide/load/resource/gif/GifData.java
+8
-2
library/src/main/java/com/bumptech/glide/load/resource/gif/GifDataTransformation.java
...mptech/glide/load/resource/gif/GifDataTransformation.java
+28
-0
library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java
...ava/com/bumptech/glide/load/resource/gif/GifDrawable.java
+13
-2
library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameManager.java
...com/bumptech/glide/load/resource/gif/GifFrameManager.java
+23
-9
library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceDecoder.java
.../bumptech/glide/load/resource/gif/GifResourceDecoder.java
+1
-1
library/src/main/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformation.java
...ad/resource/gifbitmap/GifBitmapWrapperTransformation.java
+22
-20
library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTest.java
...ava/com/bumptech/glide/load/resource/gif/GifDataTest.java
+1
-1
library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTransformationTest.java
...ch/glide/load/resource/gif/GifDataTransformationTest.java
+66
-0
library/src/test/java/com/bumptech/glide/load/resource/gif/GifDrawableTest.java
...com/bumptech/glide/load/resource/gif/GifDrawableTest.java
+21
-5
library/src/test/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformationTest.java
...esource/gifbitmap/GifBitmapWrapperTransformationTest.java
+92
-33
未找到文件。
library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java
浏览文件 @
a37592fb
...
@@ -92,7 +92,7 @@ public class DrawableRequestBuilder<ModelType> extends
...
@@ -92,7 +92,7 @@ public class DrawableRequestBuilder<ModelType> extends
}
}
public
DrawableRequestBuilder
<
ModelType
>
bitmapTransform
(
Transformation
<
Bitmap
>
bitmapTransformation
)
{
public
DrawableRequestBuilder
<
ModelType
>
bitmapTransform
(
Transformation
<
Bitmap
>
bitmapTransformation
)
{
return
transform
(
new
GifBitmapWrapperTransformation
(
context
,
bitmapTransformation
));
return
transform
(
new
GifBitmapWrapperTransformation
(
bitmapTransformation
));
}
}
@Override
@Override
...
...
library/src/main/java/com/bumptech/glide/load/resource/gif/GifData.java
浏览文件 @
a37592fb
...
@@ -16,15 +16,20 @@ public class GifData {
...
@@ -16,15 +16,20 @@ public class GifData {
private
final
GifHeader
header
;
private
final
GifHeader
header
;
private
final
byte
[]
data
;
private
final
byte
[]
data
;
private
String
gifId
;
private
String
gifId
;
private
final
int
targetWidth
;
private
final
int
targetHeight
;
private
final
List
<
GifDrawable
>
drawables
=
new
ArrayList
<
GifDrawable
>();
private
final
List
<
GifDrawable
>
drawables
=
new
ArrayList
<
GifDrawable
>();
private
Transformation
<
Bitmap
>
frameTransformation
;
private
Transformation
<
Bitmap
>
frameTransformation
;
public
GifData
(
Context
context
,
BitmapPool
bitmapPool
,
String
gifId
,
GifHeader
header
,
byte
[]
data
)
{
public
GifData
(
Context
context
,
BitmapPool
bitmapPool
,
String
gifId
,
GifHeader
header
,
byte
[]
data
,
int
targetWidth
,
int
targetHeight
)
{
this
.
context
=
context
;
this
.
context
=
context
;
this
.
bitmapPool
=
bitmapPool
;
this
.
bitmapPool
=
bitmapPool
;
this
.
header
=
header
;
this
.
header
=
header
;
this
.
data
=
data
;
this
.
data
=
data
;
this
.
gifId
=
gifId
;
this
.
gifId
=
gifId
;
this
.
targetWidth
=
targetWidth
;
this
.
targetHeight
=
targetHeight
;
}
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
...
@@ -47,7 +52,8 @@ public class GifData {
...
@@ -47,7 +52,8 @@ public class GifData {
public
GifDrawable
getDrawable
()
{
public
GifDrawable
getDrawable
()
{
GifDecoder
gifDecoder
=
new
GifDecoder
(
bitmapPool
);
GifDecoder
gifDecoder
=
new
GifDecoder
(
bitmapPool
);
gifDecoder
.
setData
(
gifId
,
header
,
data
);
gifDecoder
.
setData
(
gifId
,
header
,
data
);
GifFrameManager
frameManager
=
new
GifFrameManager
(
context
,
gifDecoder
,
getFrameTransformation
());
GifFrameManager
frameManager
=
new
GifFrameManager
(
context
,
gifDecoder
,
getFrameTransformation
(),
targetWidth
,
targetHeight
);
GifDrawable
result
=
new
GifDrawable
(
gifDecoder
,
frameManager
);
GifDrawable
result
=
new
GifDrawable
(
gifDecoder
,
frameManager
);
drawables
.
add
(
result
);
drawables
.
add
(
result
);
...
...
library/src/main/java/com/bumptech/glide/load/resource/gif/GifDataTransformation.java
0 → 100644
浏览文件 @
a37592fb
package
com.bumptech.glide.load.resource.gif
;
import
android.graphics.Bitmap
;
import
com.bumptech.glide.Resource
;
import
com.bumptech.glide.load.MultiTransformation
;
import
com.bumptech.glide.load.Transformation
;
public
class
GifDataTransformation
implements
Transformation
<
GifData
>
{
private
Transformation
<
Bitmap
>
wrapped
;
public
GifDataTransformation
(
Transformation
<
Bitmap
>
wrapped
)
{
this
.
wrapped
=
wrapped
;
}
@Override
public
Resource
<
GifData
>
transform
(
Resource
<
GifData
>
resource
,
int
outWidth
,
int
outHeight
)
{
GifData
data
=
resource
.
get
();
Transformation
<
Bitmap
>
newTransformation
=
new
MultiTransformation
<
Bitmap
>(
data
.
getFrameTransformation
(),
wrapped
);
data
.
setFrameTransformation
(
newTransformation
);
return
resource
;
}
@Override
public
String
getId
()
{
return
wrapped
.
getId
();
}
}
library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java
浏览文件 @
a37592fb
...
@@ -13,6 +13,8 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
...
@@ -13,6 +13,8 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
private
final
Paint
paint
;
private
final
Paint
paint
;
private
final
GifFrameManager
frameManager
;
private
final
GifFrameManager
frameManager
;
private
int
width
;
private
int
height
;
private
GifDecoder
decoder
;
private
GifDecoder
decoder
;
private
boolean
isRunning
;
private
boolean
isRunning
;
private
Bitmap
currentFrame
;
private
Bitmap
currentFrame
;
...
@@ -21,6 +23,8 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
...
@@ -21,6 +23,8 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
public
GifDrawable
(
GifDecoder
decoder
,
GifFrameManager
frameManager
)
{
public
GifDrawable
(
GifDecoder
decoder
,
GifFrameManager
frameManager
)
{
this
.
decoder
=
decoder
;
this
.
decoder
=
decoder
;
this
.
frameManager
=
frameManager
;
this
.
frameManager
=
frameManager
;
width
=
-
1
;
height
=
-
1
;
paint
=
new
Paint
();
paint
=
new
Paint
();
}
}
...
@@ -44,14 +48,15 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
...
@@ -44,14 +48,15 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
return
super
.
setVisible
(
visible
,
restart
);
return
super
.
setVisible
(
visible
,
restart
);
}
}
@Override
@Override
public
int
getIntrinsicWidth
()
{
public
int
getIntrinsicWidth
()
{
return
decoder
.
getWidth
()
;
return
width
;
}
}
@Override
@Override
public
int
getIntrinsicHeight
()
{
public
int
getIntrinsicHeight
()
{
return
decoder
.
getHeight
()
;
return
height
;
}
}
@Override
@Override
...
@@ -96,6 +101,12 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
...
@@ -96,6 +101,12 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
if
(!
isRunning
)
{
if
(!
isRunning
)
{
return
;
return
;
}
}
if
(
width
==
-
1
)
{
width
=
frame
.
getWidth
();
}
if
(
height
==
-
1
)
{
height
=
frame
.
getHeight
();
}
if
(
frame
!=
null
)
{
if
(
frame
!=
null
)
{
currentFrame
=
frame
;
currentFrame
=
frame
;
...
...
library/src/main/java/com/bumptech/glide/load/resource/gif/GifFrameManager.java
浏览文件 @
a37592fb
...
@@ -32,23 +32,29 @@ class GifFrameManager {
...
@@ -32,23 +32,29 @@ class GifFrameManager {
private
final
Context
context
;
private
final
Context
context
;
private
Transformation
<
Bitmap
>
transformation
;
private
Transformation
<
Bitmap
>
transformation
;
private
final
int
targetWidth
;
private
final
int
targetHeight
;
private
DelayTarget
current
;
private
DelayTarget
current
;
private
DelayTarget
next
;
private
DelayTarget
next
;
private
int
frameSize
=
-
1
;
public
interface
FrameCallback
{
public
interface
FrameCallback
{
public
void
onFrameRead
(
Bitmap
frame
);
public
void
onFrameRead
(
Bitmap
frame
);
}
}
public
GifFrameManager
(
Context
context
,
GifDecoder
decoder
,
Transformation
<
Bitmap
>
transformation
)
{
public
GifFrameManager
(
Context
context
,
GifDecoder
decoder
,
Transformation
<
Bitmap
>
transformation
,
int
targetWidth
,
this
(
context
,
decoder
,
new
Handler
(
Looper
.
getMainLooper
()),
transformation
);
int
targetHeight
)
{
this
(
context
,
decoder
,
new
Handler
(
Looper
.
getMainLooper
()),
transformation
,
targetWidth
,
targetHeight
);
}
}
public
GifFrameManager
(
Context
context
,
GifDecoder
decoder
,
Handler
mainHandler
,
public
GifFrameManager
(
Context
context
,
GifDecoder
decoder
,
Handler
mainHandler
,
Transformation
<
Bitmap
>
transformation
)
{
Transformation
<
Bitmap
>
transformation
,
int
targetWidth
,
int
targetHeight
)
{
this
.
context
=
context
;
this
.
context
=
context
;
this
.
decoder
=
decoder
;
this
.
decoder
=
decoder
;
this
.
mainHandler
=
mainHandler
;
this
.
mainHandler
=
mainHandler
;
this
.
transformation
=
transformation
;
this
.
transformation
=
transformation
;
this
.
targetWidth
=
targetWidth
;
this
.
targetHeight
=
targetHeight
;
calculator
=
new
MemorySizeCalculator
(
context
);
calculator
=
new
MemorySizeCalculator
(
context
);
frameLoader
=
new
GifFrameModelLoader
();
frameLoader
=
new
GifFrameModelLoader
();
frameResourceDecoder
=
new
GifFrameResourceDecoder
();
frameResourceDecoder
=
new
GifFrameResourceDecoder
();
...
@@ -70,14 +76,22 @@ class GifFrameManager {
...
@@ -70,14 +76,22 @@ class GifFrameManager {
return
transformation
;
return
transformation
;
}
}
private
int
getEstimatedTotalFrameSize
()
{
if
(
frameSize
==
-
1
)
{
return
decoder
.
getDecodedFramesByteSizeSum
();
}
else
{
return
frameSize
*
decoder
.
getFrameCount
();
}
}
public
void
getNextFrame
(
FrameCallback
cb
)
{
public
void
getNextFrame
(
FrameCallback
cb
)
{
decoder
.
advance
();
decoder
.
advance
();
// We don't want to blow out the entire memory cache with frames of gifs, so try to set some
// We don't want to blow out the entire memory cache with frames of gifs, so try to set some
// maximum size beyond which we will always just decode one frame at a time.
// maximum size beyond which we will always just decode one frame at a time.
boolean
skipCache
=
decoder
.
getDecodedFramesByteSizeSum
()
>
calculator
.
getMemoryCacheSize
()
/
2
;
boolean
skipCache
=
getEstimatedTotalFrameSize
()
>
calculator
.
getMemoryCacheSize
()
/
2
;
long
targetTime
=
SystemClock
.
uptimeMillis
()
+
(
Math
.
min
(
MIN_FRAME_DELAY
,
decoder
.
getNextDelay
()));
long
targetTime
=
SystemClock
.
uptimeMillis
()
+
(
Math
.
min
(
MIN_FRAME_DELAY
,
decoder
.
getNextDelay
()));
next
=
new
DelayTarget
(
decoder
,
cb
,
targetTime
,
mainHandler
);
next
=
new
DelayTarget
(
cb
,
targetTime
);
Glide
.
with
(
context
)
Glide
.
with
(
context
)
.
using
(
frameLoader
,
GifDecoder
.
class
)
.
using
(
frameLoader
,
GifDecoder
.
class
)
...
@@ -105,18 +119,18 @@ class GifFrameManager {
...
@@ -105,18 +119,18 @@ class GifFrameManager {
class
DelayTarget
extends
SimpleTarget
<
Bitmap
>
implements
Runnable
{
class
DelayTarget
extends
SimpleTarget
<
Bitmap
>
implements
Runnable
{
private
FrameCallback
cb
;
private
FrameCallback
cb
;
private
long
targetTime
;
private
long
targetTime
;
private
Handler
mainHandler
;
private
Bitmap
resource
;
private
Bitmap
resource
;
public
DelayTarget
(
GifDecoder
decoder
,
FrameCallback
cb
,
long
targetTime
,
Handler
mainHandler
)
{
public
DelayTarget
(
FrameCallback
cb
,
long
targetTime
)
{
super
(
decoder
.
getWidth
(),
decoder
.
getHeight
()
);
super
(
targetWidth
,
targetHeight
);
this
.
cb
=
cb
;
this
.
cb
=
cb
;
this
.
targetTime
=
targetTime
;
this
.
targetTime
=
targetTime
;
this
.
mainHandler
=
mainHandler
;
}
}
@Override
@Override
public
void
onResourceReady
(
final
Bitmap
resource
)
{
public
void
onResourceReady
(
final
Bitmap
resource
)
{
// Ignore allocationByteSize, we only want the minimum frame size.
frameSize
=
resource
.
getHeight
()
*
resource
.
getRowBytes
();
this
.
resource
=
resource
;
this
.
resource
=
resource
;
mainHandler
.
postAtTime
(
this
,
targetTime
);
mainHandler
.
postAtTime
(
this
,
targetTime
);
if
(
current
!=
null
)
{
if
(
current
!=
null
)
{
...
...
library/src/main/java/com/bumptech/glide/load/resource/gif/GifResourceDecoder.java
浏览文件 @
a37592fb
...
@@ -35,7 +35,7 @@ public class GifResourceDecoder implements ResourceDecoder<InputStream, GifData>
...
@@ -35,7 +35,7 @@ public class GifResourceDecoder implements ResourceDecoder<InputStream, GifData>
byte
[]
data
=
inputStreamToBytes
(
source
);
byte
[]
data
=
inputStreamToBytes
(
source
);
GifHeader
header
=
new
GifHeaderParser
(
data
).
parseHeader
();
GifHeader
header
=
new
GifHeaderParser
(
data
).
parseHeader
();
String
id
=
getGifId
(
data
);
String
id
=
getGifId
(
data
);
return
new
GifDataResource
(
new
GifData
(
context
,
bitmapPool
,
id
,
header
,
data
));
return
new
GifDataResource
(
new
GifData
(
context
,
bitmapPool
,
id
,
header
,
data
,
width
,
height
));
}
}
@Override
@Override
...
...
library/src/main/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformation.java
浏览文件 @
a37592fb
package
com.bumptech.glide.load.resource.gifbitmap
;
package
com.bumptech.glide.load.resource.gifbitmap
;
import
android.content.Context
;
import
android.graphics.Bitmap
;
import
android.graphics.Bitmap
;
import
com.bumptech.glide.Resource
;
import
com.bumptech.glide.Resource
;
import
com.bumptech.glide.load.MultiTransformation
;
import
com.bumptech.glide.load.Transformation
;
import
com.bumptech.glide.load.Transformation
;
import
com.bumptech.glide.load.resource.gif.GifData
;
import
com.bumptech.glide.load.resource.gif.GifData
;
import
com.bumptech.glide.load.resource.gif.GifData
Resource
;
import
com.bumptech.glide.load.resource.gif.GifData
Transformation
;
public
class
GifBitmapWrapperTransformation
implements
Transformation
<
GifBitmapWrapper
>
{
public
class
GifBitmapWrapperTransformation
implements
Transformation
<
GifBitmapWrapper
>
{
private
Context
context
;
private
Transformation
<
Bitmap
>
bitmapTransformation
;
private
Transformation
<
Bitmap
>
wrapped
;
private
Transformation
<
GifData
>
gifDataTransformation
;
public
GifBitmapWrapperTransformation
(
Context
context
,
Transformation
<
Bitmap
>
wrapped
)
{
public
GifBitmapWrapperTransformation
(
Transformation
<
Bitmap
>
bitmapTransformation
)
{
this
.
context
=
context
;
this
(
bitmapTransformation
,
new
GifDataTransformation
(
bitmapTransformation
));
this
.
wrapped
=
wrapped
;
}
GifBitmapWrapperTransformation
(
Transformation
<
Bitmap
>
bitmapTransformation
,
Transformation
<
GifData
>
gifDataTransformation
)
{
this
.
bitmapTransformation
=
bitmapTransformation
;
this
.
gifDataTransformation
=
gifDataTransformation
;
}
}
@Override
@Override
public
Resource
<
GifBitmapWrapper
>
transform
(
Resource
<
GifBitmapWrapper
>
resource
,
int
outWidth
,
int
outHeight
)
{
public
Resource
<
GifBitmapWrapper
>
transform
(
Resource
<
GifBitmapWrapper
>
resource
,
int
outWidth
,
int
outHeight
)
{
Resource
<
Bitmap
>
bitmapResource
=
resource
.
get
().
getBitmapResource
();
Resource
<
Bitmap
>
bitmapResource
=
resource
.
get
().
getBitmapResource
();
if
(
bitmapResource
!=
null
)
{
Resource
<
GifData
>
gifResource
=
resource
.
get
().
getGifResource
();
Resource
<
Bitmap
>
transformed
=
wrapped
.
transform
(
bitmapResource
,
outWidth
,
outHeight
);
if
(
bitmapResource
!=
null
&&
bitmapTransformation
!=
null
)
{
Resource
<
Bitmap
>
transformed
=
bitmapTransformation
.
transform
(
bitmapResource
,
outWidth
,
outHeight
);
if
(
transformed
!=
bitmapResource
)
{
if
(
transformed
!=
bitmapResource
)
{
GifBitmapWrapper
gifBitmap
=
new
GifBitmapWrapper
(
transformed
,
null
);
GifBitmapWrapper
gifBitmap
=
new
GifBitmapWrapper
(
transformed
,
resource
.
get
().
getGifResource
());
return
new
GifBitmapWrapperResource
(
gifBitmap
);
}
}
else
if
(
gifResource
!=
null
&&
gifDataTransformation
!=
null
)
{
Resource
<
GifData
>
transformed
=
gifDataTransformation
.
transform
(
gifResource
,
outWidth
,
outHeight
);
if
(
transformed
!=
gifResource
)
{
GifBitmapWrapper
gifBitmap
=
new
GifBitmapWrapper
(
resource
.
get
().
getBitmapResource
(),
transformed
);
return
new
GifBitmapWrapperResource
(
gifBitmap
);
return
new
GifBitmapWrapperResource
(
gifBitmap
);
}
}
}
else
{
//TODO: this should be pushed down into a GifData transformation?
Resource
<
GifData
>
gifResource
=
resource
.
get
().
getGifResource
();
GifData
gifData
=
gifResource
.
get
();
Transformation
<
Bitmap
>
newTransformation
=
new
MultiTransformation
<
Bitmap
>(
gifData
.
getFrameTransformation
(),
wrapped
);
gifData
.
setFrameTransformation
(
newTransformation
);
return
new
GifBitmapWrapperResource
(
new
GifBitmapWrapper
(
null
,
new
GifDataResource
(
gifData
)));
}
}
return
resource
;
return
resource
;
}
}
@Override
@Override
public
String
getId
()
{
public
String
getId
()
{
return
wrapped
.
getId
();
return
bitmapTransformation
.
getId
();
}
}
}
}
library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTest.java
浏览文件 @
a37592fb
...
@@ -29,7 +29,7 @@ public class GifDataTest {
...
@@ -29,7 +29,7 @@ public class GifDataTest {
BitmapPool
bitmapPool
=
mock
(
BitmapPool
.
class
);
BitmapPool
bitmapPool
=
mock
(
BitmapPool
.
class
);
GifHeader
header
=
mock
(
GifHeader
.
class
);
GifHeader
header
=
mock
(
GifHeader
.
class
);
bytes
=
new
byte
[]
{
'G'
,
'I'
,
'F'
};
bytes
=
new
byte
[]
{
'G'
,
'I'
,
'F'
};
data
=
new
GifData
(
Robolectric
.
application
,
bitmapPool
,
"gifId"
,
header
,
bytes
);
data
=
new
GifData
(
Robolectric
.
application
,
bitmapPool
,
"gifId"
,
header
,
bytes
,
123
,
456
);
}
}
@Test
@Test
...
...
library/src/test/java/com/bumptech/glide/load/resource/gif/GifDataTransformationTest.java
0 → 100644
浏览文件 @
a37592fb
package
com.bumptech.glide.load.resource.gif
;
import
android.graphics.Bitmap
;
import
com.bumptech.glide.Resource
;
import
com.bumptech.glide.load.Transformation
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.mockito.invocation.InvocationOnMock
;
import
org.mockito.stubbing.Answer
;
import
org.robolectric.RobolectricTestRunner
;
import
static
junit
.
framework
.
Assert
.
assertEquals
;
import
static
org
.
mockito
.
Matchers
.
any
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
doAnswer
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
RobolectricTestRunner
.
class
)
public
class
GifDataTransformationTest
{
Transformation
<
Bitmap
>
wrapped
;
GifDataTransformation
transformation
;
@SuppressWarnings
(
"unchecked"
)
@Before
public
void
setUp
()
{
wrapped
=
mock
(
Transformation
.
class
);
transformation
=
new
GifDataTransformation
(
wrapped
);
}
@Test
public
void
testReturnsWrappedTransformationId
()
{
final
String
id
=
"testId"
;
when
(
wrapped
.
getId
()).
thenReturn
(
id
);
assertEquals
(
id
,
transformation
.
getId
());
}
@Test
public
void
testSetsTransformationAsFrameTransformation
()
{
Resource
<
GifData
>
resource
=
mock
(
Resource
.
class
);
GifData
gifData
=
mock
(
GifData
.
class
);
when
(
gifData
.
getFrameTransformation
()).
thenReturn
(
Transformation
.
NONE
);
when
(
resource
.
get
()).
thenReturn
(
gifData
);
final
Resource
<
Bitmap
>
toTransform
=
mock
(
Resource
.
class
);
final
int
width
=
123
;
final
int
height
=
456
;
doAnswer
(
new
Answer
<
Void
>()
{
@Override
public
Void
answer
(
InvocationOnMock
invocation
)
throws
Throwable
{
Transformation
<
Bitmap
>
transformation
=
(
Transformation
<
Bitmap
>)
invocation
.
getArguments
()[
0
];
transformation
.
transform
(
toTransform
,
width
,
height
);
return
null
;
}
}).
when
(
gifData
).
setFrameTransformation
(
any
(
Transformation
.
class
));
transformation
.
transform
(
resource
,
width
,
height
);
verify
(
gifData
).
setFrameTransformation
(
any
(
Transformation
.
class
));
verify
(
wrapped
).
transform
(
eq
(
toTransform
),
eq
(
width
),
eq
(
height
));
}
}
library/src/test/java/com/bumptech/glide/load/resource/gif/GifDrawableTest.java
浏览文件 @
a37592fb
...
@@ -39,17 +39,33 @@ public class GifDrawableTest {
...
@@ -39,17 +39,33 @@ public class GifDrawableTest {
}
}
@Test
@Test
public
void
testReturnsDecoderWidth
()
{
public
void
testReturnsInvalidWidthBeforeFirstFrame
()
{
assertEquals
(-
1
,
drawable
.
getIntrinsicWidth
());
}
@Test
public
void
testReturnsInvalidHeightBeforeFirstFrame
()
{
assertEquals
(-
1
,
drawable
.
getIntrinsicHeight
());
}
@Test
public
void
testReturnsFrameWidthAfterFirstFrame
()
{
int
width
=
123
;
int
width
=
123
;
when
(
gifDecoder
.
getWidth
()).
thenReturn
(
width
);
Bitmap
bitmap
=
Bitmap
.
createBitmap
(
width
,
1231
,
Bitmap
.
Config
.
ARGB_8888
);
drawable
.
setIsRunning
(
true
);
drawable
.
onFrameRead
(
bitmap
);
assertEquals
(
width
,
drawable
.
getIntrinsicWidth
());
assertEquals
(
width
,
drawable
.
getIntrinsicWidth
());
}
}
@Test
@Test
public
void
testReturnsDecoderHeight
()
{
public
void
testReturnsFrameHeightAfterFirstFrame
()
{
int
height
=
321
;
int
height
=
456
;
when
(
gifDecoder
.
getHeight
()).
thenReturn
(
height
);
Bitmap
bitmap
=
Bitmap
.
createBitmap
(
1
,
height
,
Bitmap
.
Config
.
RGB_565
);
drawable
.
setIsRunning
(
true
);
drawable
.
onFrameRead
(
bitmap
);
assertEquals
(
height
,
drawable
.
getIntrinsicHeight
());
assertEquals
(
height
,
drawable
.
getIntrinsicHeight
());
}
}
...
...
library/src/test/java/com/bumptech/glide/load/resource/gifbitmap/GifBitmapWrapperTransformationTest.java
浏览文件 @
a37592fb
...
@@ -7,11 +7,11 @@ import com.bumptech.glide.load.resource.gif.GifData;
...
@@ -7,11 +7,11 @@ import com.bumptech.glide.load.resource.gif.GifData;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.robolectric.Robolectric
;
import
org.robolectric.RobolectricTestRunner
;
import
org.robolectric.RobolectricTestRunner
;
import
static
junit
.
framework
.
Assert
.
assertEquals
;
import
static
junit
.
framework
.
Assert
.
assertEquals
;
import
static
junit
.
framework
.
Assert
.
assertNotSame
;
import
static
junit
.
framework
.
Assert
.
assertNotSame
;
import
static
junit
.
framework
.
Assert
.
assertSame
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Matchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
mockito
.
Mockito
.
when
;
...
@@ -19,13 +19,43 @@ import static org.mockito.Mockito.when;
...
@@ -19,13 +19,43 @@ import static org.mockito.Mockito.when;
@RunWith
(
RobolectricTestRunner
.
class
)
@RunWith
(
RobolectricTestRunner
.
class
)
public
class
GifBitmapWrapperTransformationTest
{
public
class
GifBitmapWrapperTransformationTest
{
private
Transformation
<
Bitmap
>
bitmapTransformation
;
private
Transformation
<
Bitmap
>
bitmapTransformation
;
private
GifBitmapWrapperTransformation
t
ransformation
;
private
Transformation
<
GifData
>
gifT
ransformation
;
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
@Before
@Before
public
void
setUp
()
{
public
void
setUp
()
{
bitmapTransformation
=
mock
(
Transformation
.
class
);
bitmapTransformation
=
mock
(
Transformation
.
class
);
transformation
=
new
GifBitmapWrapperTransformation
(
Robolectric
.
application
,
bitmapTransformation
);
gifTransformation
=
mock
(
Transformation
.
class
);
}
private
class
BitmapResourceHarness
{
Resource
<
Bitmap
>
bitmapResource
=
mock
(
Resource
.
class
);
GifBitmapWrapper
gifBitmapWrapper
=
mock
(
GifBitmapWrapper
.
class
);
Resource
<
GifBitmapWrapper
>
resource
=
mock
(
Resource
.
class
);
GifBitmapWrapperTransformation
transformation
=
new
GifBitmapWrapperTransformation
(
bitmapTransformation
);
int
width
=
123
;
int
height
=
456
;
public
BitmapResourceHarness
()
{
when
(
gifBitmapWrapper
.
getBitmapResource
()).
thenReturn
(
bitmapResource
);
when
(
resource
.
get
()).
thenReturn
(
gifBitmapWrapper
);
}
}
private
class
GifResourceHarness
{
GifData
gifData
=
mock
(
GifData
.
class
);
Resource
<
GifData
>
gifResource
=
mock
(
Resource
.
class
);
GifBitmapWrapper
gifBitmapWrapper
=
mock
(
GifBitmapWrapper
.
class
);
Resource
<
GifBitmapWrapper
>
resource
=
mock
(
Resource
.
class
);
GifBitmapWrapperTransformation
transformation
=
new
GifBitmapWrapperTransformation
(
null
,
gifTransformation
);
int
width
=
123
;
int
height
=
456
;
public
GifResourceHarness
()
{
when
(
gifResource
.
get
()).
thenReturn
(
gifData
);
when
(
gifBitmapWrapper
.
getGifResource
()).
thenReturn
(
gifResource
);
when
(
resource
.
get
()).
thenReturn
(
gifBitmapWrapper
);
}
}
}
@Test
@Test
...
@@ -33,51 +63,80 @@ public class GifBitmapWrapperTransformationTest {
...
@@ -33,51 +63,80 @@ public class GifBitmapWrapperTransformationTest {
String
expectedId
=
"asdfas"
;
String
expectedId
=
"asdfas"
;
when
(
bitmapTransformation
.
getId
()).
thenReturn
(
expectedId
);
when
(
bitmapTransformation
.
getId
()).
thenReturn
(
expectedId
);
assertEquals
(
expectedId
,
transformation
.
getId
());
assertEquals
(
expectedId
,
new
GifBitmapWrapperTransformation
(
bitmapTransformation
)
.
getId
());
}
}
@Test
@Test
public
void
testAppliesTransformationToBitmapResourceAndReturnsNewGifBitmapResource
()
{
public
void
testAppliesBitmapTransformationIfBitmapTransformationIsGivenAndResourceHasBitmapResource
()
{
int
dimens
=
123
;
BitmapResourceHarness
harness
=
new
BitmapResourceHarness
();
Resource
<
Bitmap
>
initial
=
mock
(
Resource
.
class
);
Resource
<
Bitmap
>
transformed
=
mock
(
Resource
.
class
);
Resource
<
Bitmap
>
transformedBitmapResource
=
mock
(
Resource
.
class
);
when
(
bitmapTransformation
.
transform
(
eq
(
initial
),
eq
(
dimens
),
eq
(
dimens
))).
thenReturn
(
transformed
);
when
(
bitmapTransformation
.
transform
(
eq
(
harness
.
bitmapResource
),
eq
(
harness
.
width
),
eq
(
harness
.
height
)))
.
thenReturn
(
transformedBitmapResource
);
Resource
<
GifBitmapWrapper
>
transformed
=
harness
.
transformation
.
transform
(
harness
.
resource
,
harness
.
width
,
harness
.
height
);
GifBitmapWrapper
gifBitmap
=
mock
(
GifBitmapWrapper
.
class
);
assertNotSame
(
harness
.
resource
,
transformed
);
when
(
gifBitmap
.
getBitmapResource
()).
thenReturn
(
initial
);
assertEquals
(
transformedBitmapResource
,
transformed
.
get
().
getBitmapResource
());
Resource
<
GifBitmapWrapper
>
gifBitmapResource
=
mock
(
Resource
.
class
);
}
when
(
gifBitmapResource
.
get
()).
thenReturn
(
gifBitmap
);
@Test
public
void
testReturnsOriginalResourceIfTransformationDoesNotTransformGivenBitmapResource
()
{
BitmapResourceHarness
harness
=
new
BitmapResourceHarness
();
assertEquals
(
transformed
,
transformation
.
transform
(
gifBitmapResource
,
dimens
,
dimens
).
get
()
when
(
bitmapTransformation
.
transform
(
eq
(
harness
.
bitmapResource
),
eq
(
harness
.
width
),
eq
(
harness
.
height
)))
.
getBitmapResource
());
.
thenReturn
(
harness
.
bitmapResource
);
Resource
<
GifBitmapWrapper
>
transformed
=
harness
.
transformation
.
transform
(
harness
.
resource
,
harness
.
width
,
harness
.
height
);
assertSame
(
harness
.
resource
,
transformed
);
}
}
@Test
@Test
public
void
testReturnsNewGifBitmapResourceIfNoBitmapResource
()
{
public
void
testReturnsOriginalResourceIfBitmapTransformationIsGivenButResourceHasNoBitmapResource
()
{
GifBitmapWrapper
gifBitmap
=
mock
(
GifBitmapWrapper
.
class
);
BitmapResourceHarness
harness
=
new
BitmapResourceHarness
();
Resource
<
GifBitmapWrapper
>
gifBitmapResource
=
mock
(
Resource
.
class
);
when
(
harness
.
gifBitmapWrapper
.
getBitmapResource
()).
thenReturn
(
null
);
when
(
gifBitmapResource
.
get
()).
thenReturn
(
gifBitmap
);
GifData
gifData
=
mock
(
GifData
.
class
);
Resource
<
GifBitmapWrapper
>
transformed
=
harness
.
transformation
.
transform
(
harness
.
resource
,
harness
.
width
,
Resource
<
GifData
>
gifDataResource
=
mock
(
Resource
.
class
);
harness
.
height
);
when
(
gifDataResource
.
get
()).
thenReturn
(
gifData
);
when
(
gifBitmap
.
getGifResource
()).
thenReturn
(
gifDataResource
);
assert
NotSame
(
gifBitmapResource
,
transformation
.
transform
(
gifBitmapResource
,
100
,
100
)
);
assert
Same
(
harness
.
resource
,
transformed
);
}
}
@Test
@Test
public
void
testReturnsGivenResourceIfWrappedTransformationDoesNotTransformBitmapResource
()
{
public
void
testAppliesGifTransformationIfGifTransformationGivenAndResourceHasGifResource
()
{
int
dimens
=
321
;
GifResourceHarness
harness
=
new
GifResourceHarness
();
Resource
<
Bitmap
>
initial
=
mock
(
Resource
.
class
);
Resource
<
GifData
>
transformedGifResource
=
mock
(
Resource
.
class
);
GifBitmapWrapper
gifBitmap
=
mock
(
GifBitmapWrapper
.
class
);
when
(
gifTransformation
.
transform
(
eq
(
harness
.
gifResource
),
eq
(
harness
.
width
),
eq
(
harness
.
height
)))
when
(
gifBitmap
.
getBitmapResource
()).
thenReturn
(
initial
);
.
thenReturn
(
transformedGifResource
);
Resource
<
GifBitmapWrapper
>
gifBitmapResource
=
mock
(
Resource
.
class
);
Resource
<
GifBitmapWrapper
>
transformed
=
harness
.
transformation
.
transform
(
harness
.
resource
,
harness
.
width
,
when
(
gifBitmapResource
.
get
()).
thenReturn
(
gifBitmap
);
harness
.
height
);
assertNotSame
(
harness
.
resource
,
transformed
);
assertEquals
(
transformedGifResource
,
transformed
.
get
().
getGifResource
());
}
@Test
public
void
testReturnsOriginalresourceIfTransformationDoesNotTransformGivenGifResource
()
{
GifResourceHarness
harness
=
new
GifResourceHarness
();
when
(
gifTransformation
.
transform
(
eq
(
harness
.
gifResource
),
eq
(
harness
.
width
),
eq
(
harness
.
height
)))
.
thenReturn
(
harness
.
gifResource
);
when
(
bitmapTransformation
.
transform
(
eq
(
initial
),
eq
(
dimens
),
eq
(
dimens
))).
thenReturn
(
initial
);
Resource
<
GifBitmapWrapper
>
transformed
=
harness
.
transformation
.
transform
(
harness
.
resource
,
harness
.
width
,
harness
.
height
);
assertEquals
(
gifBitmapResource
,
transformation
.
transform
(
gifBitmapResource
,
dimens
,
dimens
));
assertSame
(
harness
.
resource
,
transformed
);
}
@Test
public
void
testReturnsOriginalResourceIfGifTransformationIsGivenButResourceHasNoGifResource
()
{
GifResourceHarness
harness
=
new
GifResourceHarness
();
when
(
harness
.
gifBitmapWrapper
.
getGifResource
()).
thenReturn
(
null
);
Resource
<
GifBitmapWrapper
>
transformed
=
harness
.
transformation
.
transform
(
harness
.
resource
,
harness
.
width
,
harness
.
height
);
assertSame
(
harness
.
resource
,
transformed
);
}
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录