Android中的Glide加载图片
注意:在Android Studio的项目的build.gradle中添加:
compile 'com.github.bumptech.glide:glide:3.6.1'
然后同步一下
目录:
- 使用Glide结合列表的样式进行图片加载
- 如果使用的是RecyclerView,可以在Adapter的onBindViewHolder方法中使用
- 当加载网络图片时,由于加载过程中图片未能及时显示,此时可能需要设置等待时的图片,通过placeHolder()方法
- 当加载图片失败时,通过error(Drawable drawable)方法设置加载失败后的图片显示
- 图片的缩放,centerCrop()和fitCenter()
- 显示gif动画
- 显示本地视频
- 缓存策略
- 优先级,设置图片加载的顺序
- 当不需要将加载的资源直接放入到ImageView中而是想获取资源的Bitmap对象
- 集成网络栈(okHttp,Volley)
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (null == convertView) {//.....}Glide.with(context).load(imageUrls[position]).into(holder.imageView);return convertView;}
@Overridepublic void onBindViewHolder(RVViewHolder holder, int position) {Glide.with(MainActivity.this).load(args[position]).into(holder.imageView);}
Glide.with(context).load(UsageExampleListViewAdapter.eatFoodyImages[0]).placeholder(R.mipmap.ic_launcher) // can also be a drawable.into(imageViewPlaceholder);
Glide.with(context).load("http://futurestud.io/non_existing_image.png").error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded.into(imageViewError);
//使用centerCrop是利用图片图填充ImageView设置的大小,如果ImageView的//Height是match_parent则图片就会被拉伸填充Glide.with(MainActivity.this).load(args[position]).centerCrop().into(holder.imageView);
//使用fitCenter即缩放图像让图像都测量出来等于或小于 ImageView 的边界范围//该图像将会完全显示,但可能不会填满整个 ImageView。Glide.with(MainActivity.this).load(args[position]).fitCenter().into(holder.imageView);
Glide.with( context ).load( gifUrl ).asGif() //判断加载的url资源是否为gif格式的资源.error( R.drawable.full_cake ).into( imageViewGif );
String filePath = "/storage/emulated/0/Pictures/example_video.mp4";Glide.with( context ).load( Uri.fromFile( new File( filePath ) ) ).into( imageViewGifAsBitmap );
Glide.with( context ).load( Images[0] ).skipMemoryCache( true ) //跳过内存缓存.into( imageViewInternet );
Glide.with( context ).load( images[0] ).diskCacheStrategy( DiskCacheStrategy.NONE ) //跳过硬盘缓存.into( imageViewInternet );
DiskCacheStrategy.NONE什么都不缓存DiskCacheStrategy.SOURCE仅仅只缓存原来的全分辨率的图像DiskCacheStrategy.RESULT仅仅缓存最终的图像,即降低分辨率后的(或者是转换后的)DiskCacheStrategy.ALL缓存所有版本的图像(默认行为)
Priority.LOWPriority.NORMALPriority.HIGHPriority.IMMEDIATE
private void loadImageWithHighPriority() {Glide.with( context ).load( mages[0] ).priority( Priority.HIGH ).into( imageViewHero );}private void loadImagesWithLowPriority() {Glide.with( context ).load( images[1] ).priority( Priority.LOW ).into( imageViewLowPrioLeft );Glide.with( context ).load( images[2] ).priority( Priority.LOW ).into( imageViewLowPrioRight );}
//括号中的300,600代表宽和高但是未有作用SimpleTarget target = new SimpleTarget<Bitmap>(300,600) {@Overridepublic void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {holder.imageView.setImageBitmap(resource);}};Glide.with(MainActivity.this).load(args[position]).asBitmap().into(target);
dependencies {// your other dependencies// ...// Glidecompile 'com.github.bumptech.glide:glide:3.6.1'// Glide's OkHttp Integrationcompile 'com.github.bumptech.glide:okhttp-integration:1.3.1@aar'compile 'com.squareup.okhttp:okhttp:2.5.0'}
dependencies {// your other dependencies// ...// Glidecompile 'com.github.bumptech.glide:glide:3.6.1'// Glide's Volley Integrationcompile 'com.github.bumptech.glide:volley-integration:1.3.1@aar'compile 'com.mcxiaoke.volley:library:1.0.8'}
Android中的Glide加载图片的更多相关文章
- Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案
我们在使用ListView异步加载图片的时候,在快速滑动或者网络不好的情况下,会出现图片错位.重复.闪烁等问题,其实这些问题总结起来就是一个问题,我们需要对这些问题进行ListView的优化. 比如L ...
- Android的ListView异步加载图片时,错位、重复、闪烁问题的分析及解决方法
Android ListView异步加载图片错位.重复.闪烁分析以及解决方案,具体问题分析以及解决方案请看下文. 我们在使用ListView异步加载图片的时候,在快速滑动或者网络不好的情况下,会出现图 ...
- Android Glide加载图片时转换为圆形、圆角、毛玻璃等图片效果
Android Glide加载图片时转换为圆形.圆角.毛玻璃等图片效果 附录1简单介绍了Android开源的图片加载框架.在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬 ...
- Glide 加载图片
//通过model获取到图片的url,将Url转换成bitmap对象: //设置不保存内存和硬盘缓存, 1 Glide.with(mContext).load(model.getVideoUrl()) ...
- RoundedImageView使用吐槽心得(RoundedImageView与Glide加载图片,第一次加载无法圆角问题)
最近使用的时候发现一个问题, RoundedImageView与Glide搭配使用的时候,第一次加载图片(内存中没有),后图片无法圆角,后来尝试各种改,最后想到了一个办法,就是让Glide加载图片的 ...
- Glide加载图片问题记录
Glide加载图片相比于Picasso而言性能较好,又比Fresco轻巧,而且又支持加载gif动图,是Google 推荐.专注平滑的滚动.简单易用.可扩展的一款图片加载框架.但是使用时还是会遇到一些问 ...
- Glide加载图片缓存库出现——You cannot start a load for a destroyed activity
请记住一句话:不要再非主线程里面使用Glide加载图片,如果真的使用了,请把context参数换成getApplicationContext.
- arcgis android 中shapefile的加载
前言 本文为大家分享arcgis android 中shapefile的加载,默认你有java环境,懂一定的android基础知识,默认你已经安装android studio.如缺乏以上环境和知识,请 ...
- Glide加载图片的事例
//获取图片的url String url = resultsEntity.getUrl(); //判断获取的图片是否存在 if (resultsEntity.getItemHeight() > ...
随机推荐
- 常用网页标签重置CSS
做前端开发时常需要将网页中所有标签的格式清空,从而使各个浏览器标签样式一样.以下是网络上常见的CSS代码 /* =s Reset (by YUI 3) */ html{color:#000;} bod ...
- 建立tcl文件
- 压测软件-Tsung.安装篇
author :James,jimingsong@vip.qq.com author :James,jimingsong@vip.qq.com since :2015-03-02 tsung介绍 ts ...
- centos php nginx 添加到service
1. nginx A. # vi /etc/init.d/nginx B. #!/bin/sh # Comments to support chkconfig on RedHat Linux # ch ...
- js便利关联数组 及数组定义方式 分类
"http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv=& ...
- ueditor1.4.3 在IE8下的 BUG
ueditor1.4.3 .net 版 在IE8 下,多图片上传完成后,点击确认时报错,无法插入图片到编辑器中 原因是 ueditor.all.js 中的 24835 行 if (whitList[ ...
- 浏览器exp使用经验
0x00背景 windows平台下,浏览器安全是绕不过的话题,其涉及的安全问题涵盖二进制和web,攻击场景也非常多样化: 用户点击攻击者的恶意URL链接被感染恶意代码 访问恶意站点被绕过同源策略窃取c ...
- Eclipse Bug: Unhandled event loop exception No more handles
我的解决方法如下: I had the same problem, turned out that TeamViewer was causing this. In your TeamViewer go ...
- thinkphp pdo 重写问题
ThinkPHP3.2.3版本数据库驱动采用PDO完全重写,配置和使用上面也比之前版本更加灵活和强大,我们来了解下如何使用. 首先,3.2.3的数据库配置信息有所调整,完整的数据库设置包括: 复制代码 ...
- 蓝桥杯—盾神与条状项链(C++实现)
思路: 直接使用STL的list容器. 分两种情况: 1.DEL Q.直接调用void remove( const TYPE &val )函数即可. 2.ADD P Q.首先找出P所在的位置, ...