Android Open Source - iPhoroidUI Image Cache






From Project

Back to project page iPhoroidUI.

License

The source code is released under:

Apache License

If you think the Android project iPhoroidUI listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (c) 2011 by KLab Inc., All rights reserved.
 */*ww  w.  j a  va  2  s .  co  m*/
 * Programmed by iphoroid team
 */

package org.klab.iphoroid.widget.support;

import java.util.HashMap;
import java.util.Map;

import android.graphics.Bitmap;
import android.util.Log;

import org.klab.iphoroid.util.Cache;


/**
 * ????????????
 *
 * <li>WeakHashMap ??? value ? WeakReference ?????????????????? GC ?????????</li>
 * <li>GC ?????????????????????????????????????</li>
 * <li>WeakHashMap ???????? ReferenceQueue</li>
 *
 * @author <a href="mailto:sano-n@klab.jp">Naohide Sano</a> (sano-n)
 */
public abstract class ImageCache {

    /** */
    private static Map<String, Bitmap> cache;

    /** */
    private static Map<String, Bitmap> expired = new HashMap<String, Bitmap>();

static int hitCount;
static int unhitCount;

    /**
     * ????????????????????
     */
    private static int maxSize = 70;

    /**
     * ???????????????????[msec]
     */
    private static int leftTime = 30 * 1000;

    /** */
    public static void setMaxSize(int maxSize) {
        ImageCache.maxSize = maxSize;
    }

    /** */
    public static void setLeftTime(int leftTime) {
        ImageCache.leftTime = leftTime;
    }

    static {
//        cache = new HashMap<String, Bitmap>();
        cache = new Cache<String, Bitmap>(maxSize, leftTime);
        ((Cache<String, Bitmap>) cache).setExpiredListener(new Cache.OnExpiredListener<Bitmap>() {
            @Override
            public void onExpired(Object key, Bitmap bitmap) {
                // ????????????????????????????????????????????????? recycle() ????????????
Log.w("ImageCache", "Expired: " + bitmap + ", " + key);
                expired.put((String) key, bitmap);
            }
        });
    }

    /**
     * thread unsafe
     * @return null when no cache
     */
    public static Bitmap getImage(String key) {
try {
        if (cache.containsKey(key)) {
            Bitmap bitmap = cache.get(key);
++hitCount;
            return bitmap;
        } else {
++unhitCount;
            return null;
        }
} finally {
 Log.d("ImageCache", "cache: hit: " + hitCount + ", fail: " + unhitCount + ", size: " + cache.size() + ", key: " + key);
 Log.d("ImageCache", "mem: " + Runtime.getRuntime().freeMemory() + "/" + Runtime.getRuntime().totalMemory());
}
    }

    /** thread unsafe */
    public static void setImage(String key, Bitmap image) {
        try {
            cache.put(key, image);
        } catch (Exception e) {
Log.e("ImageCache", e.getMessage(), e);
        }
    }

    /** 
     * ??????????????????
     */
    public static void clear() {
        for (Bitmap bitmap : cache.values()) {
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
            }
        }
        cache.clear();
    }

    /**
     * 
     * @return
     */
    public static Map<String, Bitmap> getExpired() {
        return expired;
    }
}




Java Source Code List

org.klab.iphoroid.util.ActivityUtil.java
org.klab.iphoroid.util.Cache.java
org.klab.iphoroid.widget.adpterview.OnScrollListener.java
org.klab.iphoroid.widget.coverflow.CoverFlowGallery.java
org.klab.iphoroid.widget.coverflow.CoverFlowImageAdapterBase.java
org.klab.iphoroid.widget.flowview.CircleFlowIndicator.java
org.klab.iphoroid.widget.flowview.FlowIndicator.java
org.klab.iphoroid.widget.flowview.FlowView.java
org.klab.iphoroid.widget.flowview.TitleFlowIndicator.java
org.klab.iphoroid.widget.flowview.TitleProvider.java
org.klab.iphoroid.widget.gallery.ScrollDetectableGallery.java
org.klab.iphoroid.widget.listview.AdapterWrapper.java
org.klab.iphoroid.widget.listview.EndlessAdapter.java
org.klab.iphoroid.widget.listview.PullToRefreshEndlessListView.java
org.klab.iphoroid.widget.listview.PullToRefreshListView.java
org.klab.iphoroid.widget.listview.RefreshableArrayAdapter.java
org.klab.iphoroid.widget.support.DownloadTask.java
org.klab.iphoroid.widget.support.HasImage.java
org.klab.iphoroid.widget.support.ImageCache.java
org.klab.iphoroid.widget.support.ImageDownloadTask.java
org.klab.iphoroid.widget.support.SimpleImageDownloadTask.java