android wallpapers: making it live

android wallpapers: making it live

 Anil Tiwari

View (768)

What are live wallpapers?

Creating beautiful and an interesting live wallpaper in android is a good practice and looks very cool. Live Wallpapers looks richer, animated , interactive backgrounds for the Android home screen. A live wallpaper is similar to other Android applications most of the functionality is same.

In this article, I will describe how to create live wallpaper. Step-by-step, we will create our own live wallpaper.

Example:-

In this live wallpaper logo is moving from left to right. This example is been described in a very simple way so that u can easily understand it.

 

 

 

 

 

 

 

 

Follow these steps:-

Step 1. xml file:- In order to create a live walpaper first of all you need to create a xml file which will describe your walpaper. Inside xml folder create a file named mywalpaper.xml

  

<?xml version="1.0" encoding="UTF-8"?>
            <wallpaper 
                     xmlns:android="http://schemas.android.com/apk/res/android"
                     android:thumbnail="@drawable/fish"
                     android:description="@string/wallpaper_description"  
            />

 

Now, here thumbnail is the image that will be shown in list of livewallpapers and description attribute is the description of your live wallpaper.

Also you need to add following in your string.xml file inside the values folder
<string name="wallpaper_description">Fish Aquarium</string> 

Step 2. manifest.xml file:- 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.livewallpapertutorial"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

     <uses-feature
        android:name="android.software.live_wallpaper"
        android:required="true" >
    </uses-feature>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service
            android:name="LiveWallpaperService"
            android:enabled="true"
            android:label="Wallpaper Example "
            android:permission="android.permission.BIND_WALLPAPER" >
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" >
                </action>
            </intent-filter>

            <meta-data
                android:name="android.service.wallpaper"
                android:resource="@xml/mywallpaper" >
            </meta-data>
        </service>

    </application>

</manifest>

Step 3. java file:- Create a class named service which extends WallpaperService.

This class will act as the base class for all live wallpapers in the system. You have to implement the  onCreateEngine()   method .
The Engine class defines the life cycle methods, as for example onCreate(), onSurfaceCreated(),onVisibilityChanged(), onOffsetsChanged(), onTouchEvent() and onCommand().


public class LiveWallpaperService extends WallpaperService 
{
    int x,y;
                
    public void onCreate(){
        super.onCreate();
	}

	public void onDestroy(){
		super.onDestroy();
	}

	public Engine onCreateEngine(){
		return new MyWallpaperEngine();
	}

	class MyWallpaperEngine extends Engine{

		private final Handler handler = new Handler();
		private final Runnable drawRunner = new Runnable() {
			@Override
			public void run() {
				draw();
			}
		};
		private boolean visible = true;
		public Bitmap image1,backgroundImage;

		MyWallpaperEngine() 
		{
			 // get the fish and background image references
			image1 = BitmapFactory.decodeResource(getResources(),R.drawable.fish);
			backgroundImage = BitmapFactory.decodeResource(getResources(),R.drawable.background);
			x=-130; // initialize x position 
			y=200;  // initialize y position 
				
		}


		public void onCreate(SurfaceHolder surfaceHolder)
		{
			super.onCreate(surfaceHolder);
		}

		@Override
		public void onVisibilityChanged(boolean visible)
		{
			this.visible = visible;
			// if screen wallpaper is visible then draw the image otherwise do not draw
			if (visible) 
			{
				handler.post(drawRunner);
			}
			else 
			{
				handler.removeCallbacks(drawRunner);
			}
		}

		@Override
		public void onSurfaceDestroyed(SurfaceHolder holder)
		{
			super.onSurfaceDestroyed(holder);
			this.visible = false;
			handler.removeCallbacks(drawRunner);
		}

		public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) 
		{
			draw();
		}

		void draw() 
		{
			final SurfaceHolder holder = getSurfaceHolder();

			Canvas c = null;
			try 
			{
				c = holder.lockCanvas();
				// clear the canvas
				c.drawColor(Color.BLACK);
				if (c != null)
				{
					// draw the background image
					c.drawBitmap(backgroundImage, 0, 0, null);
					// draw the fish
					c.drawBitmap(image1, x,y, null);
					// get the width of canvas
					int width=c.getWidth();
					
					// if x crosses the width means  x has reached to right edge
					if(x>width+100)
					{   
						// assign initial value to start with
						x=-130;
					}
					// change the x position/value by 1 pixel
					x=x+1;
				}
			 }
			finally{
				if (c != null)
				holder.unlockCanvasAndPost(c);
			}

			handler.removeCallbacks(drawRunner);
			if (visible) {
				handler.postDelayed(drawRunner, 10); // delay 10 mileseconds
			}    

		}
	}
}



Step 4. Run your project:- Now You can run this application and set the Wallpaper.( Long Press on Screen ->Set Wallpaper -> HomeScreen -> Live Wallpapers -> select your Live wall paper).

 



 

About the author

author

I am loving nature

Most Read

The new kid in the block: Android Noughat

Google has been following a similar pattern with its Android naming process....

Watch Security Cams in the open using Google dorks

Are you aware that there is no such thing on the internet as, “Hiding in...

How to create Splash Screen in Android

Android splash screen are normally used to show user some kind of progress...

Top 10 reasons why you should learn Java

If you're a programmer now, or you want to become one, you've probably...

Reasons that prove BIG DATA is the next Big thing!

According to analysts, the big data market has been predicted to be USD $50...
X
Register Here
 
By signing up , you agree to our Terms of Service and Privacy Policy.
 Login With
Facebook
Facebook
Google
Google
X
Log In Here
 
Remember me Forgot password?
 Login With
Facebook
Facebook
Google
Google
X
Reset Password
 
X

Confirmation Message

Yes No
X
Forgot Password
 
Do you want to Login ?