Picasso:一个专为Android打造的强大的图片下载和缓存库

2024-03-28 21:32

本文主要是介绍Picasso:一个专为Android打造的强大的图片下载和缓存库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

在Android应用中,图片消费了大量的资源,却为应用提供了很好的视觉体验。幸运的是,Picasso为你的应用提供了非常容易的图片加载方式——通常一行代码就可以搞定!

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Picasso处理了Android上图片加载的许多坑:

1)在Adapter中,处理了ImageView的循环利用和取消下载。

2)耗费最小的内存处理了复杂的图形变换。

3)在内存和磁盘中自动缓存图片。


特点

Adapter中的下载

自动检测adapter中的重用功能,且一旦发现重用,将自动取消之前的下载。

@Override public void getView(int position, View convertView, ViewGroup parent) {SquaredImageView view = (SquaredImageView) convertView;if (view == null) {view = new SquaredImageView(context);}String url = getItem(position);Picasso.with(context).load(url).into(view);
}

图片转换

变换图片以便更好的适应布局,同时也减少了内存的使用。

Picasso.with(context).load(url).resize(50, 50).centerCrop().into(imageView)

你也可以自定义图片的转换方式以达到更复杂的变换要求。

public class CropSquareTransformation implements Transformation {@Override public Bitmap transform(Bitmapsource) {int size = Math.min(source.getWidth(),source.getHeight());int x = (source.getWidth() - size) / 2;int y = (source.getHeight() - size) / 2;Bitmap result = Bitmap.createBitmap(source,x, y, size, size);if (result != source) {source.recycle();}return result;}@Override public String key() { return"square()"; }
}

CropSquareTransformation这个类的一个实例传递给transform()函数即可。

占位图片

Picasso同时支持“正在下载”时和“图片下载出错”后这两种状态展示的默认图片。

Picasso.with(context).load(url).placeholder(R.drawable.user_placeholder).error(R.drawable.user_placeholder_error)
.into(imageView);

Picasso至多会尝试三次下载,如果三次下载都失败了,就会在原图位置上展示“图片下载出错”时的图片。

资源加载

Picasso支持将resources、assets、files和contentprovider作为图片的加载源。

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(newFile(...)).into(imageView3);

调试指示器

开发者可以在图片的左上角展示一个彩色的小三角,不同的颜色指明了图片资源的不同来源。调用Picasso对象的setIndicatorsEnabled(true)方法就可以了。


下载

http://repo1.maven.org/maven2/com/squareup/picasso/picasso/2.5.2/picasso-2.5.2.jar

Picasso的源代码,例子和这个网址(http://square.github.io/picasso/)都放在了 GitHub

MAVEN<dependency><groupId>com.squareup.picasso</groupId><artifactId>picasso</artifactId><version>2.5.2</version>
</dependency>GRADLEcompile 'com.squareup.picasso:picasso:2.5.2'

为Picasso做贡献

如果你想为Picasso开发贡献你的代码,你可以上github,然后fork该代码库,再把你的修改发给我们(pull request)。

在提交代码的时候,请保持代码的习惯和风格与原来的一致以便于尽可能保持代码的可阅读性。同时,为了保证你的代码正确编译,请运行mvn clean verify。

你需要同意Individual ContributorLicense Agreement (CLA)才能将你的代码合并到Picasso工程中。

重要资料

1]Javadoc : http://square.github.io/picasso/javadoc/index.html

[2]StackOverflow:http://stackoverflow.com/questions/tagged/picasso?sort=active

许可证

Copyright 2013 Square, Inc.

 

Licensed under the Apache License, Version2.0 (the "License");

you may not use this file except incompliance with the License.

You may obtain a copy of the License at

 

  http://www.apache.org/licenses/LICENSE-2.0

 

Unless required by applicable law or agreedto in writing, software

distributed under the License isdistributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANYKIND, either express or implied.

See the License for the specific languagegoverning permissions and

limitations under the License.

 

原文链接:http://square.github.io/picasso/







这篇关于Picasso:一个专为Android打造的强大的图片下载和缓存库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/856739

相关文章

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Python结合PyWebView库打造跨平台桌面应用

《Python结合PyWebView库打造跨平台桌面应用》随着Web技术的发展,将HTML/CSS/JavaScript与Python结合构建桌面应用成为可能,本文将系统讲解如何使用PyWebView... 目录一、技术原理与优势分析1.1 架构原理1.2 核心优势二、开发环境搭建2.1 安装依赖2.2 验

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

基于Python打造一个全能文本处理工具

《基于Python打造一个全能文本处理工具》:本文主要介绍一个基于Python+Tkinter开发的全功能本地化文本处理工具,它不仅具备基础的格式转换功能,更集成了中文特色处理等实用功能,有需要的... 目录1. 概述:当文本处理遇上python图形界面2. 功能全景图:六大核心模块解析3.运行效果4. 相

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me