Skip to content

Instantly share code, notes, and snippets.

@brucetoo
Last active August 8, 2017 02:35
Show Gist options
  • Save brucetoo/d51cacc6602307613757 to your computer and use it in GitHub Desktop.
Save brucetoo/d51cacc6602307613757 to your computer and use it in GitHub Desktop.
Picasso使用
1、加载图片到 ImageView
ImageView view = null; Picasso picasso = Picasso.with(this); picasso.setIndicatorsEnabled(true); //开启调模式,它能够在图片左上角显示小三角形,这个小三角形的颜色标明了图片的来源:网络、内存缓存、磁盘缓存
picasso.setLoggingEnabled(true); //打开日志,即log中会打印出目前下载的进度、情况 picasso.load("http://xxx.jpg") //可以是本地图片或网络图片 .placeholder(R.drawable.placeholder) //当图片正在加载时显示的图片(optional) .error(R.drawable.error) //当图片加载失败时显示的图片(optional)
.into(view, new Callback() { //将图片下载完后放进view中,回调是可选的
@Override
public void onSuccess() { //加载图片成功时回调 }
@Override
public void onError() { //加载图片失败时回调 } });
2、加载图片并自定义动作
Target target = new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
//当图片加载成功时调用,bitmap是加载的图片,loadFrom 标明图片的来源是网络、内存还是磁盘
//可以在里面执行把图片保存到本地的操作
}
@Override
public void onBitmapFailed(Drawable errorDrawable) { //当图片加载失败时调用
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) { //当任务被提交时调用
}
};
picasso.load(new File("/1.jpg")).into(target); //指定target任务加载图片
3、对图片进行变换
Transformation transformation = new Transformation() {
@Override
public Bitmap transform(Bitmap bitmap) {
Bitmap newBitmap = null;
//自定义变换
if(bitmap != null && !bitmap.isRecycled()){ //将旧图片回收
bitmap.recycle();
}
return newBitmap;
}
@Override
public String key() { // 将用作cache的key
return "key";
}
};
picasso.load(R.drawable.download)
.skipMemoryCache() //不要把加载的图片放入缓存,也不要从缓存中取图片
.transform(transformation) //执行自定义变换
.into(view);
@ContainsK
Copy link

picasso.load(url).into(target); //指定target任务加载图片

有时候 onBitmapLoaded 不会被调用是什么原因?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment