Grid图片加载
来源:互联网 发布:java的数据类型 编辑:程序博客网 时间:2024/06/05 08:13
Glide是一款图片加载框架,可以在Android平台上以简单的方式加载和展示图片。
dependencies {
compile
'com.github.bumptech.glide:glide:3.7.0'
}
在清单文件中加入权限
<
uses-permission
android:name
=
"android.permission.INTERNET"
/>
调用Glide.with()方法用于创建一个图片的实例。with方法可以接受Context、Activity和Fragment类型的参数。如果调用不在Activity中或者Fragment中,可以传入ApplicationContext。
Glide支持加载各种图片资源,包括网络图片、本地图片、应用资源、Uri对象等
// 加载本地图片
File file =
new
File(getExternalCacheDir() +
"/demo.jpg"
);
Glide.with(
this
).load(file).into(imageView);
// 加载应用资源
Glide.with(
this
).load(R.drawable.image).into(imageView);
// 加载二进制流
byte
[] image = getImageBytes();
Glide.with(
this
).load(image).into(imageView);
// 加载Uri对象
Uri imageUri = getImageUri();
Glide.with(
this
).load(imageUri).into(imageView)
占位图
Glide.with(MainActivity.
this
)
.load(url)
.placeholder(R.mipmap.placeholder)
.into(mImage);
异常占位图
//错误的图片地址
String url1 =
"http://sc.net/uploads/allimg/150709/14-150FZ94211O4.jpg"
;
Glide.with(MainActivity.
this
)
.load(url1)
.placeholder(R.mipmap.placeholder)
//加载占位图
.error(R.mipmap.error)
//异常占位图
.into(mImage);
指定图片格式
Glide支持加载GIF图片的,而Picasso是不支持加载GIF图片的。
只允许加载静态图:asBitmap
String url2 =
"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3396140532,1228025744&fm=23&gp=0.jpg"
;
Glide.with(MainActivity.
this
)
.load(url2)
.asBitmap()
//只允许加载静态图
.placeholder(R.mipmap.placeholder)
//加载占位图
.error(R.mipmap.error)
//异常占位图
.into(mImage);
只允许加载动态图:.asGif()
String url2 =
"https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3396140532,1228025744&fm=23&gp=0.jpg"
;
Glide.with(MainActivity.
this
)
.load(url2)
.asGif()
//只允许加载动态图
.placeholder(R.mipmap.placeholder)
//加载占位图
.error(R.mipmap.error)
//异常占位图
.into(mImage);
指定图片大小
使用Glide,我们就不用担心图片内存浪费,甚至是内存溢出的问题。
因为Glide从来都不会直接将图片的完整尺寸全部加载到内存中,而是用多少加载多少。Glide会自动判断ImageView的大小,然后只将这么大的图片像素加载到内存当中。
当然我们也可以指定图片的固定大小
当指定图片大小的时候,要把ImageView的宽高该为包裹内容
Glide.
with
(MainActivity.
this
)
.load(url)
.asBitmap()
//只允许加载动态图
.placeholder(R.mipmap.placeholder)
//加载占位图
.error(R.mipmap.error)
//异常占位图
.override(400, 300)
.into(mImage);
3.添加图片淡入加载的效果
.crossFade()
4.用 animate() 自定义动画
从资源中的动画:
回到代码,第一个选项是传一个 Android 资源 id,即动画的资源。一个简单的例子是每个 Android 系统都提供的:slide-in-left(从左滑入)动画, android.R.anim.slide_in_left 。下面这段代码是这个动画的 XML 描述:
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
translate
android:fromXDelta
=
"-50%p"
android:toXDelta
=
"0"
android:duration
=
"@android:integer/config_mediumAnimTime"
/>
<
alpha
android:fromAlpha
=
"0.0"
android:toAlpha
=
"1.0"
android:duration
=
"@android:integer/config_mediumAnimTime"
/>
</
set
>
当然你可以创建你自己的 XML 动画。比如一个小的缩放动画,图片刚开始小的,然后逐渐增大到原尺寸。
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
set
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:fillAfter
=
"true"
>
<
scale
android:duration
=
"@android:integer/config_longAnimTime"
android:fromXScale
=
"0.1"
android:fromYScale
=
"0.1"
android:pivotX
=
"50%"
android:pivotY
=
"50%"
android:toXScale
=
"1"
android:toYScale
=
"1"
/>
</
set
>
这两个动画都可以用到 Glide 建造者中:
Glide
.with( context )
.load( eatFoodyImages[
0
] )
.animate( android.R.anim.slide_in_left )
// or R.anim.zoom_in
.into( imageView1 );
在图片从网络加载完并准备好之后将从左边滑入。
通过自定义类实现动画
这个很简单,你只需实现 void animate(View view) 方法。这个视图对象是整个 target 视图。如果它是一个自定义的视图,你要找到你的视图的子元素,并且做些必要的动画。
来看个简单的例子。假设你想要实现一个渐现动画,你得需要创建这样的动画对象:
ViewPropertyAnimation.Animator animationObject =
new
ViewPropertyAnimation.Animator() {
@Override
public
void
animate(View view) {
// if it's a custom view class, cast it here
// then find subviews and do the animations
// here, we just use the entire view for the fade animation
view.setAlpha( 0f );
ObjectAnimator fadeAnim = ObjectAnimator.ofFloat( view,
"alpha"
, 0f, 1f );
fadeAnim.setDuration(
2500
);
fadeAnim.start();
}
};
接下来,你需要在 Glide 请求中去设置这个动画:
Glide
.with( context )
.load( eatFoodyImages[
1
] )
.animate( animationObject )
.into( imageView2 );
当然,在 animate(View view) 中你的动画对象方法中, 你可以做任何你想要对视图做的事情。自由的用你的动画创建吧。
如果你要在你的自定义视图中实现,你只需要创建这个视图对象,然后在你的自定义视图中创建你的自定义方法。
5.添加加载完成监听
Glide.with(ShowImgActivity.
this
)
.load(urlString)
.centerCrop()
.error(R.drawable.failed)
.crossFade()
.into(
new
GlideDrawableImageViewTarget(imageView) {
@Override
public
void
onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
super
.onResourceReady(drawable, anim);
//在这里添加一些图片加载完成的操作
}
)};
6.图片缓存机制
Glide缓存策略
Glide默认开启磁盘缓存和内存缓存,当然也可以对单张图片进行设置特定的缓存策略。
设置图片不加入到内存缓存
Glide
.with( context )
.load( eatFoodyImages[
0
] )
.skipMemoryCache(
true
)
.into( imageViewInternet );
设置图片不加入到磁盘缓存
Glide
.with( context )
.load( eatFoodyImages[
0
] )
.diskCacheStrategy( DiskCacheStrategy.NONE )
.into( imageViewInternet );
Glide支持多种磁盘缓存策略:
DiskCacheStrategy.NONE :不缓存图片
DiskCacheStrategy.SOURCE :缓存图片源文件
DiskCacheStrategy.RESULT:缓存修改过的图片
DiskCacheStrategy.ALL:缓存所有的图片,默认
图片加载优先级
Glide支持为图片加载设置优先级,优先级高的先加载,优先级低的后加载:
private
void
loadImageWithHighPriority() {
Glide
.with( context )
.load( UsageExampleListViewAdapter.eatFoodyImages[
0
] )
.priority( Priority.HIGH )
.into( imageViewHero );
}
private
void
loadImagesWithLowPriority() {
Glide
.with( context )
.load( UsageExampleListViewAdapter.eatFoodyImages[
1
] )
.priority( Priority.LOW )
.into( imageViewLowPrioLeft );
Glide
.with( context )
.load( UsageExampleListViewAdapter.eatFoodyImages[
2
] )
.priority( Priority.LOW )
.into( imageViewLowPrioRight );
}
7.加载圆角图片
/**
* 圆形图
*
* Created by <lzh> on 2016/7/29.
*/
public
class
GlideCircleTransform
extends
BitmapTransformation {
public
GlideCircleTransform(Context context) {
super
(context);
}
@Override
protected
Bitmap transform(BitmapPool pool, Bitmap toTransform,
int
outWidth,
int
outHeight) {
return
circleCrop(pool, toTransform);
}
private
static
Bitmap circleCrop(BitmapPool pool, Bitmap source) {
if
(source ==
null
)
return
null
;
int
size = Math.min(source.getWidth(), source.getHeight());
int
x = (source.getWidth() - size) /
2
;
int
y = (source.getHeight() - size) /
2
;
// TODO this could be acquired from the pool too
Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
if
(result ==
null
) {
result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
}
Canvas canvas =
new
Canvas(result);
Paint paint =
new
Paint();
paint.setShader(
new
BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
paint.setAntiAlias(
true
);
float
r = size / 2f;
canvas.drawCircle(r, r, r, paint);
return
result;
}
@Override
public
String getId() {
return
getClass().getName();
}
}
**然后使用的时候只要加上这句话就行了
.transform(new GlideCircleTransform(context))**
Glide.with(mContext)
.load(imageUrl)
.transform(
new
GlideCircleTransform(mContext))
.into(holder.imageView);
注意事项:
不能直接给要使用glide的imageview设置tag;
因为glide在加载图片的时候用到了tag,会造成冲突,并报错;
当要用到tag写逻辑代码的时候,可以这样
.setTag(R.string.xxx,xxx);并.getTag(R.string.xxx);
- Grid图片加载
- WPF的Grid面板加载图片鼠标画框
- Ext 重新加载grid
- EasyUi加载Grid数据
- extjs-grid数据加载
- Grid-Json数据加载
- DevExpress GridControl 用路径加载图片,插入到Grid列中
- DevExpress GridControl 用路径加载图片,插入到Grid列中
- Grid 自动加载异常处理
- Extjs Grid store加载数据
- Extjs 代理加载grid store
- 加载图片
- 图片加载
- 图片加载
- 图片加载
- 图片加载
- 图片加载
- 加载图片
- ubuntu 配置openjdk
- 如何查看OpenCV中自带函数的源代码
- 关于jquery中的extend方法的用法
- RancherOS正式版发布:最轻量的开源容器Linux操作系统
- java 如果一个变量是double或者float,如何初始化为0?
- Grid图片加载
- YV12格式介绍
- Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400
- ROS环境变量的设置
- Get Record(s) by Ids
- HDU 1242 Rescue
- 快速排序详解
- margin布局居中显示
- android中的broadcastReceiver