Picassoネット画像のロードフレームの使用
9969 ワード
公式サイト:http://square.github.io/picasso/ 紹介 は、ListViewまたはRecycleViewのアダプターによく使われています。 ピクチャの変換機能には、切り取り方法があります。 ロード失敗とローディング中のピクチャ を設定します。は、異なる経路の画像リソース を設定する。カスタムダウンロード機 を設定します。
ソースのダウンロードクラスは、OkHttpをサポートする前提でこのクラスを使用しますが、カスタムディスクのキャッシュパスが必要な場合は、「public OkHttpDownloader」の構造方法を実行する必要があります。
メモリバッファの動作 一時停止、継続的なロード操作は、ListViewとRecycleViewのスライド時に、カードトンを回避するために、要求を一時停止することができます。コントロールがスライドを停止した時に引き続きロードします。
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
@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()
// ImageView
.into(imageView)
カスタム画像変換:// , transform
public static 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!=null){
source.recycle();
}
return result;
}
@Override
public String key() {
return "square()";
}
}
======================================================================================
//
Picasso.with(context).load(url).transform(new CropSquareTransformation()).into(imageView);
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);
// File
Picasso.with(context).load(new File(...)).into(imageView3);
ソースのダウンロードクラスは、OkHttpをサポートする前提でこのクラスを使用しますが、カスタムディスクのキャッシュパスが必要な場合は、「public OkHttpDownloader」の構造方法を実行する必要があります。
final class Utils {
//
static Downloader createDefaultDownloader(Context context) {
try {
// OKhttp, UrlConnectionDownloader
Class.forName("com.squareup.okhttp.OkHttpClient");
return OkHttpLoaderCreator.create(context);
} catch (ClassNotFoundException ignored) {
}
return new UrlConnectionDownloader(context);
}
//
static File createDefaultCacheDir(Context context) {
File cache = new File(context.getApplicationContext().getCacheDir(), PICASSO_CACHE);
if (!cache.exists()) {
//noinspection ResultOfMethodCallIgnored
cache.mkdirs();
}
return cache;
}
}
==========================================================================
public class OkHttpDownloader implements Downloader {
...
public OkHttpDownloader(final File cacheDir) {
this(cacheDir, Utils.calculateDiskCacheSize(cacheDir));
}
public OkHttpDownloader(final Context context, final long maxSize) {
this(Utils.createDefaultCacheDir(context), maxSize);
}
public OkHttpDownloader(final File cacheDir, final long maxSize) {
this(defaultOkHttpClient());
try {
client.setCache(new com.squareup.okhttp.Cache(cacheDir, maxSize));
} catch (IOException ignored) {
}
}
使い方:Picasso.setSingletonInstance(new Picasso
.downloader(new OkHttpDownloader(" , File"))
.build());
Picasso取得キャッシュはデフォルトではOkhttpで取得していますが、Okhttpで使用されているキャッシュはDiskLru Cacheに基づいています。DiskLru Cacheの実現原理はコロナしています。jurnalファイルを読んでキャッシュファイルを操作するので、カスタムディレクトリを設定したまま、DiskLru Cacheで取得とキャッシュを消去することができます。Picasso.with(context)
.load(url)
// ,NO_CACHE ,NO_STORE Cache ( )
.memoryPolicy(NO_CACHE, NO_STORE)
.into(imageView);
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState){
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
Picasso.with(MainActivity.this).resumeTag(tag);
break;
default:
Picasso.with(MainActivity.this).pauseTag(tag);
break;
}
}
....
});
}