Adsense Ad

Wednesday 13 September 2017

How to Use Animated GIFs on Android

Views and Drawable for displaying animated GIFs on Android

Overview

Bundled GIFLib via JNI is used to render frames. This way should be more efficient than WebView or Movie classes.

Setup

Gradle (Android Studio)

Copy / Paste following in gradle.build (Project: project_name) in buildscript and allprojects
buildscript {
    repositories {
        mavenCentral()
    }
}
allprojects {
    repositories {
        mavenCentral()
    }
}

Gradle, snapshot repository

Copy / Paste following in gradle.build (Module: app) in dependencies
dependencies {
    compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.+'
}

Now click on sync to compile

From XML

The simplest way is to use GifImageView like a normal ImageView:
First copy/paste any animated GIF in drawable and then,
Go to layout XML and copy/paste following code:
<pl.droidsonroids.gif.GifImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/your_image"
    android:background="#fff"
    />
Now execute your project on your cellphone screen

No comments: