Picasso是Square公司一个开源的Android图片处理的缓存库,可以实现图片的下载与缓存的功能。
Picasso库下载地址:http://square.github.io/picasso/
主要有以下特点:
1、可以使用最少的内存完成复杂的图形转换
2、自带内存和硬盘二级缓存功能
3、可以加载网络资源
4、提供了图形操作接口,如大小和旋转等
5、在adapter中回收和取消当前下载
本文主要将通过两种方式获取同一网络资源图片来比较,接下来将是主要实现方法。
Picasso方法
加载网络图片操作简单,只需一步操作即可完成
Picasso.with(MainActivity.this).load(imageUrl).config(Bitmap.Config.ARGB_8888).into(imageView);
将图片裁剪为1/2大小代码
Picasso.with(MainActivity.this).load(imageUrl)
.transform(new CropSquareTransformation()).into(imageView2);
MainActivity具体代码如下:
package example.com.picassotest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;
/**
* Title: PicassoTest
* Package example.com.picassotest
* Description: ${TODO}
* Copyright: Copyright (c) 2015
* author Jimmy.li
* Date: 2016-04-22
* Time: 15:03
* version V1.0
*/
public class MainActivity extends AppCompatActivity {
private ImageView imageView, imageView2;
private TextView textView;
//网络图片链接地址
String imageUrl = "http://img.ptcms.csdn.net/article/201503/30/5519091be9a85_middle.jpg?_=30474";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initData();
}
private void initData() {
/**
* resize大小
* ARGB_8888:每个像素占用4byte内存
* 显示ImageView
*/
Picasso.with(MainActivity.this).load(imageUrl).config(Bitmap.Config.ARGB_8888).into(imageView);
textView.setText("Picasso");
/**
* 将图片裁剪为原来1/2,并显示在第二个ImageView中
*/
Picasso.with(MainActivity.this).load(imageUrl)
.transform(new CropSquareTransformation()).into(imageView2);
}
private void initUI() {
imageView = (ImageView) findViewById(R.id.iv1);
imageView2 = (ImageView) findViewById(R.id.iv2);
textView = (TextView) findViewById(R.id.tv);
}
}
裁剪类 CropSquareTransformation.java,将原图片大小裁剪为原来1/2。具体实现如下
package example.com.picassotest;
import android.graphics.Bitmap;
import com.squareup.picasso.Transformation;
/**
* Title: PicassoTest
* Package example.com.picassotest
* Description: ${TODO}
* Copyright: Copyright (c) 2015
* author Jimmy.li
* Date: 2016-04-22
* Time: 15:03
* version V1.0
*/
/**
* CropSquareTransformation 图片裁剪类
* 截取图片1/2
*/
public class CropSquareTransformation implements Transformation {
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()";
}
}
Piacsso 方法加载网络图片采用LRU算法,减少耗时与内存的占用,加载速度快,效率高。
布局文件比较简单,再此就不贴出来了,如需参考请下载源码。
HttpUrlConnection方式加载网络图片
package example.com.picassotest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Title: PicassoTest
* Package example.com.picassotest
* Description: ${TODO}
* Copyright: Copyright (c) 2015
* author Jimmy.li
* Date: 2016-04-22
* Time: 11:00
* version V1.0
*/
public class HttpUrlConnectionActivity extends Activity {
private ImageView imageView, imageView2;
private TextView textView;
String url = "http://img.ptcms.csdn.net/article/201503/30/5519091be9a85_middle.jpg?_=30474";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initData();
}
private void initData() {
textView.setText("HttpURLConnection");
// 执行Task
new ImageDownloadTask(imageView).execute(url);
}
private void initUI() {
imageView = (ImageView) findViewById(R.id.iv1);
imageView2 = (ImageView) findViewById(R.id.iv2);
textView = (TextView) findViewById(R.id.tv);
}
//自定义获取图片Task
private class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
public ImageDownloadTask(ImageView imageView) {
this.imageView = imageView;
}
protected Bitmap doInBackground(String... addresses) {
Bitmap bitmap = null;
InputStream in;
try {
// 建立URL连接
URL url = new URL(addresses[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 打开输入流
conn.connect();
in = conn.getInputStream();
// 编码输入流
bitmap = BitmapFactory.decodeStream(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
// if(in != null)
// in.close();
}
return bitmap;
}
// Task执行完毕,返回bitmap
@Override
protected void onPostExecute(Bitmap result) {
// Set bitmap image for the result
imageView.setImageBitmap(result);
}
}
}
使用Picasso与HttpUrlConnection的区别
1:、Picasso是第三方开源框架,使用成熟、方便
2、操作简单,Picasso提供大量图像操作接口
3、Picasso获取网络图片简单,只需调用接口即可,HttpUrlConnection获取需要写大量代码,还需处理缓存与下载管理等操作,该方式复杂且不方便。
4、HttpUrlConnection获取需要线程异步加载,所以还需要AsyncTask,耗时大。
源码下载地址:http://download.csdn.net/detail/u012721519/9546092
Good luck!
Write by Jimmy.li