图片加载神装——Picasso
来源:互联网 发布:a1530支持什么网络 编辑:程序博客网 时间:2024/06/06 01:43
已经用过Square公司的很多重量级开源框架了(OkHttp, Retrofit),现在来了解一下加载图片的重量级框架!异步缓存加载图片只需一步~~~
**几大优点:
1.实现了图片异步加载的功能
2.解决了在AdapterView中显示图片时视图回收和再利用产生的问题。由于AdapterView在加载Item时有复用机制——预加载复用已经初始化的容器,离开视野清除容器中的数据。Picasso解决了复用过程中图片加载的问题。
3.自带压缩防止OOM,自带内存、二级缓存机制
4.支持加载res、asserts、local file、ContentProvider中的图片,以及网络图片。**
用过的朋友都说好!先看效果:
可以看出上面加载的是本地的图片。
一、Picasso使用前准备工作:
AndroidManifest.xml
<!--比如说腰读取本地的图片,的读取权限--> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!--比如要读取网络图片的网络权限--> <uses-permission android:name="android.permission.INTERNET"/>
build.gradle
dependencies { compile 'com.squareup.picasso:picasso:2.5.2'}
二、Picasso初体验:
1.加载localfile小例
在Android设备中放置一个图片,比如在根目录下创建一个/content/test.png
再使用Picasso加载本地文件的方法。
String path = "/content/test.png"; // 初始化一个ImageView控件 File localImage = new File(path); Picasso.with(Context).load(localImage).into(ImageView);
2.加载网络图片小例
在网上照一张美图url,比如:http://a3.att.hudong.com/62/74/19300263504520132460748360286.jpg
String imageUrl = "http://a3.att.hudong.com/62/74/19300263504520132460748360286.jpg"; Picasso.with(this).load(imageUrl).into(ImageView);
其他类似的例子非常多。就不累述了。
三、Picasso的深入理解
1.静态单例构造器:public static Picasso with(Context context) {}方法
使用此方法可以创建Picasso对象
public static Picasso with(Context context) { if (singleton == null) { synchronized (Picasso.class) { if (singleton == null) { singleton = new Builder(context).build(); } } } return singleton; }
2.build方法中创建了图片加载的下载器、缓存、服务、请求传递器等对象
public Picasso build() { Context context = this.context; // 创建下载器 if (downloader == null) { downloader = Utils.createDefaultDownloader(context); } // 创建内存缓存 if (cache == null) { cache = new LruCache(context); } // 创建服务 if (service == null) { service = new PicassoExecutorService(); } // 创建请求传递器 if (transformer == null) { transformer = RequestTransformer.IDENTITY; } Stats stats = new Stats(cache); Dispatcher dispatcher = new Dispatcher(context, service, HANDLER, downloader, cache, stats); return new Picasso(context, dispatcher, cache, listener, transformer, requestHandlers, stats, defaultBitmapConfig, indicatorsEnabled, loggingEnabled);}
3.into 方法
into方法首先进行一系列的校验,如是否是在主(UI)线程,目标ImageView是否为空,URI或ResourceId是否为空等。经过校验后,建立请求,并通过请求生成一个requestKey,该Key即为缓存中缓存该图片的Key值,请求网络前先通过该Key检查图片是否在缓存中。否则将请求加入Picasso的请求队列中。
核心类为Dispatcher,通过DispatcherThread和BitmapHunter进行整个工作队列和Bitmap缓存等的管理。
详细的分析参考:http://blog.csdn.net/u013278099/article/details/50613979
—————————华丽丽的分割线—————————————–
- 图片加载神装——Picasso
- 图片加载框架——Picasso
- 图片加载利器——Picasso
- Android图片加载框架——Picasso和Glide
- Android图片加载框架——Picasso和Glide
- 学习笔记之——Android图片加载框架Picasso
- Android——图片加载框架Picasso的使用
- Picasso 图片加载库
- Picasso加载图片
- Picasso加载图片
- Picasso加载图片
- Picasso加载图片
- 使用Picasso加载图片
- Picasso 加载图片
- 使用Picasso加载图片
- Picasso 图片加载
- 图片加载框架-Picasso
- 使用Picasso加载图片
- 19.常量-final
- make menuconfig错误
- 【Android基础笔记02】 API Guide学习和常用布局及基本UI控件
- native本地方法
- 解析数论
- 图片加载神装——Picasso
- 如何用位操作符将float转换为unsigned char数组
- Spring Boot中使用JavaMailSender发送邮件
- jsoncole 远程监控linux上tomcat配置
- Android studio安装常见错误之Gradle sync failed: Cause: org/gradle/api/...
- Android Studio编译运行找不到指定APK的问题
- window.top.location.href 和 window.location.href 的区别
- 机器视觉之 ICP算法和RANSAC算法
- 使用Google Earth画线路图