Picasso图片加载器简单封装

简介: Picasso是一款当下好用并且流行的图片加载器,在这里分享一下我的简单封装,小巧玲珑、方便使用。废话不多讲直接上代码。首先Picasso在github上面的显示地址:https://github.com/square/picasso然后根据当下的依赖版本,添加依赖在你的Module的build.gradle内部。

Picasso是一款当下好用并且流行的图片加载器,在这里分享一下我的简单封装,小巧玲珑、方便使用。废话不多讲直接上代码。

首先Picasso在github上面的显示地址:https://github.com/square/picasso

然后根据当下的依赖版本,添加依赖在你的Module的build.gradle内部。

   compile 'com.squareup.picasso:picasso:2.5.2'

接下来就是简单的封装使用。

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

/**
 * 图片加载工具
 * Created by 韩小呆 on 2017/12/10.
 */

public class PicassoUtils {
    /**
     * 默认加载图片
     */
    public static void loadImageView(Context context, String url, ImageView imageView) {

        Picasso.with(context).load(url).into(imageView);
    }

    /**
     * 从res文件内部加载图片
     */
    public static void loadImageViewFromRes(Context context, int resourceId, ImageView imageView) {
        Picasso.with(context).load(resourceId).into(imageView);
    }

    /**
     * 网上加载指定图片大小的图片
     */
    public static void loadImageViewSize(Context context, String url, int width, int height, ImageView imageView) {
        Picasso.with(context).load(url).resize(width, height).centerCrop().into(imageView);
    }
  /**
     *res文件内部加载指定大小图片
     */
    public static void loadImageViewSizeForRes(Context context, int id, int width, int height, ImageView imageView) {
        Picasso.with(context).load(id).resize(width, height).centerCrop().into(imageView);
    }

    /**
     * 1、加载中图片,加载错误图片,加载正确图片并且设置
     * 2、当没有图片时,因此imageView
     */
    public static void loadImageViewHolder(Context context, String url, int loadingImage, int loadingErrorImage, ImageView imageView) {
        if (url != null && !TextUtils.isEmpty(url)) {
            Picasso.with(context).load(url).placeholder(loadingImage).error(loadingErrorImage).into(imageView);
        } else {
            imageView.setVisibility(View.GONE);
        }

    }

    public static void loadImageViewHolderSize(Context context, String url, int width, int height, int loadingImage, int loadingErrorImage, ImageView imageView) {
        if (url != null && !TextUtils.isEmpty(url)) {
            Picasso.with(context).load(url).resize(width, height).centerCrop().placeholder(loadingImage).error(loadingErrorImage).into(imageView);
        } else {
            imageView.setVisibility(View.GONE);
        }

    }


    /**
     * 裁剪图片
     */
    public static void loadImageViewCrop(Context context, String url, ImageView imageView) {
        Picasso.with(context).load(url).transform(new CropSquareTransformation()).into(imageView);
    }

    /**
     * 按比例裁剪图片
     */
    public static 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 "crop";
        }
    }
}

最后就是,你可以在你的类内部进行调用了,简单方便,一步到位。

目录
相关文章
|
C语言
C语言基础语法
这段文字主要介绍了C语言中的基础语法,包括函数调用的不同方式(如使用位置参数或命名参数传递,处理变参数的情况)及如何正确地进行组合调用,并保持数据类型的统一。此外,还介绍了操作符的使用,如比较运算符和逻辑运算符(`and`、`or`、`not`)。相关详细内容和示例可以通过阿里云的帮助文档进一步了解,包括函数调用方式、评估表达式的设置方法、告警条件表达式的语法,以及查询语法结构等。这为初学者提供了理解和实践C语言编程的良好起点。
300 13
|
存储 关系型数据库 MySQL
|
SQL 分布式计算 Java
Hadoop大数据平台实战(02):HBase vs. Hive vs. Impala 对比
Hadoop大数据平台实战(02):HBase vs. Hive vs. Impala 对比。
|
SQL 监控 Oracle
Oracle 常见故障及日常规划
--============================= -- Oracle 常见故障及日常规划 --=============================       对任何数据库系统而言,对显而易见的故障,应当避免发生本文列出了Oracle常见的故障并给出了解决方案,同时列出了一些日常规划。
1372 0
|
3天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
14天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1303 5
|
13天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1330 87
|
2天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
184 82
2025年阿里云域名备案流程(新手图文详细流程)