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

Android图片加载框架Picasso最全使用教程 一

2017-02-14 10:35 666 查看
原文:http://blog.csdn.net/smallcheric/article/details/51049638


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
的用法,愿大家都有美好的一天~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: