Android图片加载框架Picasso最全使用教程
来源:互联网 发布:台达触摸屏编程手册 编辑:程序博客网 时间:2024/05/17 23:59
Android图片加载框架Picasso最全使用教程 一
版权声明:本文为博主原创文章,转载请注明出处,未经博主允许不得转载。
目录(?)[+]
- Picasso介绍
- 为什么要用Picasso
- 将Picasso加入到你的项目中
- 小试牛刀从网络加载一张图片
- 图片的其他加载方式
- 从Android Resources 中加载
- 从本地File文件中加载
- 从URI地址中加载
Picasso介绍
Picasso是Square公司开源的一个Android图形缓存库
A powerful image downloading and caching library for Android
一个Android下强大的图片下载缓存库
Picasso实现了图片的异步加载,并解决了Android中加载图片时常见的一些问题,它有以下特点:
- 在
Adapter
中取消了不在视图范围内的ImageView
的资源加载,因为可能会产生图片错位; - 使用复杂的图片转换技术降低内存的使用
- 自带内存和硬盘的二级缓存机制
为什么要用Picasso
Android系统作为图片资源加载的主角,它是通过图像的像素点来把图像加载到内存中的;现在一张500W的摄像头拍出的照片(2592x1936),加载到内存中需要大约19M的内存;如果你加入了信号强度不一的网络中进行了复杂的网络请求,并进行图片的缓存与其他处理,你会耗费大量的时间与精力来处理这些问题,但如果用了Picasso, 这些问题都一消而散;
将Picasso加入到你的项目中
目前Picasso的最新版本是2.5.2,你可以下载对应的Jar包,将Jar包添加到你的项目中,或者在build.gradle
配置文件中加入
compile 'com.squareup.picasso:picasso:2.5.2'
- 1
- 1
注意如果你开启了混淆,你需要将以下代码添加到混淆规则文件中:
-dontwarn com.squareup.okhttp.**
- 1
- 1
小试牛刀:从网络加载一张图片
Picasso使用简单易用的接口,并有一个实现类Picasso
,一个完整的功能请求至少需要三个参数;
with(Context context)
-Context
上下文在很多Android Api中都是必须的load(String imageUrl)
- 图片网络加载地址into(ImageView targetImageView)
- 想进行图片展示的ImageView
简单用例:
ImageView targetImageView = (ImageView) findViewById(R.id.imageView);String internetUrl = "http://www.jycoder.com/json/Image/1.jpg";Picasso .with(context) .load(internetUrl) .into(targetImageView);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
就是这么简单,如果你的 URL
地址正确并且图片存在,在几秒中之内就能看到这张图片了;如果图片资源不存在,Picasso也会有错误的回调,现在你已经看到了只需3行代码就能加载图片了,当然这只是冰山一角,让我们继续揭开Picasso的神秘面纱;
图片的其他加载方式
Picasso的图片不仅仅能加载网络资源,也能从本地文件,Android项目资源,以及URI
地址进行图片加载,下面我们就对这三种方式进行实例说明;
从Android Resources 中加载
代码也是三行,只需要将网络资源地址更改为一个int
值地址即可,上代码:
ImageView targetImageView = (ImageView) findViewById(R.id.imageView);int resourceId = R.mipmap.ic_launcher;Picasso .with(context) .load(resourceId) .into(targetImageView);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
注意: R.mipmap
是Android Studio
中新的资源引用路径,这个老司机都知道.
从本地File文件中加载
如果你让用户选择本地的一张图片进行展示的话,就需要用到这个加载方式了,当然,也是So Easy,只需要将地址更换为一个File
即可,上代码:
ImageView targetImageView = (ImageView) findViewById(R.id.imageView);File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Running.jpg");Picasso .with(context) .load(file) .into(targetImageView);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
注意:这个file
并不一定非得是在你的设备中,可以是任意的路径,只要是File路径即可;
从URI
地址中加载
这个请求方式相比其他也并没有什么不同,上代码:
public static final String ANDROID_RESOURCE = "android.resource://";public static final String FOREWARD_SLASH = "/";private static Uri resourceIdToUri(Context context, int resourceId) { return Uri.parse(ANDROID_RESOURCE + context.getPackageName() + FOREWARD_SLASH + resourceId);}Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);ImageView targetImageView = (ImageView) findViewById(R.id.imageView);Picasso .with(context) .load(uri) .into(targetImageView);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
注意:为了示范,只能用资源文件转换为URI
,并不仅仅是这种方式, 它可以支持任意的URI
地址;
OK,到此我们已经对Picasso有一个基本的认识和了解了,跟着我的脚步,继续发现Picasso更多好玩的功能,下面会介绍Picasso在ListView
及GridView
的用法,愿大家都有美好的一天~~
- 顶
- 6
- 踩
- 3
- 下一篇Android-Studio-插件小结
我的同类文章
- •Picasso源码解析 一2016-04-05
- •Android图片加载框架Picasso最全使用教程 四2016-04-04
- •Android图片加载框架Picasso最全使用教程 二2016-04-03
- •Android图片加载框架Picasso最全使用教程 五2016-04-04
- •Android图片加载框架Picasso最全使用教程 三2016-04-04
- Android图片加载框架Picasso最全使用教程 一
- Android图片加载框架Picasso最全使用教程 二
- Android图片加载框架Picasso最全使用教程 三
- Android图片加载框架Picasso最全使用教程 四
- Android图片加载框架Picasso最全使用教程 五
- Android图片加载框架Picasso最全使用教程 三
- Android图片加载框架Picasso最全使用教程 一
- Android图片加载框架Picasso最全使用教程 二
- Android图片加载框架Picasso最全使用教程
- Android图片加载框架Picasso使用教程 (三)
- Android图片加载框架Picasso使用教程 (四)
- Android图片加载框架Picasso使用教程 (五)
- Android图片加载框架Picasso
- Android加载图片框架Picasso
- 安卓网络图片加载框架-Picasso使用教程
- Android图片加载框架Picasso使用教程 (二)listviewh和gridview
- Android框架 加载图片 库 Picasso 的使用简介
- Android——图片加载框架Picasso的使用
- 常用激活函数比较
- Oracle修改表,提示“资源正忙,要求指定NOWAIT”
- 详解Node.js API系列 Crypto加密模块
- WCF入门教程(二)从零做起-创建WCF服务
- 55个必备技巧带你玩转JavaScript
- Android图片加载框架Picasso最全使用教程
- 图片批量处理
- JQuery 中each的使用方法
- go中引用socket库 ws2_32
- 判断点击点是不是在UICollectionView中的cell上
- 百度地图api 实时显示 每个人的位置和头像
- Linux 2.6内核中新的锁机制--RCU
- 谈谈企业的数据工作!——企业的数据分析能力金字塔
- 转屏及数据保存