您的位置:首页 > 移动开发 > Android开发

Glide使用记录2

2016-07-18 21:40 447 查看

1.使用自定义控件显示图片

获取请求的bitmap对象:

private SimpleTarget target = new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
// do something with the bitmap
// for demonstration purposes, let's just set it to an ImageView
imageView1.setImageBitmap( bitmap );
}
};

private void loadImageSimpleTarget() {
Glide
.with( context ) // could be an issue!
.load( eatFoodyImages[0] )
.asBitmap()
.into( target );
}


当然,还可以设置获取Bitmap的大小

private SimpleTarget target2 = new SimpleTarget<Bitmap>( 250, 250 ) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
imageView2.setImageBitmap( bitmap );
}
};


对于自定义的View,可以使用一下方式实现设置图片显示:

// 自定义view
public class FutureStudioView extends FrameLayout {

public void setImage(Drawable drawable) {
iv = (ImageView) findViewById( R.id.custom_view_image );

iv.setImageDrawable( drawable );
}
}

private void loadImageViewTarget() {
FutureStudioView customView = (FutureStudioView) findViewById( R.id.custom_view );

viewTarget = new ViewTarget<FutureStudioView, GlideDrawable>( customView ) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
this.view.setImage( resource.getCurrent() );
}
};

Glide
.with( context.getApplicationContext() ) // safer!
.load( eatFoodyImages[2] )
.into( viewTarget );
}


2.使用okhttp 或volley 来完成网络请求

okhttp:

在app.gradle中添加以下代码,Gradle 会自动合并必要的 GlideModule 到你的 Android.Manifest。Glide 会认可在 manifest 中的存在,然后使用 OkHttp 做到所有的网络连接。

compile 'com.github.bumptech.glide:okhttp-integration:1.3.1@aar'
compile 'com.squareup.okhttp:okhttp:2.5.0'


volley:

使用一下代码将添加 Volley 并集成该库到你的项目中。集成库添加到 GlideModule 到你的 Android.Manifest。Glide 会自动认出它,然后使用 Volley 作为网络库。并不要求做其他的配置!

compile 'com.github.bumptech.glide:volley-integration:1.3.1@aar'
compile 'com.mcxiaoke.volley:library:1.0.8'


3.自定义配置

添加class:

public class SimpleGlideModule implements GlideModule {
@Override public void applyOptions(Context context, GlideBuilder builder) {
// todo
}

@Override public void registerComponents(Context context, Glide glide) {
// todo
}
}


请确保你将 android:name 属性改成你的包名+类名的形式,这样的引用才是正确的。就

<meta-data      android:name="io.futurestud.tutorials.glide.glidemodule.SimpleGlideModule" android:value="GlideModule" />


在applyOptions(Context context, GlideBuilder builder)方法中可以自定义一下配置:

.setMemoryCache(MemoryCache memoryCache)
.setBitmapPool(BitmapPool bitmapPool)
.setDiskCache(DiskCache.Factory diskCacheFactory)
.setDiskCacheService(ExecutorService service)
.setResizeService(ExecutorService service)
.setDecodeFormat(DecodeFormat decodeFormat)


4.添加常规异常日志记录

Glide 不能直接去访问 GenericRequest 类去设置日志,但万一一些请求出错了你是可以捕获异常的。如果你明确想要知道这个异常,创建一个监听并传 .listener() 方法到 Glide 的建造者中。

在 onException 方法中, 你可以捕获错误,并且你可以决定要做什么,比如,打个 log。重要的是如果 Glide 要在后续处理的话,如显示一个错误的占位符等情况的话,你需要返回了 false 在 onException 方法中。

private RequestListener<String, GlideDrawable> requestListener = new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
// important to return false so the error placeholder can be placed
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
return false;
}
};

Glide
.with( context )
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.listener( requestListener )
.error( R.drawable.cupcake )
.into( imageViewPlaceholder );


5.接受自签名证书的 HTTPS

Module 实例:接受自签名证书的 HTTPS
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android