您的位置:首页 > 移动开发 > Android开发

Android 图片加载问题

2014-05-13 14:25 465 查看
1、Picasso (http://square.github.io/picasso/)

Picasso是一个用于Android平台上的下载和缓存图片的项目。它有许多定制选项,如何处理下载图片(包括调整和裁剪,以及提供一个接口让你随自己心意将图片转换成圆角等)。Picasso将要下载的图片(如果没有缓存)并将它负载到指定的目标,转换图片以适合所显示的ImageView,来减少内存消耗。

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

 

 

<dependency>  <groupId>com.squareup.picasso</groupId> 
<artifactId>picasso</artifactId> 
<
9a2e
;version>2.2.0</version></dependency>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 图片 缓存