开篇——介绍Picasso

(Picasso是什么?)Picasso:A Powerfull Image Downloading and Caching Library for Android,即Android平台的网络图片下载和缓存框架。

(Picasso如何使用?)框架嘛!既然牛人能够写出这个框架,自然使用流畅。不用担心,很简单,但深入源代码就需要花点功夫。

(为什么会出现Picasso框架?)Android开发中,常需要从远程服务器端获取图片;一般情况下,我们会使用HttpURLConnection(java.net.HttpURLConnection)和AsyncTask(android.os.AsyncTask)结合实现该功能;但还需要考虑其他情况:图片缓存和下载管理(??),因此聪明的做法是:封装成自己的工具类,或者是使用第三方框架(比如这里的Picasso)。

热身运动——HttpUrlConnection和AsyncTask实现网络图片下载

在使用Picasso框架之前,先来一个热身运动:使用HttpUrlConnection(java.net.HttpUrlConnection)和AsyncTask(android.os.AsyncTask)实现网络图片加载。

activity_main.xml文件方面,LinearLayout的垂直布局中布置两个控件:ImageView和Button;Button的点击事件触发网络图片请求和加载内存(即:显示图片)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <ImageView
android:id="@+id/iv_image"
android:layout_width="200dp"
android:layout_height="200dp" /> <Button
android:onClick="loadImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loading..." /> </LinearLayout>

AndroidManifest.xml文件方面,添加网络请求的用户权限。

<uses-permission android:name="android.permission.INTERNET"/>

MainActivity.java方面:

package com.fenix.picassodemo;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView; public class MainActivity extends Activity { private ImageView mIView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
mIView = (ImageView) findViewById(R.id.iv_image);
} private class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> { ImageView iv; public ImageDownloadTask(ImageView iv) {
this.iv = iv;
} @Override
protected void onPreExecute() {
super.onPreExecute();
} @Override
protected Bitmap doInBackground(String... imgAddress) {
URL imgUrl = null;
HttpURLConnection connection = null;
InputStream inputStream = null;
Bitmap bitmap = null; try {
imgUrl = new URL(imgAddress[0]); // 将网络图片路径转化为URL实例 connection = (HttpURLConnection) imgUrl.openConnection();
connection.setDoInput(true);
connection.connect(); inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} return bitmap;
} @Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result); iv.setImageBitmap(result);
}
} public void loadImage(View v) {
new ImageDownloadTask(mIView).execute("http://pic32.nipic.com/20130829/12906030_124355855000_2.png");
} }

点击布局文件中的Button,执行loadImage(),即网络图片请求;网络请求不应该被写在主线程中,因此需要借助AsyncTask工具(封装Handler和Thread);一旦点击Button,即执行ImageDownloadTask。

图片资源来自:www.baidu.com的图片库(右键-》复制图片地址)。

AsyncTask结合HttpURLConnection,确实可以实现网络图片的请求,但问题是:Android应用程序(从网络图片角度),还比如:管理图片下载请求,合理使用内存等方面都需要考虑(Google工程师——极致体验的代言人)。这就是为什么出现了这么多类似于Picasso图片请求框架的缘故!当然还有很多开源框架需要熟悉(后续再说呗)。

Picasso框架——初体验

Android开源框架——Picasso的更多相关文章

  1. Android 开源框架Universal-Image-Loader学习

    Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用 Android 开源框架Universal-Image-Loader完全解析(二)--- 图片 ...

  2. Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/39057201),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...

  3. Android 开源框架Universal-Image-Loader完全解析(二)--- 图片缓存策略详解

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...

  4. Android进阶笔记13:RoboBinding(实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架)

    1.RoboBinding RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架.从简单的角度看,他移除了如addXXListen ...

  5. android 开源框架推荐

    同事整理的 android 开源框架,个个都堪称经典.32 个赞! 1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)  JS ...

  6. Android 开源框架Universal-Image-Loader全然解析(二)--- 图片缓存策略具体解释

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...

  7. Android开源框架Afinal第一篇——揭开圣女的面纱

    Android开源框架Afinal第一篇——揭开圣女的面纱 分类: Android开源框架哪点事2013-09-02 14:25 260人阅读 评论(0) 收藏 举报 Afinal 这是Afinal在 ...

  8. 六款值得推荐的Android开源框架简介

    技术不再多,知道一些常用的.不错的就够了.下面就是最近整理的“性价比”比较高的Android开源框架,应该是相对实用的. 1.volley 项目地址 https://github.com/smanik ...

  9. Android——开源框架Universal-Image-Loader + Fragment使用+轮播广告

    原文地址: Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用 Android 开源框架Universal-Image-Loader完全解析(二) ...

随机推荐

  1. NGUI之UIRoot

    原文:http://www.tasharen.com/forum/index.php?topic=6710.0 概述 UIRoot总是放在NGUI UI层级的最上层. 它用来使UI的缩放变得更容易.w ...

  2. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

    今天碰到了一个查询异常问题,上网查了一下,感谢原创和译者 如果你使用的数据库连接类是 the Data Access Application Blocks "SqlHelper" ...

  3. 使用Freemarker宏进行可扩展式模块化编程

    作者:Chu Lung 原文链接:http://blog.chulung.com/article/13 本文由MetaCLBlog于2016-07-08 14:42:10自动同步至cnblogs 一. ...

  4. VBS操作剪切板

    '设置剪切板的内容 Dim Form, TextBox Set Form = CreateObject("Forms.Form.1") Set TextBox = Form.Con ...

  5. php关闭错误提示

    今天调试phalcon的一个接口时候碰到如下提示: Deprecated: mongogo::mongogo(): The Mongo class is deprecated, please use ...

  6. Jmeter上传文件

    Jmeter上传文件 一.Fiddler抓包获取表单信息 操作被测系统,上传文件,Fiddler抓包获取提交表单信息如下:

  7. 《C和指针(Pointer on c)》 学习笔记

    转载:http://dsqiu.iteye.com/blog/1687944 首先本文是对参考中三个连接的博客进行的整理,非常感谢三位博主的努力,每次都感叹网友的力量实在太强大了…… 第一章 快速上手 ...

  8. C# 队列集合的使用

    using System; using System.Collections.Generic; using System.Text; using System.Collections; namespa ...

  9. VC++ 用setsockopt()来控制recv()与send()的超时

    在send(),recv()过程中有时由于网络状况等原因,收发不能预期进行,而设置收发超时控制: 以下是来自于网上一篇文章中的摘录,它是这样写的: ;//1秒, //设置发送超时 setsockopt ...

  10. Linux内核分析第一周学习总结:计算机是如何工作的?

    韩玉琪 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.冯诺依曼体系 ...