Create a Live Wallpaper on Android Using an Animated GIF
Have you ever seen a beautiful animated GIF that loops seamlessly and wondered if you could have a GIF live wallpaper on your Android device? Well, you can. Continue reading to learn how to set a GIF as a wallpaper on Android.
How to Set a GIF As a Wallpaper on Android
Creating an interesting and beautiful live wallpaper from scratch using only math and code to generate the graphics can be tedious and time-consuming. It also requires lots of creativity. On the other hand, creating an animated GIF or finding one online is a lot easier. In this tutorial, you are going to learn how to convert any animated GIF into a live wallpaper.
Prerequisites
Ensure that you have the latest version of Android Studio set up. You can get it from the Android Developer website.
Even though any animated GIF will do, I suggest that you download a good cinemagraph. A cinemagraph is nothing but an animated GIF—usually created from a video—that loops seamlessly.
You can find Premium and high-quality stock videos from Envato Elements. Or if you're looking for free videos, try Mixkit. Here you'll find awesome video options for free! To turn these videos into a GIF, use this online tool, it allows you to pick any URL or video from your computer and create a GIF out of it.
1. Create a New Project
Start Android Studio, create a new project, and name the project GIF Wallpaper Android. Pick a unique package name if you plan to publish this app on Google Play.



Set the minimum SDK to API 8: Android 2.2 (Froyo).



Our app is not going to have an Activity, so choose Add No Activity and click Finish.



2. Describe the GIF Wallpaper for Android
A GIFl wallpaper that will be live needs a file that describes it. Create a new XML file named res/xml/wallpaper.xml and replace its contents with the following XML:
1 |
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
<wallpaper
|
3 |
xmlns:android="http://schemas.android.com/apk/res/android" |
4 |
android:label="GIF Wallpaper" |
5 |
android:thumbnail="@drawable/ic_launcher"> |
6 |
</wallpaper>
|
The label and thumbnail are particularly important as they will be used when the wallpaper shows up in the list of the wallpapers available on your device.
3. Edit the Manifest
To run as a GIF live wallpaper, our app needs only one permission, android.permission.BIND_WALLPAPER.
A GIF live wallpaper runs as a Service that can receive the android.service.wallpaper.WallpaperService intent action. Name the Service GIFWallpaperService and add it to the project's manifest, AndroidManifest.xml.
1 |
<service
|
2 |
android:name=".GIFWallpaperService" |
3 |
android:enabled="true" |
4 |
android:label="GIF Wallpaper" |
5 |
android:permission="android.permission.BIND_WALLPAPER" > |
6 |
<intent-filter>
|
7 |
<action android:name="android.service.wallpaper.WallpaperService"/> |
8 |
</intent-filter>
|
9 |
<meta-data
|
10 |
android:name="android.service.wallpaper" |
11 |
android:resource="@xml/wallpaper" > |
12 |
</meta-data>
|
13 |
</service>
|
Next, to make sure that the app can be installed only on devices that can run live wallpapers, add the following snippet to the manifest:
1 |
<uses-feature
|
2 |
android:name="android.software.live_wallpaper" |
3 |
android:required="true" > |
4 |
</uses-feature>
|
4. Add Animated GIF
Copy the animated GIF you downloaded from Flickr to the assets folder of the project. I've named the GIF girl.gif.
5. Create the Service
Create a new Java class and name it GIFWallpaperService.java. This class should extend the WallpaperService class.
1 |
public class GIFWallpaperService extends WallpaperService { |
2 |
|
3 |
}
|
Because WallpaperService is an abstract class, you have to override its onCreateEngine method and return an instance of your own Engine, which can render the frames of the GIF.
To use the animated GIF, you first have to convert it into a Movie object. You can use the Movie class's decodeStream method to do so. Once the Movie object has been created, pass it as a parameter to the constructor of the custom Engine.
This is what the onCreateEngine method should look like:
1 |
@Override
|
2 |
public WallpaperService.Engine onCreateEngine() { |
3 |
try { |
4 |
Movie movie = Movie.decodeStream( |
5 |
getResources().getAssets().open("girl.gif")); |
6 |
|
7 |
return new GIFWallpaperEngine(movie); |
8 |
}catch(IOException e){ |
9 |
Log.d("GIF", "Could not load asset"); |
10 |
return null; |
11 |
}
|
12 |
}
|
6. Create the Engine
Let's start working on the Engine now. Create a class named GIFWallpaperEngine inside the GIFWallpaperService class and make it extend WallpaperService.Engine.
Add the following fields to this new class:
-
frameDuration: This integer represents the delay between re-draw operations. A value of 20 gives you 50 frames per second. -
visible: This boolean lets the engine know if the live wallpaper is currently visible on the screen. This is important, because we should not be drawing the wallpaper when it isn't visible. -
movie: This is the animated GIF in the form of aMovieobject. -
holder: This refers to theSurfaceHolderobject available to the engine. It has to be initialized by overriding theonCreatemethod. -
handler: This is aHandlerobject that will be used to start aRunnablethat is responsible for actually drawing the wallpaper.
Your class should now look like this:
1 |
private class GIFWallpaperEngine extends WallpaperService.Engine { |
2 |
private final int frameDuration = 20; |
3 |
|
4 |
private SurfaceHolder holder; |
5 |
private Movie movie; |
6 |
private boolean visible; |
7 |
private Handler handler; |
8 |
|
9 |
public GIFWallpaperEngine(Movie movie) { |
10 |
this.movie = movie; |
11 |
handler = new Handler(); |
12 |
}
|
13 |
|
14 |
@Override
|
15 |
public void onCreate(SurfaceHolder surfaceHolder) { |
16 |
super.onCreate(surfaceHolder); |
17 |
this.holder = surfaceHolder; |
18 |
}
|
19 |
}
|
Next, create a method named draw that draws the contents of the animated GIF. Let's break this method down:
- We first check if the
visiblevariable is set totrue. We only continue if it is. - Use the
SurfaceHolder'slockCanvasmethod to get aCanvasto draw on. - Draw a frame of the animated GIF on the
Canvasafter scaling and positioning it. - Once all the drawing is done, pass the
Canvasback to theSurfaceHolder. - Update the current frame of the animated GIF using the
Movieobject'ssetTimemethod. - Call the method again using the
handlerafter waiting forframeDurationmilliseconds.
The draw method is never called directly. It is always called using a Handler and a Runnable object. Therefore, let's make the Runnable object a field of the class and call it drawGIF.
Add the following code to the GIFWallpaperService class:
1 |
private Runnable drawGIF = new Runnable() { |
2 |
public void run() { |
3 |
draw(); |
4 |
}
|
5 |
};
|
6 |
|
7 |
private void draw() { |
8 |
if (visible) { |
9 |
Canvas canvas = holder.lockCanvas(); |
10 |
canvas.save(); |
11 |
// Adjust size and position so that
|
12 |
// the image looks good on your screen
|
13 |
canvas.scale(3f, 3f); |
14 |
movie.draw(canvas, -100, 0); |
15 |
canvas.restore(); |
16 |
holder.unlockCanvasAndPost(canvas); |
17 |
movie.setTime((int) (System.currentTimeMillis() % movie.duration())); |
18 |
|
19 |
handler.removeCallbacks(drawGIF); |
20 |
handler.postDelayed(drawGIF, frameDuration); |
21 |
}
|
22 |
}
|
The onVisibilityChanged method is automatically called whenever the visibility of the wallpaper changes. We need to override it and, based on the value of the visible argument, either start or stop drawGIF. The removeCallbacks method of the Handler is used to stop any pending drawGIF runs.
1 |
@Override
|
2 |
public void onVisibilityChanged(boolean visible) { |
3 |
this.visible = visible; |
4 |
if (visible) { |
5 |
handler.post(drawGIF); |
6 |
} else { |
7 |
handler.removeCallbacks(drawGIF); |
8 |
}
|
9 |
}
|
Finally, override the onDestroy method of the Engine to stop any pending drawGIF runs if the wallpaper is deactivated.
1 |
@Override
|
2 |
public void onDestroy() { |
3 |
super.onDestroy(); |
4 |
handler.removeCallbacks(drawGIF); |
5 |
}
|
7. Compile and Install
Your GIF live wallpaper is now ready. Compile it and install it on your Android device. Once installed, you should be able to find the wallpaper in the list of available wallpapers.
Most launchers give you an option to change the wallpaper after a long tap gesture. Alternatively, you can go to the display settings to change the wallpaper.



