Thursday, June 27, 2013

Android AnimationDrawable - Loading Example

0 comments

Android Drawable Animation 


In this tutorial you will learn how to animate Image set by using Android AnimationDrawable 




loading.xml

res/anim/loading.xml


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/voipemLoading"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/loading_01"
        android:duration="300"/> 
    <item
        android:drawable="@drawable/loading_02"
        android:duration="300"/>
    <item
        android:drawable="@drawable/loading_03"
        android:duration="300"/>
    <item
        android:drawable="@drawable/loading_04"
        android:duration="300"/>

</animation-list>


DravableAnimation


package com.javasrilankansupport.drawableanimation;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;

public class DravableAnimation extends Activity implements OnClickListener {

 private AnimationDrawable mAnimation;
 private ImageView mAnimLogo;
 private Button mbtnStart;
 private boolean mStart;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dravable_animation);

  mAnimLogo = (ImageView) findViewById(R.id.loading_image);
  mAnimation = (AnimationDrawable) mAnimLogo.getDrawable();
  mbtnStart = (Button) findViewById(R.id.btn_start);

  mbtnStart.setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  if (!mStart) {
    // start animation 
   mAnimLogo.post(new Runnable() {
    @Override
    public void run() {
     mAnimLogo.setVisibility(View.VISIBLE);
     mAnimation.start();
     mStart = true;
     mbtnStart.setText("Stop");
    }
   });
  } else {     
   // stop animation            
   mAnimation.stop();
   mbtnStart.setText("Start");
   mStart = false;
  }
 }
}


dravable_animation.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DravableAnimation"
    android:background="@drawable/bg_login" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="44dp"
        android:text="@string/hello_world"
        android:textColor="@color/black"
        android:textSize="24sp" />

    <ImageView
        android:id="@+id/loading_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:src="@anim/loading"
        android:visibility="invisible" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/loading_image"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="@string/btn_start" />

</RelativeLayout>

Download AnimationDrawable complete source code 




0 comments:

Post a Comment