<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>wu-media lab</title>
	<atom:link href="http://wu-media.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://wu-media.com</link>
	<description></description>
	<pubDate>Mon, 15 Jun 2009 13:43:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Android - Events the easy (AS3) way</title>
		<link>http://wu-media.com/2009/06/android-events-the-easy-as3-way/</link>
		<comments>http://wu-media.com/2009/06/android-events-the-easy-as3-way/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 17:50:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://wu-media.com/?p=128</guid>
		<description><![CDATA[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&#40;&#34;MyEvent&#34;, new EventListener&#40;&#41; &#123;
    public void run&#40;Event event&#41; &#123;
        // remove the listener if no longer needed
        event.target.removeEventlistener&#40;event.type, this&#41;;
     [...]]]></description>
			<content:encoded><![CDATA[<p>I like the EventDispatcher class from ActionScript 3 so I decided to write one for Java.<br />
Usage</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">foo.<span style="color: #006633;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;MyEvent&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">EventListener</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #003399;">Event</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// remove the listener if no longer needed</span>
        event.<span style="color: #006633;">target</span>.<span style="color: #006633;">removeEventlistener</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">type</span>, <span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// do stuff</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now your class can either extend EventDispatcherImp or implement EventDispatcher and you&#8217;re ready to dispatch events.</p>
<p>Via inheritance</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.wumedia.events.EventDispatcher.EventDispatcherImpl</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Dispatcher <span style="color: #000000; font-weight: bold;">extends</span> EventDispatcherImpl <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Via composition</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.wumedia.events.EventDispatcher.EventDispatcherImpl</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Dispatcher <span style="color: #000000; font-weight: bold;">implements</span> EventDispatcher <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Dispatcher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        _dispatcher <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EventDispatcherImpl<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-128"></span></p>
<p>Here are the 3 required classes,<br />
The Event</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.wumedia.events</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Event</span> <span style="color: #009900;">&#123;</span>	
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> type<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> EventDispatcher target<span style="color: #339933;">;</span>	
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Event</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">type</span> <span style="color: #339933;">=</span> type<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The Listener</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.wumedia.events</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">interface</span> <span style="color: #003399;">EventListener</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #003399;">Event</span> event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The Dispatcher</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.wumedia.events</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.ArrayList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> EventDispatcher <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Register an event listener
	 * @param type		String the name of the event to listen to
	 * @param listener	EventListener the listener
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addEventListener<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> type, <span style="color: #003399;">EventListener</span> listener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Unregister an event listener
	 * @param type		String the name of the event registered to
	 * @param listener	EventListener the listener
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeEventlistener<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> type, <span style="color: #003399;">EventListener</span> listener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Remomve all listeners
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeAllListeners<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Dispatch an Event
	 * @param event	Event
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dispatchEvent<span style="color: #009900;">&#40;</span><span style="color: #003399;">Event</span> event<span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Query to see if there are any listeners registered to a certain event name
	 * @param type	String the event name to query
	 * @return	boolean
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> willTrigger<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EventDispatcherImpl <span style="color: #000000; font-weight: bold;">implements</span> EventDispatcher <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">protected</span> HashMap<span style="color: #339933;">&lt;</span>string , ArrayList<span style="color: #339933;">&lt;</span>EventListener<span style="color: #339933;">&gt;&gt;</span> _listenersMap<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">protected</span> EventDispatcher _eventTarget<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #008000; font-style: italic; font-weight: bold;">/**
		 * Constructor - intended for inheritance
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> EventDispatcherImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			removeAllListeners<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #008000; font-style: italic; font-weight: bold;">/**
		 * Constructor - intended for composition
		 * @param eventTarget EventListener to use as the event target in the Event sent
		 */</span>
		<span style="color: #000000; font-weight: bold;">public</span> EventDispatcherImpl<span style="color: #009900;">&#40;</span>EventDispatcher eventTarget<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			_eventTarget <span style="color: #339933;">=</span> eventTarget<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> addEventListener<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> type, <span style="color: #003399;">EventListener</span> listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			removeEventlistener<span style="color: #009900;">&#40;</span>type, listener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			ArrayList<span style="color: #339933;">&lt;</span>eventlistener<span style="color: #339933;">&gt;</span> listeners <span style="color: #339933;">=</span> _listenersMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> listeners <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				listeners <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;</span>eventlistener<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				_listenersMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>type, listeners<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			listeners.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>listener<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeEventlistener<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> type, <span style="color: #003399;">EventListener</span> listener<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			ArrayList<span style="color: #339933;">&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;</span>eventlistener<span style="color: #339933;">&gt;</span> listeners <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ArrayList<span style="color: #339933;">&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;</span>eventlistener<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> _listenersMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> listeners <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				Iterator<span style="color: #339933;">&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;</span>eventlistener<span style="color: #339933;">&gt;</span> items <span style="color: #339933;">=</span> listeners.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span> items.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">EventListener</span><span style="color: #009900;">&#41;</span> items.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> listener <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		            	items.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		            	<span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
		            <span style="color: #009900;">&#125;</span>
		        <span style="color: #009900;">&#125;</span>
		        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> listeners.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		        	_listenersMap.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		        <span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> removeAllListeners<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			_listenersMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>string , ArrayList<span style="color: #339933;">&lt;</span>EventListener<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dispatchEvent<span style="color: #009900;">&#40;</span><span style="color: #003399;">Event</span> event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			ArrayList<span style="color: #339933;">&lt;</span>eventlistener<span style="color: #339933;">&gt;</span> listeners <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ArrayList<span style="color: #339933;">&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;</span>eventlistener<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> _listenersMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>event.<span style="color: #006633;">type</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> listeners <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				event.<span style="color: #006633;">target</span> <span style="color: #339933;">=</span> _eventTarget <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> _eventTarget <span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
				Iterator<span style="color: #339933;">&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;</span>eventlistener<span style="color: #339933;">&gt;</span> items <span style="color: #339933;">=</span> listeners.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		        <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span> items.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		            <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">EventListener</span><span style="color: #009900;">&#41;</span> items.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #006633;">run</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		        <span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> willTrigger<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			ArrayList<span style="color: #339933;">&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;</span>eventlistener<span style="color: #339933;">&gt;</span> listeners <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ArrayList<span style="color: #339933;">&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;</span>eventlistener<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> _listenersMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">return</span> listeners <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> listeners.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #339933;">&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;/</span>string<span style="color: #339933;">&gt;&lt;/</span>eventlistener<span style="color: #339933;">&gt;&lt;/</span>string<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://wu-media.com/2009/06/android-events-the-easy-as3-way/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Android ImageLoader - load images sequencially in the background</title>
		<link>http://wu-media.com/2009/06/android-imageloader-load-images-sequencially-in-the-background/</link>
		<comments>http://wu-media.com/2009/06/android-imageloader-load-images-sequencially-in-the-background/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 21:47:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://wu-media.com/?p=110</guid>
		<description><![CDATA[A few days ago I started to learn android&#8230; and it&#8217;s been a fairly smooth transition from flash. Although I have to say, as flash developers we&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago I started to learn android&#8230; and it&#8217;s been a fairly smooth transition from flash. Although I have to say, as flash developers we&#8217;re just spoiled. We take for granted all the background stuff flash does for us to make coding that much easier.</p>
<p>One of those things is Loading images. in flash we have the Loader class which makes loading images very easy.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">loader</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Loader</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Loader</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">loader</span>.<span style="color: #004993;">load</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">URLRequest</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;myimage.jpg&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p>In java/android it takes a few more lines</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">HttpURLConnection</span> conn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myimage.jpg&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
conn.<span style="color: #006633;">setDoInput</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
conn.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">InputStream</span> inStream <span style="color: #339933;">=</span> conn.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Bitmap bitmap <span style="color: #339933;">=</span> BitmapFactory.<span style="color: #006633;">decodeStream</span><span style="color: #009900;">&#40;</span>inStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
inStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
conn.<span style="color: #006633;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code above runs in the same thread so you&#8217;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.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">ImageLoader.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span>myImageView, <span style="color: #0000ff;">&quot;myimage.jpg&quot;</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The last parameter tells the ImageLoader class to cache that the bitmap.</p>
<p>The ImageLoader class loads images sequentially so you don&#8217;t slow your mobile device down to a crawl. To cancel Loading simply call clearQueue() and to clear the cache call clearCache()<br />
<span id="more-110"></span></p>
<p>Here&#8217;s the class</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.wumedia.net</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.HttpURLConnection</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashMap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Iterator</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.LinkedList</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Queue</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.graphics.Bitmap</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.graphics.BitmapFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Handler</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.widget.ImageView</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ImageLoader <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">private</span> ImageLoader _instance<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">public</span> ImageLoader getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_instance <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			_instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ImageLoader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> _instance<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> HashMap<span style="color: #339933;">&lt;</span>string , Bitmap<span style="color: #339933;">&gt;</span> _urlToBitmap<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Queue<span style="color: #339933;">&lt;</span>group<span style="color: #339933;">&gt;</span> _queue<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> DownloadThread _thread<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Bitmap _missing<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> _busy<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Constructor
     */</span>
	<span style="color: #000000; font-weight: bold;">private</span> ImageLoader <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		_urlToBitmap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>string , Bitmap<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		_queue <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedList<span style="color: #339933;">&lt;</span>group<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		_busy <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Bitmap get<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> _urlToBitmap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> load<span style="color: #009900;">&#40;</span>ImageView image, <span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		load<span style="color: #009900;">&#40;</span>image, url, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> load<span style="color: #009900;">&#40;</span>ImageView image, <span style="color: #003399;">String</span> url, <span style="color: #000066; font-weight: bold;">boolean</span> cache<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_urlToBitmap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>image<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				image.<span style="color: #006633;">setImageBitmap</span><span style="color: #009900;">&#40;</span>_urlToBitmap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			image.<span style="color: #006633;">setImageBitmap</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			queue<span style="color: #009900;">&#40;</span>image, url, cache<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> queue<span style="color: #009900;">&#40;</span>ImageView image, <span style="color: #003399;">String</span> url, <span style="color: #000066; font-weight: bold;">boolean</span> cache<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Iterator<span style="color: #339933;">&lt;/</span>group<span style="color: #339933;">&gt;&lt;</span>group<span style="color: #339933;">&gt;</span> it <span style="color: #339933;">=</span> _queue.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>image<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">image</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>image<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					it.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>url<span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">url</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					it.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		_queue.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Group</span><span style="color: #009900;">&#40;</span>image, url, <span style="color: #000066; font-weight: bold;">null</span>, cache<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		loadNext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> clearQueue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		_queue <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedList<span style="color: #339933;">&lt;/</span>group<span style="color: #339933;">&gt;&lt;</span>group<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> clearCache<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		_urlToBitmap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashMap<span style="color: #339933;">&lt;</span>string , Bitmap<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> cancel<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		clearQueue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> _thread <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			_thread.<span style="color: #006633;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			_thread <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMissingBitmap<span style="color: #009900;">&#40;</span>Bitmap bitmap<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		_missing <span style="color: #339933;">=</span> bitmap<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> loadNext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Iterator<span style="color: #339933;">&lt;</span>group<span style="color: #339933;">&gt;</span> it <span style="color: #339933;">=</span> _queue.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>_busy <span style="color: #339933;">&amp;&amp;</span> it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			_busy <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">Group</span> group <span style="color: #339933;">=</span> it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			it.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// double check image availability</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_urlToBitmap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">url</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">image</span><span style="color: #339933;">!=</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					group.<span style="color: #006633;">image</span>.<span style="color: #006633;">setImageBitmap</span><span style="color: #009900;">&#40;</span>_urlToBitmap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				_busy <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				loadNext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
				_thread <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DownloadThread<span style="color: #009900;">&#40;</span>group<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				_thread.<span style="color: #006633;">start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> onLoad<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_thread <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">Group</span> group <span style="color: #339933;">=</span> _thread.<span style="color: #006633;">group</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">bitmap</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">cache</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					_urlToBitmap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">url</span>, group.<span style="color: #006633;">bitmap</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">image</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					group.<span style="color: #006633;">image</span>.<span style="color: #006633;">setImageBitmap</span><span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">bitmap</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_missing <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">image</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					group.<span style="color: #006633;">image</span>.<span style="color: #006633;">setImageBitmap</span><span style="color: #009900;">&#40;</span>_missing<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		_thread <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		_busy <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		loadNext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Group</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Group</span><span style="color: #009900;">&#40;</span>ImageView image, <span style="color: #003399;">String</span> url, Bitmap bitmap, <span style="color: #000066; font-weight: bold;">boolean</span> cache<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">image</span> <span style="color: #339933;">=</span> image<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">url</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bitmap</span> <span style="color: #339933;">=</span> bitmap<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">cache</span> <span style="color: #339933;">=</span> cache<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">public</span> ImageView image<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> url<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> Bitmap bitmap<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> cache<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> DownloadThread <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Thread</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> Handler threadHandler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Runnable</span> threadCallback <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Runnable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        	onLoad<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	        <span style="color: #009900;">&#125;</span>
	    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">HttpURLConnection</span> _conn<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Group</span> group<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">public</span> DownloadThread<span style="color: #009900;">&#40;</span><span style="color: #003399;">Group</span> group<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">group</span> <span style="color: #339933;">=</span> group<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003399;">InputStream</span> inStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			_conn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
				_conn <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>group.<span style="color: #006633;">url</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				_conn.<span style="color: #006633;">setDoInput</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				_conn.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				inStream <span style="color: #339933;">=</span> _conn.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				group.<span style="color: #006633;">bitmap</span> <span style="color: #339933;">=</span> BitmapFactory.<span style="color: #006633;">decodeStream</span><span style="color: #009900;">&#40;</span>inStream<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				inStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				_conn.<span style="color: #006633;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				inStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
				_conn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// nothing</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>inStream <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
					inStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			disconnect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			inStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			_conn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
			threadHandler.<span style="color: #006633;">post</span><span style="color: #009900;">&#40;</span>threadCallback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> disconnect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_conn <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				_conn.<span style="color: #006633;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #339933;">&lt;/</span>group<span style="color: #339933;">&gt;&lt;/</span>string<span style="color: #339933;">&gt;&lt;/</span>group<span style="color: #339933;">&gt;&lt;/</span>string<span style="color: #339933;">&gt;&lt;/</span>group<span style="color: #339933;">&gt;&lt;/</span>string<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://wu-media.com/2009/06/android-imageloader-load-images-sequencially-in-the-background/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Google Wave</title>
		<link>http://wu-media.com/2009/05/google-wave/</link>
		<comments>http://wu-media.com/2009/05/google-wave/#comments</comments>
		<pubDate>Sun, 31 May 2009 22:50:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://wu-media.com/?p=108</guid>
		<description><![CDATA[&#8220;Google Wave is a new tool for communication and collaboration on the web, coming later this year.&#8221;
but words don&#8217;t do it justice. You should watch the video for various demo of wave. I can see myself using my email, calendar and documents tools less and less&#8230; and eventually I can see wave replacing them for [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Google Wave is a new tool for communication and collaboration on the web, coming later this year.&#8221;</p>
<p>but words don&#8217;t do it justice. You should watch the video for various demo of wave. I can see myself using my email, calendar and documents tools less and less&#8230; and eventually I can see wave replacing them for good. (if everybody gives up control to the all mighty google that is :))<br />
If there is something it doesn&#8217;t do, you can extend it by writing your own server bot or client side widget that will.</p>
<p>If you haven&#8217;t checked out Google Wave, you should at <a href="http://wave.google.com/" target="_bank">http://wave.google.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wu-media.com/2009/05/google-wave/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Extracting vector from SWFs, writing and now drawing with them</title>
		<link>http://wu-media.com/2009/05/extracting-vector-from-swfs-writing-and-now-drawing-with-them/</link>
		<comments>http://wu-media.com/2009/05/extracting-vector-from-swfs-writing-and-now-drawing-with-them/#comments</comments>
		<pubDate>Tue, 26 May 2009 21:13:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<category><![CDATA[ByteArray]]></category>

		<category><![CDATA[PV3D]]></category>

		<guid isPermaLink="false">http://wu-media.com/?p=84</guid>
		<description><![CDATA[******************** Update: ********************
There&#8217;s now a google project for this: code.google.com/p/swfvector. I&#8217;m going to be updating this so there might be some changes to the how the whole thing works.



I had a lot of positive feedback for my last project that lets you extract font glyphs out of a swf and write text using the flash [...]]]></description>
			<content:encoded><![CDATA[<h3>******************** Update: ********************</h3>
<p><strong>There&#8217;s now a google project for this: <a href="http://code.google.com/p/swfvector" target="_blank">code.google.com/p/swfvector</a></strong>. I&#8217;m going to be updating this so there might be some changes to the how the whole thing works.<br />
</p>
<hr noshade/>
</p>
<p>I had a lot of positive feedback for my last project that lets you extract font glyphs out of a swf and write text using the flash drawing API or one of the popular 3D engines.</p>
<p>Now, this update fixes a couple of bugs and also adds support for any vector shape on the stage or the library.</p>
<p>Demo:</p>
<div class="frame">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_TestVectorShapes_799954694"
			class="flashmovie"
			width="500"
			height="300">
	<param name="movie" value="http://wu-media.com/wp-content/uploads/VectorParser/TestVectorShapes.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://wu-media.com/wp-content/uploads/VectorParser/TestVectorShapes.swf"
			name="fm_TestVectorShapes_799954694"
			width="500"
			height="300">
	<!--<![endif]-->
		
<p><a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object></div>
<p><span id="more-84"></span></p>
<p>The syntax is very similar to the the old DynamicText class. The main difference is that for Text, you need to define a fill type and for vectors, the fill type is ignored because vector shapes have their own fill types.</p>
<p><strong>for Vector Shapes</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #999999;">// extract shape from swf</span>
VectorShapes.extractFromLibrary<span style="color: #000000;">&#40;</span>_loader.<span style="color: #004993;">data</span>, <span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;woman&quot;</span>, <span style="color: #990000;">&quot;snowman&quot;</span><span style="color: #000000;">&#93;</span>, VectorShapes.METHOD_CONTINUOUS_POINTS<span style="color: #000000;">&#41;</span>;
<span style="color: #999999;">// draw shape</span>
VectorShapes.<span style="color: #004993;">draw</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">graphics</span>, <span style="color: #990000;">&quot;woman&quot;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>for Vector Text</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #999999;">// extract font data from swf</span>
VectorText.extractFont<span style="color: #000000;">&#40;</span><span style="color: #004993;">root</span>.<span style="color: #004993;">loaderInfo</span>.bytes<span style="color: #000000;">&#41;</span>;
<span style="color: #999999;">// draw text</span>
<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginFill</span><span style="color: #000000;">&#40;</span>0x0000<span style="color: #000000;">&#41;</span>;
VectorText.write<span style="color: #000000;">&#40;</span><span style="color: #004993;">graphics</span>, <span style="color: #990000;">&quot;_Arial&quot;</span>, <span style="color: #000000; font-weight:bold;">16</span>, <span style="color: #000000; font-weight:bold;">16</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #990000;">&quot;Hello World&quot;</span>, <span style="color: #000000; font-weight:bold;">10</span>, <span style="color: #000000; font-weight:bold;">10</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p><del datetime="2009-06-15T13:43:44+00:00"><br />
<strong>IMPORTANT</strong><br />
There are two methods to parse and draw vectors. METHOD_CONTINUOUS_POINTS &#038; METHOD_REGULAR.<br />
METHOD_CONTINUOUS_POINTS (the default method) parses shapes while trying to keep the points continuous. This is the best way but it does not support flat shapes (more on that later)</p>
<p>METHOD_REGULAR parses the data as it is saved in the swf. This supports all vector shapes but is slower in both parsing and rendering.</p>
<p>For best performance and to get data that is easier to work with. Use METHOD_CONTINUOUS_POINTS , but your vector shapes must not be flat. What do I mean by flat? Flat is when you collapse everything into one layer in flash and keep hitting CTRL+B until all groups are killed and everything is merged. The flash then saves this in a very compact way which makes it difficult to break out (more on that later), Anyway, this flatten shape takes a lot more passes to render and it also create a data set that is not that friendly to work with.</p>
<p>The best way to create your vector data is to have each individual shape grouped so that they don&#8217;t cut into each other. In the included source, the snowman symbol is the right way and the circles symbol is the wrong way.</p>
<p>ok. more,<br />
This is straight out of the SWF file specs:<br />
&#8220;Most vector formats allow only one fill and line style per path. The SWF file format extends<br />
this concept by allowing each edge to have its own line and fill style. This can have<br />
unpredictable results when fill styles change in the middle of a path.&#8221;</p>
<p>This is why i have two methods to parse the data.  METHOD_CONTINUOUS_POINTS  actually does it wrong, because it ignores the two fill style per edge thing. two fills per edges is never used if the shapes don&#8217;t intersect. So even though METHOD_CONTINUOUS_POINTS  is wrong, it is the preferred and default method.<br />
Another thing worth mentioning is that the flash drawing API DOES NOT support two fill per edge. instead flash saves the shapes as triangles with one common point, I&#8217;m using 0,0. It then draws these triangles erasing it&#8217;s 0,0 tail by drawing over it again. that&#8217;s the short version anyway, the whole version is a lot more complicated and i&#8217;m too lazy to go into it.<br />
</del></p>
<p><strong>Source code</strong>: <a href="http://code.google.com/p/swfvector" target="_blank">code.google.com/p/swfvector</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wu-media.com/2009/05/extracting-vector-from-swfs-writing-and-now-drawing-with-them/feed/</wfw:commentRss>
		</item>
		<item>
		<title>SWF file specifications</title>
		<link>http://wu-media.com/2009/05/swf-file-specifications/</link>
		<comments>http://wu-media.com/2009/05/swf-file-specifications/#comments</comments>
		<pubDate>Mon, 11 May 2009 14:01:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://wu-media.com/?p=81</guid>
		<description><![CDATA[If you&#8217;re interested in parsing or building your own SWF files.
here&#8217;s the file specification from Adobe,
http://www.adobe.com/devnet/swf/
]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re interested in parsing or building your own SWF files.</p>
<p>here&#8217;s the file specification from Adobe,</p>
<p><a href="http://www.adobe.com/devnet/swf/" target="_blank">http://www.adobe.com/devnet/swf/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wu-media.com/2009/05/swf-file-specifications/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Extracting font glyphs from SWF and Drawing with them</title>
		<link>http://wu-media.com/2009/05/extracting-font-glyphs-from-swf-and-drawing-with-them/</link>
		<comments>http://wu-media.com/2009/05/extracting-font-glyphs-from-swf-and-drawing-with-them/#comments</comments>
		<pubDate>Fri, 08 May 2009 22:32:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[ActionScript]]></category>

		<category><![CDATA[ByteArray]]></category>

		<category><![CDATA[PV3D]]></category>

		<guid isPermaLink="false">http://wu-media.com/?p=19</guid>
		<description><![CDATA[UPDATE: please use newer and improved version at http://wu-media.com/2009/05/extracting-vector-from-swfs-writing-and-now-drawing-with-them/
Drawing text in flash using font glyph information is definitely not new. five3D does it, PV3D uses the five3D files to do it. and Michael Baczynski also did it.
So what&#8217;s so different with my approach?
I extract the font glyphs from your SWFs, so you don&#8217;t need to [...]]]></description>
			<content:encoded><![CDATA[<h3>UPDATE: please use newer and improved version at <a href="http://wu-media.com/2009/05/extracting-vector-from-swfs-writing-and-now-drawing-with-them/">http://wu-media.com/2009/05/extracting-vector-from-swfs-writing-and-now-drawing-with-them/</a></h3>
<p>Drawing text in flash using font glyph information is definitely not new. five3D does it, PV3D uses the five3D files to do it. and Michael Baczynski also did it.</p>
<p>So what&#8217;s so different with my approach?</p>
<p>I extract the font glyphs from your SWFs, so you don&#8217;t need to go through the extra step of creating a Class that has a bunch of arrays describing your font. You can now embed fonts just as you normally would, and the best part is&#8230; you can use the same fonts with regular TextFields which lowers your overall file size.</p>
<p>SWF files store fonts as a bunch of ShapeRecords the same way it does with graphics youd draw on the Stage in the Flash IDE. These ShapeRecords are composed of a bunched of lineTo, moveTo, curveTo and styles instructions. For fonts we only need the first three. since we&#8217;ll be doing they styling in code when we&#8217;re about to draw text our text.</p>
<p>Now that we&#8217;ve extracted these ShapeRecords. I&#8217;ve created a DynamicText class that draws on a Graphics target. This target can be</p>
<ol>
<li>the graphics property of a DisplayObject</li>
<li>the graphics3D property of a Shape3D in five3D</li>
<li>the graphics property of a VectorShape3D in PV3D</li>
</ol>
<p><strong>Sample code:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #999999;">// extract font information from swf bytes</span>
DynamicText.extractFontInformation<span style="color: #000000;">&#40;</span><span style="color: #004993;">root</span>.<span style="color: #004993;">loaderInfo</span>.bytes, <span style="color: #0033ff; font-weight: bold;">true</span><span style="color: #000000;">&#41;</span>;
<span style="color: #999999;">// set the style</span>
<span style="color: #004993;">graphics</span>.<span style="color: #004993;">beginGradientFill</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">GradientType</span>.<span style="color: #004993;">LINEAR</span>, <span style="color: #000000;">&#91;</span>0xff4400, 0x0<span style="color: #000000;">&#93;</span>, <span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">1</span>, <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#93;</span>, <span style="color: #000000;">&#91;</span>0x00, 0xff<span style="color: #000000;">&#93;</span>, m<span style="color: #000000;">&#41;</span>;
<span style="color: #999999;">// draw the ext</span>
DynamicText.write<span style="color: #000000;">&#40;</span><span style="color: #004993;">graphics</span>, <span style="color: #990000;">&quot;Futura Md BT&quot;</span>, <span style="color: #000000; font-weight:bold;">20</span>, <span style="color: #000000; font-weight:bold;">20</span>, <span style="color: #000000; font-weight:bold;">0</span>, <span style="color: #990000;">&quot;Hello World&quot;</span><span style="color: #000000;">&#41;</span>;
<span style="color: #004993;">graphics</span>.<span style="color: #004993;">endFill</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></td></tr></table></div>

<p><strong>Flash DisplayObject.graphics draw sample:</strong></p>
<div class="frame">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_TestWrite_445992179"
			class="flashmovie"
			width="500"
			height="300">
	<param name="movie" value="http://wu-media.com/wp-content/uploads/DynamicText/TestWrite.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://wu-media.com/wp-content/uploads/DynamicText/TestWrite.swf"
			name="fm_TestWrite_445992179"
			width="500"
			height="300">
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><br />

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<p><span id="more-19"></span><br />
Noticed the gradient fill? because we&#8217;re using the flash drawing API, we can use beginFill, beginBitmapFill and beginGradientFill when drawing to a DisplayObject.</p>
<p>The DynamicText class has the following features,</p>
<ol>
<li>Kerning and Leading that use the same values as photoshop which makes it easier to match text styles to comps</li>
<li>TextBlock alignment of TL, T, TR, L, C, R, BL, B, and BR. TLC, LC and BLC provides centering of the text within the text block</li>
<li>Ability to align the text either using it&#8217;s baseline or it&#8217;s top or bottom edges. baseline alignment provides more accurate alignment, as the text starts from it&#8217;s baseline which also happens to be its zero point.</li>
</ol>
<p><strong>The same code can be use to draw on five3D and PV3D!</strong></p>
<p><strong>five3D Shape3D.graphics3D draw sample:</strong></p>
<div class="frame">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_TestFive3D_1097926591"
			class="flashmovie"
			width="500"
			height="300">
	<param name="movie" value="http://wu-media.com/wp-content/uploads/DynamicText/TestFive3D.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://wu-media.com/wp-content/uploads/DynamicText/TestFive3D.swf"
			name="fm_TestFive3D_1097926591"
			width="500"
			height="300">
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><br />

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<p><strong>PV3D VectorShape3D.graphics draw sample:</strong></p>
<div class="frame">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
			id="fm_TestPV3D_571925393"
			class="flashmovie"
			width="500"
			height="300">
	<param name="movie" value="http://wu-media.com/wp-content/uploads/DynamicText/TestPV3D.swf" />
	<!--[if !IE]>-->
	<object	type="application/x-shockwave-flash"
			data="http://wu-media.com/wp-content/uploads/DynamicText/TestPV3D.swf"
			name="fm_TestPV3D_571925393"
			width="500"
			height="300">
	<!--<![endif]-->
		<a href="http://adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a><br />

	<!--[if !IE]>-->
	</object>
	<!--<![endif]-->
</object>
</div>
<p>Currently, It only works with fonts that were transcoded with the flash IDE. you can still embed the fonts with the [Embed] tag, as long as it&#8217;s from a SWF that was published with the flash IDE. embedding TTF directly with the [Embed] tags breaks my parser for some reason.</p>
<p><strong>Source code</strong>: <a href="http://wu-media.com/wp-content/uploads/DynamicText/DynamicText.zip" onclick="pageTracker._trackEvent('download', 'click', 'DynamicText.zip');">Download</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wu-media.com/2009/05/extracting-font-glyphs-from-swf-and-drawing-with-them/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://wu-media.com/2009/05/hello-world/</link>
		<comments>http://wu-media.com/2009/05/hello-world/#comments</comments>
		<pubDate>Wed, 06 May 2009 00:56:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://wu-media.com/lab/?p=1</guid>
		<description><![CDATA[Welcome. I will use this space to post some experiments I do in my free time.
]]></description>
			<content:encoded><![CDATA[<p>Welcome. I will use this space to post some experiments I do in my free time.</p>
]]></content:encoded>
			<wfw:commentRss>http://wu-media.com/2009/05/hello-world/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
