Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉迷打码的小凳子
glide
提交
b4460406
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,发现更多精彩内容 >>
提交
b4460406
编写于
3月 23, 2014
作者:
S
Sam Judd
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Bring back special handling for views using setTag
上级
214d5d49
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
292 addition
and
209 deletion
+292
-209
library/src/com/bumptech/glide/Glide.java
library/src/com/bumptech/glide/Glide.java
+61
-26
library/src/com/bumptech/glide/ListPreloader.java
library/src/com/bumptech/glide/ListPreloader.java
+5
-9
library/src/com/bumptech/glide/presenter/target/BaseTarget.java
...y/src/com/bumptech/glide/presenter/target/BaseTarget.java
+23
-0
library/src/com/bumptech/glide/presenter/target/ImageViewTarget.java
.../com/bumptech/glide/presenter/target/ImageViewTarget.java
+7
-140
library/src/com/bumptech/glide/presenter/target/SimpleTarget.java
...src/com/bumptech/glide/presenter/target/SimpleTarget.java
+11
-28
library/src/com/bumptech/glide/presenter/target/ViewTarget.java
...y/src/com/bumptech/glide/presenter/target/ViewTarget.java
+178
-0
samples/flickr/src/com/bumptech/glide/samples/flickr/FlickrPhotoList.java
...rc/com/bumptech/glide/samples/flickr/FlickrPhotoList.java
+7
-6
未找到文件。
library/src/com/bumptech/glide/Glide.java
浏览文件 @
b4460406
...
@@ -2,11 +2,14 @@ package com.bumptech.glide;
...
@@ -2,11 +2,14 @@ package com.bumptech.glide;
import
android.content.Context
;
import
android.content.Context
;
import
android.graphics.Bitmap
;
import
android.graphics.Bitmap
;
import
android.graphics.drawable.Drawable
;
import
android.net.Uri
;
import
android.net.Uri
;
import
android.os.ParcelFileDescriptor
;
import
android.os.ParcelFileDescriptor
;
import
android.util.Log
;
import
android.util.Log
;
import
android.view.View
;
import
android.view.animation.Animation
;
import
android.view.animation.Animation
;
import
android.view.animation.AnimationUtils
;
import
android.view.animation.AnimationUtils
;
import
android.widget.ImageView
;
import
com.bumptech.glide.loader.bitmap.BaseBitmapLoadFactory
;
import
com.bumptech.glide.loader.bitmap.BaseBitmapLoadFactory
;
import
com.bumptech.glide.loader.bitmap.model.file_descriptor.FileDescriptorFileLoader
;
import
com.bumptech.glide.loader.bitmap.model.file_descriptor.FileDescriptorFileLoader
;
import
com.bumptech.glide.loader.bitmap.model.file_descriptor.FileDescriptorModelLoader
;
import
com.bumptech.glide.loader.bitmap.model.file_descriptor.FileDescriptorModelLoader
;
...
@@ -30,6 +33,8 @@ import com.bumptech.glide.loader.bitmap.transformation.None;
...
@@ -30,6 +33,8 @@ import com.bumptech.glide.loader.bitmap.transformation.None;
import
com.bumptech.glide.loader.bitmap.transformation.TransformationLoader
;
import
com.bumptech.glide.loader.bitmap.transformation.TransformationLoader
;
import
com.bumptech.glide.presenter.ImagePresenter
;
import
com.bumptech.glide.presenter.ImagePresenter
;
import
com.bumptech.glide.presenter.target.Target
;
import
com.bumptech.glide.presenter.target.Target
;
import
com.bumptech.glide.presenter.target.ImageViewTarget
;
import
com.bumptech.glide.presenter.target.ViewTarget
;
import
com.bumptech.glide.resize.ImageManager
;
import
com.bumptech.glide.resize.ImageManager
;
import
com.bumptech.glide.resize.load.BitmapDecoder
;
import
com.bumptech.glide.resize.load.BitmapDecoder
;
import
com.bumptech.glide.resize.load.Downsampler
;
import
com.bumptech.glide.resize.load.Downsampler
;
...
@@ -219,6 +224,11 @@ public class Glide {
...
@@ -219,6 +224,11 @@ public class Glide {
}
}
}
}
@SuppressWarnings
(
"unchecked"
)
private
<
T
,
Y
>
ModelLoaderFactory
<
T
,
Y
>
getFactory
(
T
model
,
Class
<
Y
>
resourceClass
)
{
return
loaderFactory
.
getFactory
((
Class
<
T
>)
model
.
getClass
(),
resourceClass
);
}
/**
/**
* Build a {@link ModelLoader} for the given model class using a registered factory.
* Build a {@link ModelLoader} for the given model class using a registered factory.
*
*
...
@@ -253,26 +263,36 @@ public class Glide {
...
@@ -253,26 +263,36 @@ public class Glide {
return
buildModelLoader
(
modelClass
,
ParcelFileDescriptor
.
class
,
context
);
return
buildModelLoader
(
modelClass
,
ParcelFileDescriptor
.
class
,
context
);
}
}
@SuppressWarnings
(
"unchecked"
)
private
<
T
,
Y
>
ModelLoaderFactory
<
T
,
Y
>
getFactory
(
T
model
,
Class
<
Y
>
resourceClass
)
{
return
loaderFactory
.
getFactory
((
Class
<
T
>)
model
.
getClass
(),
resourceClass
);
}
/**
/**
* Cancel any pending loads Glide may have for the target
. After the load is cancelled Glide will not load
* Cancel any pending loads Glide may have for the target
and free any resources (such as {@link Bitmap}s) that may
*
a placeholder or bitmap into the target so it is safe to do so yourself until you start another loa
d.
*
have been loaded for the target so they may be reuse
d.
*
*
* @param target The Target to cancel loads for
* @param target The Target to cancel loads for.
* @return True iff Glide had ever been asked to load an image for this target
*/
*/
public
static
boolean
cancel
(
Target
target
)
{
public
static
void
clear
(
Target
target
)
{
ImagePresenter
current
=
target
.
getImagePresenter
();
ImagePresenter
imagePresenter
=
target
.
getImagePresenter
();
final
boolean
cancelled
=
current
!=
null
;
if
(
imagePresenter
!=
null
)
{
if
(
cancelled
)
{
imagePresenter
.
clear
();
current
.
clear
();
}
}
}
return
cancelled
;
/**
* Cancel any pending loads Glide may have for the view and free any resources that may have been loaded for the
* view.
*
* <p>
* Note that this will only work if {@link View#setTag(Object)} is not called on this view outside of Glide.
* </p>
*
* @see #clear(Target).
*
* @param view The view to cancel loads and free resources for.
* @throws IllegalArgumentException if an object other than Glide's metadata is set as the view's tag.
*/
public
static
void
clear
(
View
view
)
{
Target
viewTarget
=
new
CancelTarget
(
view
);
clear
(
viewTarget
);
}
}
/**
/**
...
@@ -317,7 +337,6 @@ public class Glide {
...
@@ -317,7 +337,6 @@ public class Glide {
*/
*/
public
<
T
>
VideoModelRequest
<
T
>
using
(
final
FileDescriptorModelLoader
<
T
>
modelLoader
)
{
public
<
T
>
VideoModelRequest
<
T
>
using
(
final
FileDescriptorModelLoader
<
T
>
modelLoader
)
{
return
new
VideoModelRequest
<
T
>(
context
,
modelLoaderToFactory
(
modelLoader
));
return
new
VideoModelRequest
<
T
>(
context
,
modelLoaderToFactory
(
modelLoader
));
}
}
/**
/**
...
@@ -775,27 +794,31 @@ public class Glide {
...
@@ -775,27 +794,31 @@ public class Glide {
* Set the target the image will be loaded into.
* Set the target the image will be loaded into.
*
*
* @param target The target to load te image for
* @param target The target to load te image for
* @return The given target.
*/
*/
public
<
Y
extends
Target
>
Y
into
(
Y
target
)
{
public
<
Y
extends
Target
>
Y
into
(
Y
target
)
{
return
finish
(
target
);
return
finish
(
target
);
}
}
/**
* Sets the {@link ImageView} the image will be loaded into, cancels any existing loads into the view, and frees
* any resources Glide has loaded into the view so they may be reused.
*
* @see #clear(View)
*
* @param view The view to cancel previous loads for and load the new image into.
* @return The {@link ImageViewTarget} used to wrap the given {@link ImageView}.
*/
public
ImageViewTarget
into
(
ImageView
view
)
{
return
into
(
new
ImageViewTarget
(
view
));
}
private
<
Y
extends
Target
>
Y
finish
(
Y
target
)
{
private
<
Y
extends
Target
>
Y
finish
(
Y
target
)
{
ImagePresenter
<
ModelType
,
Y
>
imagePresenter
=
get
ImagePresenter
(
target
);
ImagePresenter
<
ModelType
,
Y
>
imagePresenter
=
build
ImagePresenter
(
target
);
imagePresenter
.
setModel
(
model
);
imagePresenter
.
setModel
(
model
);
return
target
;
return
target
;
}
}
@SuppressWarnings
(
"unchecked"
)
private
<
Y
extends
Target
>
ImagePresenter
<
ModelType
,
Y
>
getImagePresenter
(
Y
target
)
{
final
ImagePresenter
<
ModelType
,
Y
>
current
=
target
.
getImagePresenter
();
if
(
current
!=
null
)
{
current
.
clear
();
}
return
buildImagePresenter
(
target
);
}
private
<
Y
extends
Target
>
ImagePresenter
<
ModelType
,
Y
>
buildImagePresenter
(
final
Y
target
)
{
private
<
Y
extends
Target
>
ImagePresenter
<
ModelType
,
Y
>
buildImagePresenter
(
final
Y
target
)
{
TransformationLoader
<
ModelType
>
transformationLoader
=
getFinalTransformationLoader
();
TransformationLoader
<
ModelType
>
transformationLoader
=
getFinalTransformationLoader
();
...
@@ -885,4 +908,16 @@ public class Glide {
...
@@ -885,4 +908,16 @@ public class Glide {
public
void
teardown
()
{
}
public
void
teardown
()
{
}
};
};
}
}
private
static
class
CancelTarget
extends
ViewTarget
<
View
>
{
public
CancelTarget
(
View
view
)
{
super
(
view
);
}
@Override
public
void
onImageReady
(
Bitmap
bitmap
)
{
}
@Override
public
void
setPlaceholder
(
Drawable
placeholder
)
{
}
}
}
}
library/src/com/bumptech/glide/ListPreloader.java
浏览文件 @
b4460406
...
@@ -4,7 +4,7 @@ import android.annotation.TargetApi;
...
@@ -4,7 +4,7 @@ import android.annotation.TargetApi;
import
android.graphics.Bitmap
;
import
android.graphics.Bitmap
;
import
android.os.Build
;
import
android.os.Build
;
import
android.widget.AbsListView
;
import
android.widget.AbsListView
;
import
com.bumptech.glide.presenter.target.
Simpl
eTarget
;
import
com.bumptech.glide.presenter.target.
Bas
eTarget
;
import
java.util.ArrayDeque
;
import
java.util.ArrayDeque
;
import
java.util.LinkedList
;
import
java.util.LinkedList
;
...
@@ -136,7 +136,7 @@ public abstract class ListPreloader<T> implements AbsListView.OnScrollListener {
...
@@ -136,7 +136,7 @@ public abstract class ListPreloader<T> implements AbsListView.OnScrollListener {
private
void
cancelAll
()
{
private
void
cancelAll
()
{
for
(
int
i
=
0
;
i
<
maxPreload
;
i
++)
{
for
(
int
i
=
0
;
i
<
maxPreload
;
i
++)
{
Glide
.
c
ancel
(
preloadTargetQueue
.
next
(
0
,
0
));
Glide
.
c
lear
(
preloadTargetQueue
.
next
(
0
,
0
));
}
}
}
}
...
@@ -165,7 +165,7 @@ public abstract class ListPreloader<T> implements AbsListView.OnScrollListener {
...
@@ -165,7 +165,7 @@ public abstract class ListPreloader<T> implements AbsListView.OnScrollListener {
}
}
}
}
private
static
class
PreloadTarget
extends
Simpl
eTarget
{
private
static
class
PreloadTarget
extends
Bas
eTarget
{
private
int
photoHeight
;
private
int
photoHeight
;
private
int
photoWidth
;
private
int
photoWidth
;
...
@@ -173,13 +173,9 @@ public abstract class ListPreloader<T> implements AbsListView.OnScrollListener {
...
@@ -173,13 +173,9 @@ public abstract class ListPreloader<T> implements AbsListView.OnScrollListener {
public
void
onImageReady
(
Bitmap
bitmap
)
{
}
public
void
onImageReady
(
Bitmap
bitmap
)
{
}
@Override
@Override
p
rotected
int
getWidth
(
)
{
p
ublic
void
getSize
(
SizeReadyCallback
cb
)
{
return
photoWidth
;
cb
.
onSizeReady
(
photoWidth
,
photoHeight
)
;
}
}
@Override
protected
int
getHeight
()
{
return
photoHeight
;
}
}
}
}
}
library/src/com/bumptech/glide/presenter/target/BaseTarget.java
浏览文件 @
b4460406
package
com.bumptech.glide.presenter.target
;
package
com.bumptech.glide.presenter.target
;
import
android.graphics.Bitmap
;
import
android.graphics.drawable.Drawable
;
import
android.view.View
;
import
android.view.animation.Animation
;
import
com.bumptech.glide.Glide
;
import
com.bumptech.glide.presenter.ImagePresenter
;
import
com.bumptech.glide.presenter.ImagePresenter
;
/**
* A base {@link Target} for loading {@link Bitmap}s that provides basic or empty implementations for most methods.
*
* <p>
* For maximum efficiency, clear this target when you have finished using or displaying the {@link Bitmap} loaded
* into it using {@link Glide#clear(Target)}.
* </p>
*
* <p>
* For loading {@link Bitmap}s into {@link View}s, {@link ViewTarget} is preferable to this class.
* </p>
*/
public
abstract
class
BaseTarget
implements
Target
{
public
abstract
class
BaseTarget
implements
Target
{
private
ImagePresenter
imagePresenter
;
private
ImagePresenter
imagePresenter
;
...
@@ -15,4 +32,10 @@ public abstract class BaseTarget implements Target {
...
@@ -15,4 +32,10 @@ public abstract class BaseTarget implements Target {
public
ImagePresenter
getImagePresenter
()
{
public
ImagePresenter
getImagePresenter
()
{
return
imagePresenter
;
return
imagePresenter
;
}
}
@Override
public
void
setPlaceholder
(
Drawable
placeholder
)
{
}
@Override
public
void
startAnimation
(
Animation
animation
)
{
}
}
}
library/src/com/bumptech/glide/presenter/target/ImageViewTarget.java
浏览文件 @
b4460406
...
@@ -18,154 +18,21 @@ import static android.view.ViewGroup.LayoutParams;
...
@@ -18,154 +18,21 @@ import static android.view.ViewGroup.LayoutParams;
/**
/**
* A target wrapping an ImageView. Obtains the runtime dimensions of the ImageView.
* A target wrapping an ImageView. Obtains the runtime dimensions of the ImageView.
*/
*/
public
class
ImageViewTarget
extends
BaseTarget
{
public
class
ImageViewTarget
extends
ViewTarget
<
ImageView
>
{
private
static
final
String
TAG
=
"ImageViewTarget"
;
private
final
ImageView
view
;
private
final
ImageView
imageView
;
private
final
SizeDeterminer
sizeDeterminer
;
public
ImageViewTarget
(
ImageView
imageView
)
{
public
ImageViewTarget
(
ImageView
view
)
{
this
.
imageView
=
imageView
;
super
(
view
);
this
.
sizeDeterminer
=
new
SizeDeterminer
(
imageView
);
this
.
view
=
view
;
}
public
ImageView
getView
()
{
return
imageView
;
}
}
@Override
@Override
public
void
onImageReady
(
Bitmap
bitmap
)
{
public
void
onImageReady
(
Bitmap
bitmap
)
{
imageV
iew
.
setImageBitmap
(
bitmap
);
v
iew
.
setImageBitmap
(
bitmap
);
}
}
@Override
@Override
public
void
setPlaceholder
(
Drawable
placeholder
)
{
public
void
setPlaceholder
(
Drawable
placeholder
)
{
imageView
.
setImageDrawable
(
placeholder
);
view
.
setImageDrawable
(
placeholder
);
}
@Override
public
void
getSize
(
SizeReadyCallback
cb
)
{
sizeDeterminer
.
getSize
(
cb
);
}
@Override
public
void
startAnimation
(
Animation
animation
)
{
imageView
.
clearAnimation
();
imageView
.
startAnimation
(
animation
);
}
@Override
public
int
hashCode
()
{
return
imageView
.
hashCode
();
}
@Override
public
boolean
equals
(
Object
o
)
{
if
(
o
==
null
)
{
return
false
;
}
else
if
(!(
o
instanceof
ImageViewTarget
))
{
return
false
;
}
ImageViewTarget
other
=
(
ImageViewTarget
)
o
;
return
imageView
.
equals
(
other
.
imageView
);
}
private
static
class
SizeDeterminer
{
private
final
View
view
;
private
SizeReadyCallback
cb
;
private
SizeDeterminerLayoutListener
layoutListener
;
private
void
checkCurrentDimens
()
{
if
(
cb
==
null
)
return
;
boolean
calledCallback
=
true
;
LayoutParams
layoutParams
=
view
.
getLayoutParams
();
if
(
isViewSizeValid
())
{
cb
.
onSizeReady
(
view
.
getWidth
(),
view
.
getHeight
());
}
else
if
(
isLayoutParamsSizeValid
())
{
cb
.
onSizeReady
(
layoutParams
.
width
,
layoutParams
.
height
);
}
else
{
calledCallback
=
false
;
}
if
(
calledCallback
)
{
cb
=
null
;
// Keep a reference to the layout listener and remove it here
// rather than having the observer remove itself because the observer
// we add the listener to will be almost immediately merged into
// another observer and will therefore never be alive. If we instead
// keep a reference to the listener and remove it here, we get the
// current view tree observer and should succeed.
ViewTreeObserver
observer
=
view
.
getViewTreeObserver
();
if
(
observer
.
isAlive
())
{
observer
.
removeGlobalOnLayoutListener
(
layoutListener
);
}
}
}
public
SizeDeterminer
(
View
view
)
{
this
.
view
=
view
;
}
public
void
getSize
(
SizeReadyCallback
cb
)
{
this
.
cb
=
null
;
LayoutParams
layoutParams
=
view
.
getLayoutParams
();
if
(
isViewSizeValid
())
{
cb
.
onSizeReady
(
view
.
getWidth
(),
view
.
getHeight
());
}
else
if
(
isLayoutParamsSizeValid
())
{
cb
.
onSizeReady
(
layoutParams
.
width
,
layoutParams
.
height
);
}
else
if
(
isUsingWrapContent
())
{
WindowManager
windowManager
=
(
WindowManager
)
view
.
getContext
().
getSystemService
(
Context
.
WINDOW_SERVICE
);
Display
display
=
windowManager
.
getDefaultDisplay
();
final
int
width
=
display
.
getWidth
();
final
int
height
=
display
.
getHeight
();
if
(
Log
.
isLoggable
(
TAG
,
Log
.
WARN
))
{
Log
.
w
(
TAG
,
"Trying to load image into ImageView using WRAP_CONTENT, defaulting to screen"
+
" dimensions: ["
+
width
+
"x"
+
height
+
"]. Give the view an actual width and height "
+
" for better performance."
);
}
cb
.
onSizeReady
(
display
.
getWidth
(),
display
.
getHeight
());
}
else
{
this
.
cb
=
cb
;
final
ViewTreeObserver
observer
=
view
.
getViewTreeObserver
();
layoutListener
=
new
SizeDeterminerLayoutListener
(
this
);
observer
.
addOnGlobalLayoutListener
(
layoutListener
);
}
}
private
boolean
isViewSizeValid
()
{
return
view
.
getWidth
()
>
0
&&
view
.
getHeight
()
>
0
;
}
private
boolean
isUsingWrapContent
()
{
final
LayoutParams
layoutParams
=
view
.
getLayoutParams
();
return
layoutParams
!=
null
&&
(
layoutParams
.
width
==
LayoutParams
.
WRAP_CONTENT
||
layoutParams
.
height
==
LayoutParams
.
WRAP_CONTENT
);
}
private
boolean
isLayoutParamsSizeValid
()
{
final
LayoutParams
layoutParams
=
view
.
getLayoutParams
();
return
layoutParams
!=
null
&&
(
layoutParams
.
width
>
0
&&
layoutParams
.
height
>
0
);
}
private
static
class
SizeDeterminerLayoutListener
implements
ViewTreeObserver
.
OnGlobalLayoutListener
{
private
final
WeakReference
<
SizeDeterminer
>
sizeDeterminerRef
;
public
SizeDeterminerLayoutListener
(
SizeDeterminer
sizeDeterminer
)
{
sizeDeterminerRef
=
new
WeakReference
<
SizeDeterminer
>(
sizeDeterminer
);
}
@Override
public
void
onGlobalLayout
()
{
if
(
Log
.
isLoggable
(
TAG
,
Log
.
VERBOSE
))
{
Log
.
v
(
TAG
,
"OnGlobalLayoutListener called listener="
+
this
);
}
SizeDeterminer
sizeDeterminer
=
sizeDeterminerRef
.
get
();
if
(
sizeDeterminer
!=
null
)
{
sizeDeterminer
.
checkCurrentDimens
();
}
}
}
}
}
}
}
library/src/com/bumptech/glide/presenter/target/SimpleTarget.java
浏览文件 @
b4460406
package
com.bumptech.glide.presenter.target
;
package
com.bumptech.glide.presenter.target
;
import
android.graphics.drawable.Drawable
;
import
android.view.animation.Animation
;
import
com.bumptech.glide.presenter.ImagePresenter
;
/**
/**
* A simpler interface for targets with default (usually noop) implementations of non essential methods.
* A simpler interface for targets with default (usually noop) implementations of non essential methods that allows the
* caller to specify an exact width/height.
*/
*/
@SuppressWarnings
(
"unused"
)
public
abstract
class
SimpleTarget
extends
BaseTarget
{
public
abstract
class
SimpleTarget
extends
BaseTarget
{
private
final
int
width
;
private
final
int
height
;
@Override
public
SimpleTarget
(
int
width
,
int
height
)
{
public
void
setPlaceholder
(
Drawable
placeholder
)
{
}
this
.
width
=
width
;
this
.
height
=
height
;
@Override
}
public
void
startAnimation
(
Animation
animation
)
{
}
/**
* A default implementation that calls {@link com.bumptech.glide.presenter.target.Target.SizeReadyCallback}
* synchronously with {@link #getWidth()} and {@link #getHeight()}
*
* @param cb The callback that must be called when the size of the target has been determined
*/
@Override
@Override
public
void
getSize
(
SizeReadyCallback
cb
)
{
public
final
void
getSize
(
SizeReadyCallback
cb
)
{
cb
.
onSizeReady
(
getWidth
(),
getHeight
()
);
cb
.
onSizeReady
(
width
,
height
);
}
}
/**
* @return The height of this target, which will be used to determine how to load and crop the Bitmap.
*/
protected
abstract
int
getWidth
();
/**
* @return The width of this target, which will be used to determine how to load and crop the Bitmap.
*/
protected
abstract
int
getHeight
();
}
}
library/src/com/bumptech/glide/presenter/target/ViewTarget.java
0 → 100644
浏览文件 @
b4460406
package
com.bumptech.glide.presenter.target
;
import
android.content.Context
;
import
android.graphics.Bitmap
;
import
android.util.Log
;
import
android.view.Display
;
import
android.view.View
;
import
android.view.ViewGroup
;
import
android.view.ViewTreeObserver
;
import
android.view.WindowManager
;
import
android.view.animation.Animation
;
import
android.widget.ListView
;
import
com.bumptech.glide.presenter.ImagePresenter
;
import
java.lang.ref.WeakReference
;
/**
* A base {@link Target} for loading {@link Bitmap}s into {@link View}s that provides default implementations for most
* most methods and can determine the size of views using a {@link ViewTreeObserver.OnGlobalLayoutListener}.
*
* <p>
* To detect {@link View} reuse in {@link ListView} or any {@link ViewGroup} that reuses views, this class uses the
* {@link View#setTag(Object)} method to store some metadata so that if a view is reused, any previous loads or
* resources from previous loads can be cancelled or reused.
* </p>
*
* <p>
* Any calls to {@link View#setTag(Object)}} on a View given to this class will result in excessive allocations and
* and/or {@link IllegalArgumentException}s. If you must call {@link View#setTag(Object)} on a view, consider
* using {@link BaseTarget} or {@link SimpleTarget} instead.
* </p>
*
* @param <T> The specific subclass of view wrapped by this target.
*/
public
abstract
class
ViewTarget
<
T
extends
View
>
implements
Target
{
private
static
final
String
TAG
=
"ViewTarget"
;
private
final
T
view
;
private
final
SizeDeterminer
sizeDeterminer
;
public
ViewTarget
(
T
view
)
{
this
.
view
=
view
;
sizeDeterminer
=
new
SizeDeterminer
(
view
);
}
public
T
getView
()
{
return
view
;
}
@Override
public
void
getSize
(
SizeReadyCallback
cb
)
{
sizeDeterminer
.
getSize
(
cb
);
}
@Override
public
void
startAnimation
(
Animation
animation
)
{
view
.
clearAnimation
();
view
.
startAnimation
(
animation
);
}
@Override
public
void
setImagePresenter
(
ImagePresenter
imagePresenter
)
{
view
.
setTag
(
imagePresenter
);
}
@Override
public
ImagePresenter
getImagePresenter
()
{
Object
tag
=
view
.
getTag
();
ImagePresenter
presenter
=
null
;
if
(
tag
!=
null
)
{
if
((
tag
instanceof
ImagePresenter
))
{
presenter
=
(
ImagePresenter
)
tag
;
}
else
{
throw
new
IllegalArgumentException
(
"You must not call setTag() on a view Glide is targeting"
);
}
}
return
presenter
;
}
private
static
class
SizeDeterminer
{
private
final
View
view
;
private
SizeReadyCallback
cb
;
private
SizeDeterminerLayoutListener
layoutListener
;
public
SizeDeterminer
(
View
view
)
{
this
.
view
=
view
;
}
private
void
checkCurrentDimens
()
{
if
(
cb
==
null
)
return
;
boolean
calledCallback
=
true
;
ViewGroup
.
LayoutParams
layoutParams
=
view
.
getLayoutParams
();
if
(
isViewSizeValid
())
{
cb
.
onSizeReady
(
view
.
getWidth
(),
view
.
getHeight
());
}
else
if
(
isLayoutParamsSizeValid
())
{
cb
.
onSizeReady
(
layoutParams
.
width
,
layoutParams
.
height
);
}
else
{
calledCallback
=
false
;
}
if
(
calledCallback
)
{
cb
=
null
;
// Keep a reference to the layout listener and remove it here
// rather than having the observer remove itself because the observer
// we add the listener to will be almost immediately merged into
// another observer and will therefore never be alive. If we instead
// keep a reference to the listener and remove it here, we get the
// current view tree observer and should succeed.
ViewTreeObserver
observer
=
view
.
getViewTreeObserver
();
if
(
observer
.
isAlive
())
{
observer
.
removeGlobalOnLayoutListener
(
layoutListener
);
}
}
}
public
void
getSize
(
SizeReadyCallback
cb
)
{
this
.
cb
=
null
;
ViewGroup
.
LayoutParams
layoutParams
=
view
.
getLayoutParams
();
if
(
isViewSizeValid
())
{
cb
.
onSizeReady
(
view
.
getWidth
(),
view
.
getHeight
());
}
else
if
(
isLayoutParamsSizeValid
())
{
cb
.
onSizeReady
(
layoutParams
.
width
,
layoutParams
.
height
);
}
else
if
(
isUsingWrapContent
())
{
WindowManager
windowManager
=
(
WindowManager
)
view
.
getContext
().
getSystemService
(
Context
.
WINDOW_SERVICE
);
Display
display
=
windowManager
.
getDefaultDisplay
();
final
int
width
=
display
.
getWidth
();
final
int
height
=
display
.
getHeight
();
if
(
Log
.
isLoggable
(
TAG
,
Log
.
WARN
))
{
Log
.
w
(
TAG
,
"Trying to load image into ImageView using WRAP_CONTENT, defaulting to screen"
+
" dimensions: ["
+
width
+
"x"
+
height
+
"]. Give the view an actual width and height "
+
" for better performance."
);
}
cb
.
onSizeReady
(
display
.
getWidth
(),
display
.
getHeight
());
}
else
{
this
.
cb
=
cb
;
final
ViewTreeObserver
observer
=
view
.
getViewTreeObserver
();
layoutListener
=
new
SizeDeterminerLayoutListener
(
this
);
observer
.
addOnGlobalLayoutListener
(
layoutListener
);
}
}
private
boolean
isViewSizeValid
()
{
return
view
.
getWidth
()
>
0
&&
view
.
getHeight
()
>
0
;
}
private
boolean
isUsingWrapContent
()
{
final
ViewGroup
.
LayoutParams
layoutParams
=
view
.
getLayoutParams
();
return
layoutParams
!=
null
&&
(
layoutParams
.
width
==
ViewGroup
.
LayoutParams
.
WRAP_CONTENT
||
layoutParams
.
height
==
ViewGroup
.
LayoutParams
.
WRAP_CONTENT
);
}
private
boolean
isLayoutParamsSizeValid
()
{
final
ViewGroup
.
LayoutParams
layoutParams
=
view
.
getLayoutParams
();
return
layoutParams
!=
null
&&
(
layoutParams
.
width
>
0
&&
layoutParams
.
height
>
0
);
}
private
static
class
SizeDeterminerLayoutListener
implements
ViewTreeObserver
.
OnGlobalLayoutListener
{
private
final
WeakReference
<
SizeDeterminer
>
sizeDeterminerRef
;
public
SizeDeterminerLayoutListener
(
SizeDeterminer
sizeDeterminer
)
{
sizeDeterminerRef
=
new
WeakReference
<
SizeDeterminer
>(
sizeDeterminer
);
}
@Override
public
void
onGlobalLayout
()
{
if
(
Log
.
isLoggable
(
TAG
,
Log
.
VERBOSE
))
{
Log
.
v
(
TAG
,
"OnGlobalLayoutListener called listener="
+
this
);
}
SizeDeterminer
sizeDeterminer
=
sizeDeterminerRef
.
get
();
if
(
sizeDeterminer
!=
null
)
{
sizeDeterminer
.
checkCurrentDimens
();
}
}
}
}
}
samples/flickr/src/com/bumptech/glide/samples/flickr/FlickrPhotoList.java
浏览文件 @
b4460406
...
@@ -13,7 +13,6 @@ import com.actionbarsherlock.app.SherlockFragment;
...
@@ -13,7 +13,6 @@ import com.actionbarsherlock.app.SherlockFragment;
import
com.bumptech.glide.Glide
;
import
com.bumptech.glide.Glide
;
import
com.bumptech.glide.ListPreloader
;
import
com.bumptech.glide.ListPreloader
;
import
com.bumptech.glide.loader.bitmap.model.Cache
;
import
com.bumptech.glide.loader.bitmap.model.Cache
;
import
com.bumptech.glide.presenter.target.ImageViewTarget
;
import
com.bumptech.glide.samples.flickr.api.Photo
;
import
com.bumptech.glide.samples.flickr.api.Photo
;
import
java.net.URL
;
import
java.net.URL
;
...
@@ -33,8 +32,9 @@ public class FlickrPhotoList extends SherlockFragment implements PhotoViewer {
...
@@ -33,8 +32,9 @@ public class FlickrPhotoList extends SherlockFragment implements PhotoViewer {
@Override
@Override
public
void
onPhotosUpdated
(
List
<
Photo
>
photos
)
{
public
void
onPhotosUpdated
(
List
<
Photo
>
photos
)
{
currentPhotos
=
photos
;
currentPhotos
=
photos
;
if
(
adapter
!=
null
)
if
(
adapter
!=
null
)
{
adapter
.
setPhotos
(
currentPhotos
);
adapter
.
setPhotos
(
currentPhotos
);
};
}
}
@Override
@Override
...
@@ -45,18 +45,19 @@ public class FlickrPhotoList extends SherlockFragment implements PhotoViewer {
...
@@ -45,18 +45,19 @@ public class FlickrPhotoList extends SherlockFragment implements PhotoViewer {
list
.
setAdapter
(
adapter
);
list
.
setAdapter
(
adapter
);
preloader
=
new
FlickrListPreloader
(
getActivity
(),
5
);
preloader
=
new
FlickrListPreloader
(
getActivity
(),
5
);
list
.
setOnScrollListener
(
preloader
);
list
.
setOnScrollListener
(
preloader
);
if
(
currentPhotos
!=
null
)
if
(
currentPhotos
!=
null
)
{
adapter
.
setPhotos
(
currentPhotos
);
adapter
.
setPhotos
(
currentPhotos
);
}
return
result
;
return
result
;
}
}
private
static
class
ViewHolder
{
private
static
class
ViewHolder
{
private
final
TextView
titleText
;
private
final
TextView
titleText
;
private
final
ImageView
Target
imageViewTarget
;
private
final
ImageView
imageView
;
public
ViewHolder
(
ImageView
imageView
,
TextView
titleText
)
{
public
ViewHolder
(
ImageView
imageView
,
TextView
titleText
)
{
this
.
imageView
=
imageView
;
this
.
titleText
=
titleText
;
this
.
titleText
=
titleText
;
this
.
imageViewTarget
=
new
ImageViewTarget
(
imageView
);
}
}
}
}
...
@@ -153,7 +154,7 @@ public class FlickrPhotoList extends SherlockFragment implements PhotoViewer {
...
@@ -153,7 +154,7 @@ public class FlickrPhotoList extends SherlockFragment implements PhotoViewer {
.
load
(
current
)
.
load
(
current
)
.
centerCrop
()
.
centerCrop
()
.
animate
(
R
.
anim
.
fade_in
)
.
animate
(
R
.
anim
.
fade_in
)
.
into
(
viewHolder
.
imageView
Target
);
.
into
(
viewHolder
.
imageView
);
viewHolder
.
titleText
.
setText
(
current
.
title
);
viewHolder
.
titleText
.
setText
(
current
.
title
);
return
view
;
return
view
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录