android 下载图片的框架,Android图片下载、缓存框架——Picasso

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

83582518a73366f8f46c0eba55fbd483.png

代码使用片断:

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

}

变换图像,以更好地适应布局和减少内存大小。

Picasso.with(context)

.load(url)

.resize(50, 50)

.centerCrop()

.into(imageView)

您还可以为更高级的效果指定自定义转换。

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()"; }

}

Picasso支持下载错误占位符作为可选功能。

Picasso.with(context)

.load(url)

.placeholder(R.drawable.user_placeholder)

.error(R.drawable.user_placeholder_error)

.into(imageView);

支持各种加载方式

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

picasso-2.5.2.jar 库文件下载

package com.loopj.android.image; import android.content.Context; import android.graphics.Bitmap; import android.util.AttributeSet; import android.widget.ImageView; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class SmartImageView extends ImageView { private static final int LOADING_THREADS = 4; private static ExecutorService threadPool = Executors.newFixedThreadPool(LOADING_THREADS); private SmartImageTask currentTask; public SmartImageView(Context context) { super(context); } public SmartImageView(Context context, AttributeSet attrs) { super(context, attrs); } public SmartImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } // Helpers to set image by URL public void setImageUrl(String url) { setImage(new WebImage(url)); } public void setImageUrl(String url, SmartImageTask.OnCompleteListener completeListener) { setImage(new WebImage(url), completeListener); } public void setImageUrl(String url, final Integer fallbackResource) { setImage(new WebImage(url), fallbackResource); } public void setImageUrl(String url, final Integer fallbackResource, SmartImageTask.OnCompleteListener completeListener) { setImage(new WebImage(url), fallbackResource, completeListener); } public void setImageUrl(String url, final Integer fallbackResource, final Integer loadingResource) { setImage(new WebImage(url), fallbackResource, loadingResource); } public void setImageUrl(String url, final Integer fallbackResource, final Integer loadingResource, SmartImageTask.OnCompleteListener completeListener) { setImage(new WebImage(url), fallbackResource, loadingResource, completeListener); } // Helpers to set image by contact address book id public void setImageContact(long contactId) { setImage(new ContactImage(contactId)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值