
Normally android image view shows only a simple image like JPG, PNG, JPEG format, but sometimes we need to show an animated image in the Android activity. The animated image helps the user to easily understand.
So, when we need to show an animated (GIF) image in the android activity we’ll need another application for that. I would like to present 3 solutions to resolve this problem. Each way has own advantages and disadvantages.
- android.graphics.Movie
- WebView
- ImageView
Using android.graphics.Movie
Android actually can decode and display animated GIFs, using android.graphics.Movie class.
WebView
This solution maybe easiest to way to display an animated image in the android. Webview cannot load Gif from the Drawable resource, It can only load from URL on the Internet or from Assets.
ImageView
We can not directly use Image view to display GIF. I am going to show you the easiest way to do this by a library:
Also read this also:
Replace the whole word with Word Boundaries in Java
There are no given tools to show an animated image on the activity. But we can show an animated image in the Android by adding a dependency in the grade file. After using this dependency we can add a view for GIF image like Image view.
Required dependence is:
compile 'com.github.Cutta:GifView:1.1'
Required repositories is:
maven { url 'https://jitpack.io' }

After adding these dependence and repositories to your gradle file you can use in your activity. Now create your XML layout:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<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.example.legendblogs.animatedimage.MainActivity">
<com.cunoraz.gifview.library.GifView
android:id="@+id/sample_gif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Legend Blogs"
android:textSize="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Mainactivity:
package com.example.legendblogs.animatedimage;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.cunoraz.gifview.library.GifView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GifView sample_gif = (GifView) findViewById(R.id.sample_gif);
sample_gif.setGifResource(R.drawable.legendblogs);
sample_gif.play();
}
}
Mainifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.legendblogs.animatedimage">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
String:
<resources>
<string name="app_name">Animated Image Sample</string>
</resources>
Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.legendblogs.animatedimage"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
maven { url 'http://repo1.maven.org/maven2' }
maven { url 'https://jitpack.io' }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.github.Cutta:GifView:1.1'
}
Did you find this page helpful?
X