Java tutorial
/* * Copyright (C) 2012 www.amsoft.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.ab.bitmap; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.concurrent.locks.ReentrantLock; import android.graphics.Bitmap; import android.support.v4.util.LruCache; import android.util.Log; import com.ab.global.AbAppData; import com.ab.util.AbImageUtil; import com.ab.util.AbMd5; import com.ab.util.AbStrUtil; // TODO: Auto-generated Javadoc /** * 2012 amsoft.cn * ??AbImageCache.java * ??. * * @author * @version v1.0 * @date2013-5-23 ?10:10:53 */ public class AbImageCache { /** The tag. */ private static String TAG = "AbImageCache"; /** . */ private static final boolean D = AbAppData.DEBUG; /** ?8MB. */ public static int cacheSize = 8 * 1024 * 1024; /** ?,LruCache. */ private static final LruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) { protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getRowBytes() * bitmap.getHeight(); } @Override protected void entryRemoved(boolean evicted, String key, Bitmap oldValue, Bitmap newValue) { if (D) Log.d(TAG, "LruCache:" + key); } }; /** . */ private static final HashMap<String, Runnable> runRunnableCache = new HashMap<String, Runnable>(); /** . */ private static final List<HashMap<String, Runnable>> waitRunnableList = new ArrayList<HashMap<String, Runnable>>(); /** ?. */ public static final ReentrantLock lock = new ReentrantLock(); /** * ???Bitmap. * * @param key the key * @return the bitmap from mem cache */ public static Bitmap getBitmapFromCache(String key) { return bitmapCache.get(key); } /** * ??. * * @param key urlkey * @param bitmap the bitmap */ public static void addBitmapToCache(String key, Bitmap bitmap) { try { //if(D) Log.d(TAG, "?:"+key); lock.lock(); if (AbStrUtil.isEmpty(key)) { return; } if (getBitmapFromCache(key) == null && bitmap != null) { bitmapCache.put(key, bitmap); //if(D) Log.d(TAG, ":"+key+","+bitmap); //if(D) Log.d(TAG, "??:"+key+","+getBitmapFromCache(key)); } // removeRunRunnableFromCache(key); //if(D) Log.d(TAG, ":"+waitRunnableList.size()); // removeWaitRunnableFromCache(key); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } /** * ??. * * @param key urlkey */ public static void removeBitmapFromCache(String key) { try { lock.lock(); if (getBitmapFromCache(key) != null) { bitmapCache.remove(key); } } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } /** * ??Bitmap. */ public static void removeAllBitmapFromCache() { bitmapCache.evictAll(); } /** * ?urlkey,key+???. * * @param url ?. * @param width . * @param height . * @param type ?. * @return the cache key */ public static String getCacheKey(String url, int width, int height, int type) { if (type == AbImageUtil.ORIGINALIMG) { return AbMd5.MD5(new StringBuilder(url.length() + 12).append(url).toString()); } else { return AbMd5.MD5(new StringBuilder(url.length() + 12).append("#W").append(width).append("#H") .append(height).append("#T").append(type).append(url).toString()); } } /** * ???. * * @param key the key * @return the runnable */ public static Runnable getRunRunnableFromCache(String key) { return runRunnableCache.get(key); } /** * ??. * * @param key urlkey * @param runnable the runnable */ public static void addToRunRunnableCache(String key, Runnable runnable) { try { lock.lock(); if (AbStrUtil.isEmpty(key) || runnable == null) { return; } if (getRunRunnableFromCache(key) == null) { runRunnableCache.put(key, runnable); } } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } /** * ??. * * @param key urlkey */ public static void removeRunRunnableFromCache(String key) { if (getRunRunnableFromCache(key) != null) { runRunnableCache.remove(key); } } /** * ???. * * @param key the key * @return the runnable */ public static Runnable getWaitRunnableFromCache(String key) { return runRunnableCache.get(key); } /** * ??. * * @param key urlkey * @param runnable the runnable */ public static void addToWaitRunnableCache(String key, Runnable runnable) { try { lock.lock(); if (AbStrUtil.isEmpty(key) || runnable == null) { return; } HashMap<String, Runnable> runnableMap = new HashMap<String, Runnable>(); runnableMap.put(key, runnable); waitRunnableList.add(runnableMap); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } /** * ??. * * @param key urlkey */ public static void removeWaitRunnableFromCache(String key) { try { lock.lock(); for (int i = 0; i < waitRunnableList.size(); i++) { HashMap<String, Runnable> runnableMap = waitRunnableList.get(i); Runnable runnable = runnableMap.get(key); if (runnable != null) { //if(D) Log.d(TAG, ":"+runnable); synchronized (runnable) { runnable.notify(); } waitRunnableList.remove(runnableMap); i--; } } } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } }