Thursday, September 18, 2014

GIF Animation Android

Guys,

Here I am posting the code for GIF animation in android.

Create the new android project.

Android Manifest file :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rahul.gifanimation"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:hardwareAccelerated="false"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Create MainActivity.class

package com.rahul.gifanimation;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

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

Create the Custom View for GIF animation GifView.class

package com.rahul.gifanimation;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

class GifView extends View {
 
private InputStream gifInputStream;
private Movie gifMovie;
private int movieWidth, movieHeight;
private long movieDuration;
private long mMovieStart;

public GifView(Context context) {
 super(context);
 init(context);
}

public GifView(Context context, AttributeSet attrs) {
 super(context, attrs);
 init(context);
}

public GifView(Context context, AttributeSet attrs, 
  int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 init(context);
}
 
private void init(Context context){
 setFocusable(true);
 gifInputStream = context.getResources()
   .openRawResource(R.drawable.rotate); // add your GIF file
 
 gifMovie = Movie.decodeStream(gifInputStream);
 movieWidth = gifMovie.width();
 movieHeight = gifMovie.height();
 movieDuration = gifMovie.duration();
}

@Override
protected void onMeasure(int widthMeasureSpec, 
  int heightMeasureSpec) {
 setMeasuredDimension(movieWidth, movieHeight);
}
 
public int getMovieWidth(){
 return movieWidth;
}
 
public int getMovieHeight(){
 return movieHeight;
}
 
public long getMovieDuration(){
 return movieDuration;
}

@Override
protected void onDraw(Canvas canvas) {

 long now = android.os.SystemClock.uptimeMillis();
       if (mMovieStart == 0) {   // first time
           mMovieStart = now;
       }
       
       if (gifMovie != null) {

           int dur = gifMovie.duration();
           if (dur == 0) {
               dur = 1000;
           }

           int relTime = (int)((now - mMovieStart) % dur);
           
           gifMovie.setTime(relTime);

           gifMovie.draw(canvas, 0, 0);
           invalidate();
           
       }
       
}


}


Finally Create the XML file activity_main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.rahul.gifanimation.MainActivity" >

    <FrameLayout
        android:id="@+id/addview"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@android:color/holo_purple" >

        <com.rahul.gifanimation.GifView
            android:id="@+id/addviewa"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center" >
        </com.rahul.gifanimation.GifView>

    </FrameLayout>


</RelativeLayout>

Here is the GIF image