package com.aincc.lib.util;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import com.aincc.lib.util.ImageCache.ImageCacheParams;
import com.aincc.lib.util.ImageWorker.ImageWorkerAdapter;
/**
*
* <h3><b>ImageLoader</b></h3></br>
*
* 비트맵 이미지 다운로드 관리
*
* @author aincc@barusoft.com
* @version 1.0.0
* @since 1.0.0
*/
public class ImageLoader
{
private ImageWorker imageWorker;
private int thumbSize;
public ImageLoader(Context context, ImageWorkerAdapter adapter, String cacheDir, int loadingResId, int thumbSizeId)
{
init(context, adapter, cacheDir, loadingResId, thumbSizeId, true, true, false);
}
public ImageLoader(Context context, ImageWorkerAdapter adapter, String cacheDir, int loadingResId, int thumbSizeId, boolean memCache, boolean diskCache, boolean clearOnStart)
{
init(context, adapter, cacheDir, loadingResId, thumbSizeId, memCache, diskCache, clearOnStart);
}
/**
*
* @since 1.0.0
* @param context
* @param adapter
* 이미지어댑터
* @param cacheDir
* 캐쉬 폴더
* @param loadingResId
* 로딩이미지 리소스 아이디
* @param thumbSizeId
* 섬네일 크기 리소스 아이디
* @param memCache
* 메모리 캐시 여부
* @param diskCache
* 디스크 캐시 여부
* @param clearOnStart
* 시작시 디스크 캐시 클리어 여부
*/
private void init(Context context, ImageWorkerAdapter adapter, String cacheDir, int loadingResId, int thumbSizeId, boolean memCache, boolean diskCache, boolean clearOnStart)
{
thumbSize = context.getResources().getDimensionPixelSize(thumbSizeId);
ImageCacheParams cacheParams = new ImageCacheParams(cacheDir);
cacheParams.memoryCacheEnabled = memCache;
cacheParams.diskCacheEnabled = diskCache;
cacheParams.clearDiskCacheOnStart = clearOnStart;
// Allocate a third of the per-app memory limit to the bitmap memory cache. This value
// should be chosen carefully based on a number of factors. Refer to the corresponding
// Android Training class for more discussion:
// http://developer.android.com/training/displaying-bitmaps/
// In this case, we aren't using memory for much else other than this activity and the
// ImageDetailActivity so a third lets us keep all our sample image thumbnails in memory
// at once.
cacheParams.memCacheSize = 1024 * 1024 * Utils.getMemoryClass(context) / 3;
// The ImageWorker takes care of loading images into our ImageView children asynchronously
imageWorker = new ImageFetcher(context, thumbSize);
imageWorker.setAdapter(adapter);
imageWorker.setLoadingImage(loadingResId);
if (context instanceof FragmentActivity)
{
imageWorker.setImageCache(ImageCache.findOrCreateCache((FragmentActivity) context, cacheParams));
}
else
{
imageWorker.setImageCache(new ImageCache(context, cacheParams));
}
}
/**
*
* @since 1.0.0
* @param adapter
*/
public void setAdapter(ImageWorkerAdapter adapter)
{
imageWorker.setAdapter(adapter);
}
/**
*
* @since 1.0.0
* @param value
*/
public void setImageFadeIn(boolean value)
{
imageWorker.setImageFadeIn(value);
}
/**
*
* @since 1.0.0
* @param num
* @param imageView
*/
public void loadImage(int num, ImageView imageView)
{
imageWorker.loadImage(num, imageView);
}
/**
*
* @since 1.0.0
* @param data
* @param imageView
*/
public void loadImage(Object data, ImageView imageView)
{
imageWorker.loadImage(data, imageView);
}
/**
*
* @since 1.0.0
* @return
*/
public ImageWorker getImageWorker()
{
return imageWorker;
}
}