Skip to content

Instantly share code, notes, and snippets.

@sunwicked
Last active December 19, 2015 16:18
Show Gist options
  • Save sunwicked/5982510 to your computer and use it in GitHub Desktop.
Save sunwicked/5982510 to your computer and use it in GitHub Desktop.
Implementation of Number Pager Adapter with shell LruCache for caching of imgae. #Currently only memory caching is implemented Things to do : #Add file cache
public class NumberPagerAdapter extends PagerAdapter {
View v;
private LayoutInflater inflater;
private Context contxt;
private LruCache<String, Bitmap> mMemoryCache;
final static int ALPHAS_TO_LEARN = 26;
public NumberPagerAdapter(Context context) {
this.contxt = context;
inflater = LayoutInflater.from(context);
setCache();
}
private void setCache() {
// TODO Auto-generated method stub
// Get max available VM memory, exceeding this amount will throw an
// OutOfMemory exception. Stored in kilobytes as LruCache takes an
// int in its constructor.
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 10;
mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
@SuppressLint("NewApi")
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
} else {
return bitmap.getByteCount() / 1024;
}
}
};
}
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
public void loadBitmap(int resId, ImageView mImageView) {
final String imageKey = String.valueOf(resId);
final Bitmap bitmap = getBitmapFromMemCache(imageKey);
if (bitmap != null) {
mImageView.setImageBitmap(bitmap);
} else {
mImageView.setImageResource(R.drawable.ic_launcher);
BitmapWorkerTask task = new BitmapWorkerTask(mImageView);
task.execute(resId);
}
}
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
ImageView v;
// Decode image in background.
public BitmapWorkerTask(ImageView mImageView) {
// TODO Auto-generated constructor stub
v = mImageView;
}
@Override
protected Bitmap doInBackground(Integer... params) {
//Define your own width and height
final Bitmap bitmap = decodeSampledBitmapFromResource( contxt.getResources(), params[0], 100, 100);
//final Bitmap bitmap = decodeSampledBitmapFromResource(contxt.getResources(), params[0], width, height);
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
v.setImageBitmap(result);
}
}
public Object instantiateItem(View collection, int position) {
v = inflater.inflate(R.layout.alpha_bet, null);
initViews(v, position);
// pos = position;
((ViewPager) collection).addView(v, 0);
return v;
}
/**
* method that initializes and set views
*/
private void initViews(View v, int position) {
//your Resource Id and ImageView
loadBitmap( resId, mImageView)
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return ALPHAS_TO_LEARN;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view.equals((LinearLayout) object);
}
@Override
public void destroyItem(ViewGroup container, int position, Object view) {
((ViewPager) container).removeView((LinearLayout) view);
System.gc();
}
public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inPurgeable = true;
options.inInputShareable = true;
// Calculate inSampleSize Reduces Quality Use if You can sacrifice Quality
// options.inSampleSize=2;
// Removed quality issues
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment