<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sukitha Udugamasooriya on Medium]]></title>
        <description><![CDATA[Stories by Sukitha Udugamasooriya on Medium]]></description>
        <link>https://medium.com/@sukithaudugamasooriya?source=rss-4e237ef5b960------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BugE5WJRHeKIK9BX.</url>
            <title>Stories by Sukitha Udugamasooriya on Medium</title>
            <link>https://medium.com/@sukithaudugamasooriya?source=rss-4e237ef5b960------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 17:52:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sukithaudugamasooriya/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[2D game animations in Android]]></title>
            <link>https://medium.com/@sukithaudugamasooriya/2d-game-animations-in-android-3454e5300dea?source=rss-4e237ef5b960------2</link>
            <guid isPermaLink="false">https://medium.com/p/3454e5300dea</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[2d-animation]]></category>
            <dc:creator><![CDATA[Sukitha Udugamasooriya]]></dc:creator>
            <pubDate>Mon, 16 Mar 2020 06:43:52 GMT</pubDate>
            <atom:updated>2020-03-16T08:35:33.538Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JWxYDjMPonGX5Mvc" /><figcaption>Photo by <a href="https://unsplash.com/@polarmermaid?utm_source=medium&amp;utm_medium=referral">Anne Nygård</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Doing heavy animations is tricky in Android, unlike in iOS we do not have a SpriteKit to play around and to use sprites in a memory-efficient way. Android’s way of doing this is to use AnimationDrawable. Basically we need to feed a set of frames (drawables) into an AnimationDrawable as a sequence to achieve animation. (ex: jumping, walking). To gain smoothness into one of these animations (ex: walking), it will require around 15–20 different drawables.</p><p>Define following <strong>walk_animation</strong>.xml</p><pre>&lt;!-- Animation frames, files inside the res/drawable/ folder --&gt;<br> &lt;animation-list android:id=&quot;@+id/selected&quot; android:oneshot=&quot;false&quot;&gt;<br>    &lt;item android:drawable=&quot;@drawable/left_walk_00&quot; android:duration=&quot;50&quot; /&gt;<br>    &lt;item android:drawable=&quot;@drawable/left_walk_01&quot; android:duration=&quot;50&quot; /&gt;<br>    &lt;item android:drawable=&quot;@drawable/left_walk_02&quot; android:duration=&quot;50&quot; /&gt;<br>    &lt;item android:drawable=&quot;@drawable/left_walk_03&quot; android:duration=&quot;50&quot; /&gt;<br>    &lt;item android:drawable=&quot;@drawable/left_walk_04&quot; android:duration=&quot;50&quot; /&gt;<br>    &lt;item android:drawable=&quot;@drawable/left_walk_05&quot; android:duration=&quot;50&quot; /&gt;<br> &lt;/animation-list&gt;</pre><p>You can call the above <strong>walk_animation</strong> xml from your Activity by,</p><pre>ImageView img = (ImageView)findViewById(R.id.img_walking_guy);<br>img.setBackgroundResource(R.drawable.<strong>walk_animation</strong>);<br><br>// Get the background, which has been compiled to an AnimationDrawable object.<br>AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();<br><br>frameAnimation.start();</pre><p>The problem here is if a simple screen has 5–6 different animations, the screen will be loading a mass number of bitmap objects into the memory. Releasing or recycling bitmaps is tricky and your app will likely go out of memory. (Ex. after repeatedly going back and forth to the animation screen).</p><h3><strong>Spine to the Rescue.</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nBFFId8opkyQiWl_" /><figcaption>Photo by <a href="https://unsplash.com/@greatmalinco?utm_source=medium&amp;utm_medium=referral">Daniel Tausis</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Spine engine will combine all repeated parts of animation and export as one image like below so that on the runtime, the app does not have to load a number of drawable. The Spine runtime (pixijs) will map and stitch the animations on the runtime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U-B8x4QNhV3lDepNv66BNw.png" /><figcaption>This is the only image that you have to load to achieve the above running animation.</figcaption></figure><p>The beauty of this solution is the way it gets rid of the frame by frame animations used with AnimationDrawable class in Android SDK. This is a sample demonstration of how Spine 2D assets can be used in Android using PixiJS runtime.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*dwtWJg0Jrq8HUq5wv0jIVg.gif" /><figcaption>Actual demonstration of running and jumping using Spine</figcaption></figure><h3>Required components</h3><p>There are 3 main components.</p><ul><li>Spine exports from Spine2D graphics engine. <a href="http://esotericsoftware.com/">http://esotericsoftware.com/</a></li><li>Spine runtime of your choice. <a href="http://esotericsoftware.com/spine-runtimes">http://esotericsoftware.com/spine-runtimes</a>. This example uses PixiJS <a href="https://www.pixijs.com/">https://www.pixijs.com</a></li><li>Android/iOS app with a WebView.</li></ul><h3>Project structure</h3><p>All the examples are stored in the assets folder and loaded into a WebView.</p><h3>Native to JavaScript calls</h3><p>Include this on the Fragment</p><pre>button.setOnClickListener {<br>  webView.loadUrl(&quot;javascript:jump()&quot;)<br> }</pre><p>And then on the Javascript</p><pre>function jump(){<br> pixie.state.setAnimation(0, &#39;jump&#39;, false);<br> pixie.state.addAnimation(0, &#39;running&#39;, true, 0);<br>}</pre><h3>JavaScript to Native calls</h3><p><strong><em>In Javascript</em></strong></p><pre>function onTouchStart() {<br> JavaScriptInterface.doEchoTest(&quot;Hello from JS&quot;);<br>}</pre><p><strong><em>In native code in Fragment</em></strong></p><pre>webView.addJavascriptInterface(JSInterface(), &quot;JavaScriptInterface&quot;)</pre><p>Then create this class</p><pre>class JSInterface {<br>    @JavascriptInterface<br>    fun doEchoTest(echo: String) {<br>        println(echo)<br>    }<br>}</pre><p>You can find a working sample from this <a href="https://github.com/sukie2/android-2D-animations/">GitHub</a> repository.</p><blockquote><strong>Credits</strong></blockquote><blockquote>PixiJS runtime: <a href="https://www.pixijs.com/">https://www.pixijs.com</a></blockquote><blockquote>Spine: <a href="http://esotericsoftware.com/">http://esotericsoftware.com/</a></blockquote><blockquote>Android developer <a href="https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable">https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable</a></blockquote><h4>Important: Use an Android device for running/debugging Spine. The emulator does not work properly in many cases.</h4><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3454e5300dea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>