10

ピカソを使用してポートレートを表示しています。プロトレイトが変更された場合、このユーザーのキャッシュ (またはすべてのユーザーのポートレート キャッシュ) をクリアしたいのですが、ここに私のコードがありますが、機能しません。

LruCache lruCache = new LruCache(context);
lruCache.clear();
Picasso picasso = new Picasso.Builder(context).memoryCache(lruCache).build();
picasso.load(portraitUrl).resize(50, 50).centerCrop().error(R.drawable.user_portrait).into(portaitView);
4

2 に答える 2

2

私は同じ問題に直面していましたが、どの解決策も受け入れられませんでした...だから、少なくともリフレクションで解決策を作りました。私はトリミング、サイズ変更などを使用しないため、load(url)サイズ変更、トリミング、変換でのみ使用する場合にのみ、クリア関数が機能します...しかし、完全なgetKey関数をカスタマイズしたので、これは少なくとも拡張する大きな助けになるはずですクリア機能。または、関数をgetKey公開してclear(uri)関数を次のように変更するだけclear(key)です...

ソースコードからキー機能をコピーして採用しただけです。

ツール クラスから picasso インスタンスを取得するだけで、すべてが機能するはずです (私のようなコンテキスト プロバイダーを使用する代わりに、たとえば init 関数を追加して、アプリケーション コンテキストなどで PicassoTools クラスを初期化できます)。

次のツール クラスを使用するだけです。

public class PicassoTools
{
private static Picasso picasso = null;
private static CustomLruCache lruCache = null;

private static CustomLruCache getCache()
{
    if (lruCache == null)
        lruCache = new CustomLruCache(MainApp.getAppContext());
    return lruCache;
}

public static Picasso getPicasso()
{
    if (picasso == null)
        picasso = new Picasso.Builder(MainApp.getAppContext()).memoryCache(getCache()).build();
    return picasso;
}

public static void clear(Uri uri)
{
    getCache().remove(getKey(uri));
}

public static void clearCache()
{
    getCache().clear();
    // Picasso.with(MainApp.getAppContext()).cache.clear();
}

public static void clearCache(Context c)
{
    getCache().clear();
    // Picasso.with(c).cache.clear();
}

private static final int KEY_PADDING = 50; // Determined by exact science.

private static String getKey(Uri uri)
{
    return getKey(uri, null, 0, 0, false, false, null);
}

private static String getKey(Uri uri, Integer resourceId, int targetWidth, int targetHeight, boolean centerCrop, boolean centerInside, List<Transformation> transformations)
{
    StringBuilder builder = new StringBuilder();
    if (uri != null)
    {
        String path = uri.toString();
        builder.ensureCapacity(path.length() + KEY_PADDING);
        builder.append(path);
    }
    else
    {
        builder.ensureCapacity(KEY_PADDING);
        builder.append(resourceId);
    }
    builder.append('\n');

    if (targetWidth != 0)
    {
        builder.append("resize:").append(targetWidth).append('x').append(targetHeight);
        builder.append('\n');
    }
    if (centerCrop)
    {
        builder.append("centerCrop\n");
    }
    else if (centerInside)
    {
        builder.append("centerInside\n");
    }

    if (transformations != null)
    {
        // noinspection ForLoopReplaceableByForEach
        for (int i = 0, count = transformations.size(); i < count; i++)
        {
            builder.append(transformations.get(i).key());
            builder.append('\n');
        }
    }

    return builder.toString();
}
}

拡張キャッシュ クラス:

public class CustomLruCache extends LruCache
{
public CustomLruCache(Context context)
{
    super(context);
}

public CustomLruCache(int value)
{
    super(value);
}

@Override
public Bitmap get(String key)
{
    L.d(this, key);
    return super.get(key);
}

public void remove(String key)
{
    try
    {
        Bitmap value = map.remove(key);

        Field fieldSize = LruCache.class.getDeclaredField("size");
        fieldSize.setAccessible(true);
        Integer size = (Integer) fieldSize.get(this);
        size -= Utils.getBitmapBytes(value);
        fieldSize.set(this, size);

        Field fieldEvictionCount = LruCache.class.getDeclaredField("evictionCount");
        fieldEvictionCount.setAccessible(true);
        Integer evictionCount = (Integer) fieldEvictionCount.get(this);
        evictionCount++;
        fieldEvictionCount.set(this, evictionCount);

    }
    catch (IllegalArgumentException e)
    {
        L.e(this, e);
    }
    catch (IllegalAccessException e)
    {
        L.e(this, e);
    }
    catch (NoSuchFieldException e)
    {
        L.e(this, e);
    }
}
}

PS: Picasso の Invalidate cache に触発されました

于 2014-06-02T18:39:44.497 に答える