diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2012-07-06 18:15:07 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2012-07-06 18:15:07 +0200 |
commit | 1e4a3871ff710e55c6d4464d95e6cbecf373ba77 (patch) | |
tree | 40c0b450309217a01fac1a24ed41f285c7a4f17e /src | |
parent | 65fe944765fb879e0bf75c23b096441c118c522a (diff) | |
download | AntennaPod-1e4a3871ff710e55c6d4464d95e6cbecf373ba77.zip |
Images are now scaled down in order to save memory
Diffstat (limited to 'src')
-rw-r--r-- | src/de/podfetcher/asynctask/FeedImageLoader.java | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/src/de/podfetcher/asynctask/FeedImageLoader.java b/src/de/podfetcher/asynctask/FeedImageLoader.java index 8fcc5bdc6..a36525cf9 100644 --- a/src/de/podfetcher/asynctask/FeedImageLoader.java +++ b/src/de/podfetcher/asynctask/FeedImageLoader.java @@ -76,7 +76,7 @@ public class FeedImageLoader { public void wipeImageCache() { imageCache.evictAll(); } - + public boolean isInCache(FeedImage image) { return imageCache.get(image.getId()) != null; } @@ -86,9 +86,13 @@ public class FeedImageLoader { } class BitmapWorkerTask extends AsyncTask<FeedImage, Void, Void> { + /** The preferred width and height of a bitmap. */ + private static final int PREFERRED_LENGTH = 300; + private static final String TAG = "BitmapWorkerTask"; private ImageView target; private Bitmap bitmap; + private Bitmap decodedBitmap; public BitmapWorkerTask(ImageView target) { super(); @@ -101,10 +105,43 @@ public class FeedImageLoader { target.setImageBitmap(bitmap); } + private int calculateSampleSize(int width, int height) { + int max = Math.max(width, height); + if (max < PREFERRED_LENGTH) { + return max; + } else { + // find first sample size where max / sampleSize < + // PREFERRED_LENGTH + for (int sampleSize = 1, power = 0;; power++, sampleSize = (int) Math + .pow(2, power)) { + int newLength = max / sampleSize; + if (newLength <= PREFERRED_LENGTH) { + if (newLength > 0) { + return sampleSize; + } else { + return sampleSize - 1; + } + } + } + } + } + @Override protected Void doInBackground(FeedImage... params) { if (params[0].getFile_url() != null) { - bitmap = BitmapFactory.decodeFile(params[0].getFile_url()); + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(params[0].getFile_url(), options); + int sampleSize = calculateSampleSize(options.outWidth, + options.outHeight); + + options.inJustDecodeBounds = false; + options.inSampleSize = sampleSize; + decodedBitmap = BitmapFactory.decodeFile( + params[0].getFile_url(), options); + bitmap = Bitmap.createScaledBitmap(decodedBitmap, + PREFERRED_LENGTH, PREFERRED_LENGTH, false); + addBitmapToCache(params[0].getId(), bitmap); Log.d(TAG, "Finished loading bitmaps"); } else { |