title: How to load the gif file on button click in android?
body: <p>I am new one in android. I want to load or play the <code>gif</code> image on button click. I load the two <code>gif</code> file on layout. But when I click the next button next, next image will be played, here where we write setonclickListener for button click.... how to play the <code>gif</code> image on button click in android. If you know please tell me.</p>
  
  <p>Here is my code..</p>
  
  <pre><code>package com.androidqa;
  
  import java.io.ByteArrayOutputStream;
  import java.io.InputStream;
  import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.Movie;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  
  
  public class AnimationView extends View 
  {
      private Movie mMovie,mMovie1;
      private long mMovieStart;
      private long mMovieStart1;
      Button btnFirst,btnSecond;
      private static final boolean DECODE_STREAM = true;
      private static byte[] streamToBytes(InputStream is) 
      {
      ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
       byte[] buffer = new byte[1024];
       int len;
       try 
       {
          while ((len = is.read(buffer)) &gt;= 0) {
            os.write(buffer, 0, len);
          }
        } catch (java.io.IOException e) {
        }
        return os.toByteArray();
      }
      @SuppressWarnings("unused")
      private static byte[] streamToBytes1(InputStream is1) 
      {
      ByteArrayOutputStream os1 = new ByteArrayOutputStream(1024);
       byte[] buffer1 = new byte[1024];
       int len;
       try 
       {
          while ((len = is1.read(buffer1)) &gt;= 0) {
            os1.write(buffer1, 0, len);
          }
        } catch (java.io.IOException e) {
        }
        return os1.toByteArray();
      }
  
       public AnimationView(Context context,AttributeSet attrs) 
       {
        super(context,attrs);
        setFocusable(true);
        java.io.InputStream is;
        java.io.InputStream is1;
        is = context.getResources().openRawResource(R.drawable.th_welcome);
        is1 = context.getResources().openRawResource(R.drawable.animimage);
        if (DECODE_STREAM) 
        {
          mMovie = Movie.decodeStream(is);
          mMovie1=Movie.decodeStream(is1);
        } else 
        {
          byte[] array = streamToBytes(is);
          mMovie = Movie.decodeByteArray(array, 0, array.length);
          byte[] array1 = streamToBytes1(is1);
          mMovie1= Movie.decodeByteArray(array1, 0, array1.length);
        }
      }
      @Override
      public void onDraw(Canvas canvas) 
      {
       long now = android.os.SystemClock.uptimeMillis();
  
        if (mMovieStart == 0) { // first time
          mMovieStart = now;
        }
        if (mMovie != null) 
        {
          int dur = mMovie.duration();
          if (dur == 0) {
            dur = 3000;
          }
          int relTime = (int) ((now - mMovieStart) % dur);
         Log.d("", "real time :: " +relTime);
          mMovie.setTime(relTime);
          mMovie.draw(canvas, getWidth() - 200, getHeight()-200);
          invalidate();
        }
        if (mMovieStart1 == 0) { // first time
            mMovieStart1 = now;
          }
          if (mMovie1 != null) 
          {
            int dur = mMovie1.duration();
            if (dur == 0) {
              dur = 3000;
            }
            int relTime = (int) ((now - mMovieStart1) % dur);
           Log.d("", "real time :: " +relTime);
            mMovie1.setTime(relTime);
            mMovie1.draw(canvas, getWidth() - 200, getHeight()-200);
            invalidate();
          }
      }
    }
  </code></pre>