文档章节

Picasso使用教程

z
 zdh1000
发布于 2016/07/25 21:36
字数 305
阅读 30
收藏 0

Introduction

Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code!

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

Many common pitfalls of image loading on Android are handled automatically by Picasso:

  • Handling ImageView recycling and download cancelation in an adapter.
  • Complex image transformations with minimal memory use.
  • Automatic memory and disk caching.

Sample application screenshot.

Features

ADAPTER DOWNLOADS

Adapter re-use is automatically detected and the previous download canceled.

@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);
}

IMAGE TRANSFORMATIONS

Transform images to better fit into layouts and to reduce memory size.

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

You can also specify custom transformations for more advanced effects.

public class CropSquareTransformation implements Transformation {
  @Override public Bitmap transform(Bitmap source) {
    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()"; }
}

Pass an instance of this class to the transform method.

PLACE HOLDERS

Picasso supports both download and error placeholders as optional features.

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

A request will be retried three times before the error placeholder is shown.

RESOURCE LOADING

Resources, assets, files, content providers are all supported as image sources.

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(new File(...)).into(imageView3);

DEBUG INDICATORS

For development you can enable the display of a colored ribbon which indicates the image source. Call setIndicatorsEnabled(true)on the Picasso instance.

Debug ribbon indicators

 

本文转载自:http://square.github.io/picasso/

共有 人打赏支持
上一篇: Nucleus
下一篇: retrofit使用教程
z
粉丝 4
博文 20
码字总数 5604
作品 0
武汉
程序员
私信 提问
Android 开发第三方开源库

picasso图形缓存库,实现图片下载和缓存功能 官网:http://square.github.io/picasso/教程博客:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0731/1639.html...

CrazyManDF
2016/03/28
51
0
Android网络框架

Android OKHttp3.0 以上使用方法 Android OKHttp3.0 以上使用方法详解 Retrofit 之日志拦截 Retrofit 日志拦截相关介绍 Retrofit源码解析 Retrofit的源码分析将从基本的使用方法入手,分析r...

掘金官方
2017/12/12
0
0
Picasso detected an unsupported OkHttp on the ...

http://stackoverflow.com/questions/24125856/picasso-detected-an-unsupported-okhttp-on-the-classpath https://github.com/square/picasso/issues/541 如果使用Picasso同时也使用了okhtt......

tyronejw
2015/12/31
69
0
Android Picasso

官网: http://square.github.io/picasso/ How to retrieve Bitmap with Picasso ? http://stackoverflow.com/questions/20181491/use-picasso-to-get-a-callback-with-a-bitmap Picasso框架......

当空皓月
2016/03/17
93
0
使用自定义的Picasso导致程序崩溃问题

程序打印的错误日志基本都是与Surface unlock相关,但程序并没有操作Surface。看到日志几次打印:Looper﹕ Could not create wake pipe. errno=24,引起注意,猜想是读写文件报的错误。但没有...

哦_呢称
2016/06/19
199
0

没有更多内容

加载失败,请刷新页面

加载更多

linux脚本中父shell与子shell 执行的几种方式

本文主要介绍以下几个命令的区别: shell subshell source $ (commond) `commond` Linux执行Scripts有两种方式,主要区别在于是否建立subshell 1. source filename or . filename 不创建sub...

问题终结者
15分钟前
1
0
安装jdk和Tomcat

12月12日任务 16.1 Tomcat介绍 16.2 安装jdk 16.3 安装Tomcat Tomcat介绍 Tomcat是apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由apache、Sun和其他一些...

robertt15
16分钟前
3
0
Beetl 免费视频

来自 https://my.oschina.net/gking?q=Beetl ,Beetl终于有人录制视频了 项目git地址:https://gitee.com/gavink/beetl-blog 视频地址:下载下来会更清晰,视频比较长,可使用倍速看 百度网盘...

闲大赋
28分钟前
0
0
isEmpty和null的区别

isEmpty和null的区别: 1.一个是对象为空(IsNull),一个是值为空(IsEmpty) 2.IsNull指任务类型变量是否为空包括对象类型的变量。 IsNull函数: 功能:返回Boolean的值,指明表达是否不包...

DemonsI
54分钟前
3
0
Centos7 安装mysql与php

https://blog.csdn.net/qq_36431213/article/details/79576025 官网下载安装mysql-server 依次使用下面三个命令安装 wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.r......

Yao--靠自己
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部