Archived
Lazy loading of dancing images.
This commit is contained in:
1 parent
3acf6bb8b7
commit
da9955cd67
2 files changed
+60
-19
No files matched your search
@@ -0,0 +1,57 @@
|
||||
package com.studentersamfundet.app.network;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.security.InvalidParameterException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
public class ImageLoader extends AsyncTask<String, Void, Drawable> {
|
||||
public final static Map<String, Drawable> cache = new HashMap<String, Drawable>();
|
||||
private final ImageView view;
|
||||
|
||||
public ImageLoader(ImageView... views) {
|
||||
if (views.length > 1)
|
||||
throw new InvalidParameterException("Too many arguments to the function!");
|
||||
|
||||
this.view = views[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Drawable doInBackground(String... params) {
|
||||
if (params.length > 1)
|
||||
throw new InvalidParameterException("Too many arguments to the function!");
|
||||
|
||||
Drawable d = cache.get(params[0]);
|
||||
if (d != null)
|
||||
return d;
|
||||
|
||||
try {
|
||||
HttpURLConnection con = (HttpURLConnection)new URL(params[0]).openConnection();
|
||||
InputStream is = con.getInputStream();
|
||||
|
||||
d = Drawable.createFromStream(is, params[0].toString());
|
||||
cache.put(params[0], d);
|
||||
} catch (IOException e) {
|
||||
Log.e("DNSapp", "An exception has occured while downloading images!", e);
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Drawable result) {
|
||||
super.onPostExecute(result);
|
||||
|
||||
if (result != null && view != null) {
|
||||
view.setImageDrawable(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,12 @@
|
||||
package com.studentersamfundet.app.ui;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.text.Html;
|
||||
@@ -36,6 +30,7 @@ import com.studentersamfundet.app.Event;
|
||||
import com.studentersamfundet.app.FeedFetcher;
|
||||
import com.studentersamfundet.app.R;
|
||||
import com.studentersamfundet.app.RSSParserProgram;
|
||||
import com.studentersamfundet.app.network.ImageLoader;
|
||||
import com.studentersamfundet.app.ui.ChooseCategoryDialog.Callback;
|
||||
|
||||
public class EventListActivity extends BaseDnsActivity {
|
||||
@@ -114,19 +109,8 @@ public class EventListActivity extends BaseDnsActivity {
|
||||
|
||||
/* Set images: */
|
||||
ImageView imageView = (ImageView) row.findViewById(R.id.event_list_row_image);
|
||||
try {
|
||||
String urlStr = e.getImageUri(imageSize).toString();
|
||||
URL ulrn = new URL(urlStr);
|
||||
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
|
||||
InputStream is = con.getInputStream();
|
||||
Bitmap bmp = BitmapFactory.decodeStream(is);
|
||||
|
||||
if (null != bmp) {
|
||||
imageView.setImageBitmap(bmp);
|
||||
}
|
||||
|
||||
} catch (MalformedURLException ex1){ /* TODO: Default image */
|
||||
} catch (IOException ex2) { /* TODO: Default image */ }
|
||||
ImageLoader loader = new ImageLoader(imageView);
|
||||
loader.execute(e.getImageUri(imageSize).toString());
|
||||
|
||||
|
||||
/* Set text: */
|
||||
|
||||
Reference in new issue
Block a user