{"id":2493,"date":"2017-05-02T16:55:37","date_gmt":"2017-05-02T16:55:37","guid":{"rendered":"http:\/\/webartdevelopers.com\/?p=2493"},"modified":"2017-05-02T16:55:37","modified_gmt":"2017-05-02T16:55:37","slug":"how-to-perform-image-animation-in-android","status":"publish","type":"post","link":"https:\/\/webartdevelopers.com\/blog\/how-to-perform-image-animation-in-android\/","title":{"rendered":"How to Perform Image Animation in Android"},"content":{"rendered":"<p>In this tutorial how to perform Image Animation in Android is shown.<\/p>\n<p>Point to Remember:<\/p>\n<ul>\n<li>Add animation.xml file under the same file where all your images for the animation are located, as in my case it is under &#8216;res\/drawable-hdpi&#8217; folder.<\/li>\n<li>If you want your animation to repeat continuously then keep the &#8216;android:oneshot&#8217; property of the &lt;animation-list&gt; to false.<\/li>\n<\/ul>\n<p>Code:<\/p>\n<p>activity_main.xml<\/p>\n<p>&lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nxmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221;<br \/>\nandroid:layout_width=&#8221;match_parent&#8221;<br \/>\nandroid:layout_height=&#8221;match_parent&#8221;<br \/>\nandroid:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\nandroid:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221;<br \/>\nandroid:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221;<br \/>\ntools:context=&#8221;.MainActivity&#8221; &gt;<\/p>\n<p>&lt;ImageView<br \/>\nandroid:id=&#8221;@+id\/imageView1&#8243;<br \/>\nandroid:layout_width=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_height=&#8221;wrap_content&#8221;<br \/>\nandroid:layout_alignParentTop=&#8221;true&#8221;<br \/>\nandroid:layout_centerHorizontal=&#8221;true&#8221;<br \/>\nandroid:layout_marginTop=&#8221;211dp&#8221; \/&gt;<\/p>\n<p>&lt;\/RelativeLayout&gt;<\/p>\n<p>res\/drawable-hdpi\/animation.xml<\/p>\n<p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt;<br \/>\n&lt;animation-list xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221;<br \/>\nandroid:oneshot=&#8221;false&#8221;&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img1&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img2&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img3&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img4&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img5&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img6&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img7&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img8&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img9&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<br \/>\n&lt;item android:drawable=&#8221;@drawable\/img10&#8243;<br \/>\nandroid:duration=&#8221;120&#8243;\/&gt;<\/p>\n<p>&lt;\/animation-list&gt;<\/p>\n<p>MainActivity.java<\/p>\n<p>package com.example.demo;<\/p>\n<p>import android.os.Bundle;<br \/>\nimport android.app.Activity;<br \/>\nimport android.graphics.drawable.AnimationDrawable;<br \/>\nimport android.view.Menu;<br \/>\nimport android.widget.ImageView;<\/p>\n<p>public class MainActivity extends Activity{<\/p>\n<p>ImageView anm;<\/p>\n<p>@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\nsuper.onCreate(savedInstanceState);<br \/>\nsetContentView(R.layout.activity_main);<br \/>\nanm=(ImageView)findViewById(R.id.imageView1);<br \/>\nanm.setBackgroundResource(R.drawable.animation);<br \/>\n}<\/p>\n<p>public void onWindowFocusChanged(boolean hasFocus)<br \/>\n{<br \/>\nsuper.onWindowFocusChanged(hasFocus);<br \/>\nAnimationDrawable frameAnimation=(AnimationDrawable)anm.getBackground();<br \/>\nif(hasFocus)<br \/>\n{<br \/>\nframeAnimation.start();<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nframeAnimation.start();<br \/>\n}<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic boolean onCreateOptionsMenu(Menu menu) {<br \/>\n\/\/Inflate the menu; this adds items to the action bar if it is present.<br \/>\ngetMenuInflater().inflate(R.menu.main, menu);<br \/>\nreturn true;<br \/>\n}<br \/>\n}<\/p>\n<p><a href=\"https:\/\/youtu.be\/oWjABm-cvmo\">https:\/\/youtu.be\/oWjABm-cvmo<\/a><\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"In this tutorial how to perform Image Animation in Android is shown. Point to Remember: Add animation.xml file under the same file where all your images for the animation are located, as in my case it is under &#8216;res\/drawable-hdpi&#8217; folder. If you want your animation to repeat continuously then keep the &#8216;android:oneshot&#8217; property of the &lt;animation-list&gt; to false. Code: activity_main.xml &lt;RelativeLayout xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221; xmlns:tools=&#8221;http:\/\/schemas.android.com\/tools&#8221; android:layout_width=&#8221;match_parent&#8221; android:layout_height=&#8221;match_parent&#8221; android:paddingBottom=&#8221;@dimen\/activity_vertical_margin&#8221; android:paddingLeft=&#8221;@dimen\/activity_horizontal_margin&#8221; android:paddingRight=&#8221;@dimen\/activity_horizontal_margin&#8221; android:paddingTop=&#8221;@dimen\/activity_vertical_margin&#8221; tools:context=&#8221;.MainActivity&#8221; &gt; &lt;ImageView android:id=&#8221;@+id\/imageView1&#8243; android:layout_width=&#8221;wrap_content&#8221; android:layout_height=&#8221;wrap_content&#8221; android:layout_alignParentTop=&#8221;true&#8221; android:layout_centerHorizontal=&#8221;true&#8221; android:layout_marginTop=&#8221;211dp&#8221; \/&gt; &lt;\/RelativeLayout&gt; res\/drawable-hdpi\/animation.xml &lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243;?&gt; &lt;animation-list xmlns:android=&#8221;http:\/\/schemas.android.com\/apk\/res\/android&#8221; android:oneshot=&#8221;false&#8221;&gt; &lt;item android:drawable=&#8221;@drawable\/img1&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;item android:drawable=&#8221;@drawable\/img2&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;item android:drawable=&#8221;@drawable\/img3&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;item android:drawable=&#8221;@drawable\/img4&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;item android:drawable=&#8221;@drawable\/img5&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;item android:drawable=&#8221;@drawable\/img6&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;item android:drawable=&#8221;@drawable\/img7&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;item android:drawable=&#8221;@drawable\/img8&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;item android:drawable=&#8221;@drawable\/img9&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;item android:drawable=&#8221;@drawable\/img10&#8243; android:duration=&#8221;120&#8243;\/&gt; &lt;\/animation-list&gt; MainActivity.java package <!-- AddThis Advanced Settings generic via filter on get_the_excerpt -->","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[93],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2493"}],"collection":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/comments?post=2493"}],"version-history":[{"count":2,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2493\/revisions"}],"predecessor-version":[{"id":2495,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/posts\/2493\/revisions\/2495"}],"wp:attachment":[{"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/media?parent=2493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/categories?post=2493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webartdevelopers.com\/blog\/wp-json\/wp\/v2\/tags?post=2493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}