kamagra side effects

Posts Tagged ‘Android’

Android ImageLoader - load images sequencially in the background

Monday, June 8th, 2009

A few days ago I started to learn android… and it’s been a fairly smooth transition from flash. Although I have to say, as flash developers we’re just spoiled. We take for granted all the background stuff flash does for us to make coding that much easier.

One of those things is Loading images. in flash we have the Loader class which makes loading images very easy.

1
2
var loader:Loader = new Loader();
loader.load(new URLRequest("myimage.jpg"));

In java/android it takes a few more lines

1
2
3
4
5
6
7
HttpURLConnection conn = (HttpURLConnection) new URL("myimage.jpg").openConnection();
conn.setDoInput(true);
conn.connect();
InputStream inStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inStream);
inStream.close();
conn.disconnect();

The code above runs in the same thread so you’ll lock up your UI until the image has finish loading. Adding the Threading code adds quite a bit more code. So I decided to create an ImageLoader class to make my life just a little easier. Now I can load an image to an ImageView with one line of code.

1
ImageLoader.getInstance().load(myImageView, "myimage.jpg", true);

The last parameter tells the ImageLoader class to cache that the bitmap.

The ImageLoader class loads images sequentially so you don’t slow your mobile device down to a crawl. To cancel Loading simply call clearQueue() and to clear the cache call clearCache()
(more…)