If the GIF looks too small or it isn't positioned correctly, then go back to the draw method and adjust the scale and position.
That's It! Now You Know How to Set a GIF As a Wallpaper on Android
You now know how to use an animated GIF to create a live wallpaper. Feel free to experiment with more GIFs. If you plan to publish your live wallpaper on Google Play, make sure you have the creator's permission to use the animated GIF commercially. Visit the Android Developer website to learn more about the WallpaperService class.
10+ Amazing Android Tutorials From Envato Tuts+
If you enjoyed our tutorial on how to set a GIF as a wallpaper on Android, you should checkout our other Android Tutorials from Envato Tuts+:


Android From Scratch: Building Your First Android Application

Nitish Kumar30 Jun 2022

Android From Scratch: How to Run Your Application on a Physical Device

Ashraff Hathibelagal11 Aug 2022

How to Create an Android App Without Coding

Ashraff Hathibelagal30 Jun 2022

10 Best Android App Templates With Maps Integration

Daniel Strongin28 Sep 2022

9 Best Android App Templates for Business

Franc Lucas07 May 2022

How to Run an Android App on an Emulator

Arooha Arif30 Sep 2021

How to Make Android Apps for Beginners

Arooha Arif12 Jun 2021

How to Learn Android App Development: Start With These Courses and Tutorials

Kyle Sloka-Frey25 Apr 2020

My First App: How to Create Your First Android App Step by Step

Ashraff Hathibelagal23 May 2023

25 Best Android App Mockup Generators

Franc Lucas18 Sep 2021



