Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display an animated PNG image to my layout in android using XML

Tags:

android

xml

image

Can XML be used for displaying an animated image in my android layout?

like image 502
sahil Avatar asked Jan 31 '12 13:01

sahil


People also ask

How to add an image to an 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):

How to display image in Android 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.

How to create an animation using XML in an Android app?

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.

How to display animated GIF images in Android?

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.


1 Answers

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();
            }
        }
    });
like image 187
ChristopheCVB Avatar answered Nov 15 '22 04:11

ChristopheCVB