Permalink
Browse files

do some sane/conservative downsampling restricted to the device dimen…

…sions...
  • Loading branch information...
1 parent 15009fb commit f3ae545d6daad5ae1d87f42c255128dfd4b49db4 @koush committed Aug 24, 2012
@@ -27,13 +27,16 @@
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Looper;
import android.util.DisplayMetrics;
+import android.view.Display;
+import android.view.WindowManager;
import android.widget.ImageView;
public final class UrlImageViewHelper {
@@ -63,11 +66,32 @@ private static void prepareResources(Context context) {
mResources = new Resources(mgr, mMetrics, context.getResources().getConfiguration());
}
- private static BitmapDrawable loadDrawableFromStream(Context context, InputStream stream) {
+ private static BitmapDrawable loadDrawableFromStream(Context context, String filename, int targetWidth, int targetHeight) {
prepareResources(context);
- final Bitmap bitmap = BitmapFactory.decodeStream(stream);
- //Log.i(LOGTAG, String.format("Loaded bitmap (%dx%d).", bitmap.getWidth(), bitmap.getHeight()));
- return new BitmapDrawable(mResources, bitmap);
+
+// System.out.println(targetWidth);
+// System.out.println(targetHeight);
+ try {
+ BitmapFactory.Options o = new BitmapFactory.Options();
+ o.inJustDecodeBounds = true;
+ FileInputStream stream = new FileInputStream(filename);
+ BitmapFactory.decodeStream(stream, null, o);
+ stream.close();
+ stream = new FileInputStream(filename);
+ int scale = 0;
+ while ((o.outWidth >> scale) > targetWidth || (o.outHeight >> scale) > targetHeight) {
+// System.out.println("downsampling");
+ scale++;
+ }
+ o = new Options();
+ o.inSampleSize = 1 << scale;
+ final Bitmap bitmap = BitmapFactory.decodeStream(stream, null, o);
+ //Log.i(LOGTAG, String.format("Loaded bitmap (%dx%d).", bitmap.getWidth(), bitmap.getHeight()));
+ return new BitmapDrawable(mResources, bitmap);
+ }
+ catch (IOException e) {
+ return null;
+ }
}
public static final int CACHE_DURATION_INFINITE = Integer.MAX_VALUE;
@@ -185,15 +209,17 @@ private static void cleanup(Context context) {
private static void setUrlDrawable(final Context context, final ImageView imageView, final String url, final Drawable defaultDrawable, long cacheDurationMs, final UrlImageViewCallback callback) {
cleanup(context);
// disassociate this ImageView from any pending downloads
- if (imageView != null)
- mPendingViews.remove(imageView);
-
if (isNullOrEmpty(url)) {
if (imageView != null)
imageView.setImageDrawable(defaultDrawable);
return;
}
+ WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
+ Display display = wm.getDefaultDisplay();
+ final int tw = display.getWidth();
+ final int th = display.getHeight();
+
Drawable drawable = cache.get(url);
if (drawable != null) {
// Log.i(LOGTAG, "Cache hit on: " + url);
@@ -237,12 +263,13 @@ private static void setUrlDrawable(final Context context, final ImageView imageV
downloads.add(imageView);
mPendingDownloads.put(url, downloads);
+ final int targetWidth = tw <= 0 ? Integer.MAX_VALUE : tw;
+ final int targetHeight = th <= 0 ? Integer.MAX_VALUE : th;
final Loader loader = new Loader() {
@Override
public void run() {
try {
- FileInputStream fis = new FileInputStream(filename);
- result = loadDrawableFromStream(context, fis);
+ result = loadDrawableFromStream(context, filename, targetWidth, targetHeight);
}
catch (Exception ex) {
}
@@ -269,7 +296,9 @@ public void run() {
if (usableResult != null) {
final Drawable newImage = usableResult;
final ImageView imageView = iv;
+// System.out.println(String.format("imageView: %dx%d, %dx%d", imageView.getMeasuredWidth(), imageView.getMeasuredHeight(), imageView.getWidth(), imageView.getHeight()));
imageView.setImageDrawable(newImage);
+// System.out.println(String.format("imageView: %dx%d, %dx%d", imageView.getMeasuredWidth(), imageView.getMeasuredHeight(), imageView.getWidth(), imageView.getHeight()));
if (callback != null)
callback.onLoaded(imageView, loader.result, url, false);
}
@@ -12,4 +12,4 @@
android.library.reference.1=../UrlImageViewHelper
# Project target.
-target=android-4
+target=android-16
@@ -73,6 +73,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+
setContentView(R.layout.main);
final Button search = (Button)findViewById(R.id.search);

0 comments on commit f3ae545

Please sign in to comment.