cialis

Archive for June, 2009

Android - Events the easy (AS3) way

Tuesday, June 9th, 2009

I like the EventDispatcher class from ActionScript 3 so I decided to write one for Java.
Usage

1
2
3
4
5
6
7
foo.addEventListener("MyEvent", new EventListener() {
    public void run(Event event) {
        // remove the listener if no longer needed
        event.target.removeEventlistener(event.type, this);
        // do stuff
    }
}

Now your class can either extend EventDispatcherImp or implement EventDispatcher and you’re ready to dispatch events.

Via inheritance

1
2
3
import com.wumedia.events.EventDispatcher.EventDispatcherImpl;
public class Dispatcher extends EventDispatcherImpl {
}

Via composition

1
2
3
4
5
6
import com.wumedia.events.EventDispatcher.EventDispatcherImpl;
public class Dispatcher implements EventDispatcher {
    public Dispatcher() {
        _dispatcher = new EventDispatcherImpl(this);
    }
}

(more…)

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…)