Open top menu

In this blog post i am explain briefly about how to display the animated gif image in Android. I have explained step by step…
 

Step 1:

Save your image which you want to display in drawable folder.

Step 2:

Create GifView.java in your src/your package folder…
package com.androidbeginner.testcode;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

public class GifView extends View {

                private InputStream giffileInputStream;
                private Movie giffileMovie;
                private int filemovieWidth, filemovieHeight;
                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);
                                giffileInputStream = context.getResources().openRawResource(
                                                                R.drawable.ripple);

                                giffileMovie = Movie.decodeStream(giffileInputStream);
                                filemovieWidth = giffileMovie.width();
                                filemovieHeight = giffileMovie.height();
                                movieDuration = giffileMovie.duration();
                }

                @Override
                protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                                setMeasuredDimension(filemovieWidth, filemovieHeight);
                }

                public int getMovieWidth() {
                                return filemovieWidth;
                }

                public int getMovieHeight() {
                                return filemovieHeight;
                }

                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 (giffileMovie != null) {

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

                                                int relTime = (int) ((now - mMovieStart) % dur);

                                                giffileMovie.setTime(relTime);

                                                giffileMovie.draw(canvas, 0, 0);
                                                invalidate();

                                }

                }

}


Step 3:

Create a gif_activity.xml file inside your res/layout folder …
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.androidgif.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:textSize="30dip"
        android:text="android biginnerpoint"
        android:textStyle="bold"
        android:layout_marginBottom="40dip"
        android:layout_marginTop="10dip"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        

        <com.androidbeginner.testcode.GifView
            android:id="@+id/giffileview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:id="@+id/textinfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="info..." />

</LinearLayout>

Step 4:

Create a file called GifAnimation.java in your src/your package folder …
package com.androidbeginner.testcode;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class GifAnimation extends Activity {

 TextView textInfo;
 GifView gifView;


 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.gif_activity);
 
  gifView = (GifView)findViewById(R.id.giffileview);
 
  textInfo = (TextView)findViewById(R.id.textinfo);

  String stringvalue = "";
 
  stringvalue += "Duration: " + gifView.getMovieDuration() + "\n";
 
  stringvalue += "Width x Hight: "
    + gifView.getMovieWidth() + " x "
    + gifView.getMovieHeight() + "\n";
  
  textInfo.setText(stringvalue);

 }

}

Step 5:

Next add the followin attrs.xml file inside “res/values” folder.

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />
   
   
   
   
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
       
        <activity
            android:name="com.androidbeginner.testcode.GifAnimation"
            android:label="@string/app_name"
    <--Dose note forget this-->
            android:hardwareAccelerated="false">
           
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
           
        </activity>
       
       
    </application>

</manifest>






 the post very helpful…

0 comments