Can XML be used for displaying an animated image in my android layout?
Luckily android have a built in ImageView which will let you add images on your android layout easily. Now open your main xml file and paste this code inside your main layout (generally the main xml layout file name is activity_main.xml):
Choose the image that you want to display in your android main xml layout and paste it inside drawable directory. Luckily android have a built in ImageView which will let you add images on your android layout easily.
This example demonstrates how do I create an animation using XML in an android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.
This example demonstrates how do I display animated gif images in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Add the following dependency in build.gradle: Module: app Step 2 − Add the following code to res/layout/activity_main.xml.
Layout
<ImageView
android:layout_width="82dip"
android:layout_height="50dip"
android:layout_centerInParent="true"
android:background="@drawable/spinner"
android:id="@+id/splashSpinner"/>
Drawable
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/anim__000" android:duration="100" />
<item android:drawable="@drawable/anim__001" android:duration="100" />
<item android:drawable="@drawable/anim__002" android:duration="100" />
<item android:drawable="@drawable/anim__003" android:duration="100" />
<item android:drawable="@drawable/anim__004" android:duration="100" />
<item android:drawable="@drawable/anim__005" android:duration="100" />
<item android:drawable="@drawable/anim__006" android:duration="100" />
<item android:drawable="@drawable/anim__007" android:duration="100" />
<item android:drawable="@drawable/anim__008" android:duration="100" />
<item android:drawable="@drawable/anim__009" android:duration="100" />
<item android:drawable="@drawable/anim__010" android:duration="100" />
<item android:drawable="@drawable/anim__011" android:duration="100" />
</animation-list>
JAVA
this.spinner = this.view.findViewById(R.id.splashSpinner);
this.spinner.post(new Runnable()
{
@Override
public void run()
{
AnimationDrawable spinnerAnim = (AnimationDrawable) spinner.getBackground();
if (!spinnerAnim.isRunning())
{
spinnerAnim.start();
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With