Android Live Wallpaper

Android extend new excitement to new API. Android API 2.1 and onward support Live wallpapers feather. Live Wallpaper consists some extra function when we set it for background of Smart Phone. These are animated wallpapers which change state by touching screen and moving finger on screen.
In this tutorial we Developed Live Wallpaper Application.
Lets go to Start ............
Live Wallpaper For Android
Live Wallpaper is the shining feature of android mobile. Where you can animated wallpaper , change it with time , weahter, sensor moments , touch event and much more as you think.
Requirments
Its required Andriod API 7 (2.1 Froyo) and latest API of android . before Android 2.1 can not support touch event and live wallpaper feathures. So you must care of this requirments.
Lets Start
First of all Create new Andoird project Activity . OR delete Activity from Manifest file.

<br /> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.livewallpaer" android:versionCode="1" android:versionName="1.0" android:installLocation="auto"> <br /> <application android:icon="@drawable/ic_action_search" android:label="@string/app_name" android:theme="@style/AppTheme" </application><br /> <br /> </manifest><br />

Add Service to Manifest file and it name must be your java class which extend WallpaperService class. and remaining Sevice code as it as. Only in the meta data we add xml resource file wallpaper.xml which are the description file for wallpaper.

<br /> <service android:label="@string/app_name" android:name=".LiveWallpaper" android:enabled="true" android:permission="android.permission.BIND_WALLPAPER" android:icon="@drawable/afnan" ><br /> <intent-filter><br /> <action android:name="android.service.wallpaper.WallpaperService" /><br /> </intent-filter><br /> <meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper" /> <br /> </service><br />


Now Right Click of res Folder and add new Folder xml. Now right click on xml folder and add new file wallpaper.xml file. code is below
<br /> <!--xml version="1.0" encoding="utf-8"?--><br /> <wallpaper xmlns:android="http://schemas.android.com/apk/res/android" android:thumbnail="@drawable/android" android:description="@string/wallpaper_description" /><br />

Here we mention icon and Description for live wallpaper . Description are stored in String.xml file and icon are in Drawable folder.

Now open res folder click on value folder and open string.xml file . which code are below
<br /> <resources><br /> <br /> <string name="app_name">Live Wallpaer</string><br /> <string name="hello_world">Hello world!</string><br /> <string name="menu_settings">Settings</string><br /> <string name="title_activity_live_wallpaper">Live Wallpaper</string><br /> <string name="wallpaper_description">Parallax Scrolling</string><br /> <br /> </resources><br />

Working in JAVA Classes
The class which extend from activity, Now extend it from WallpaperService . Its return Engine Now you retune our inner class object which are extended from Engine class.
Here is the template of JAVA file
public class LiveWallpaper extends WallpaperService {

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

private class MyWallpaperEngine extends Engine {

public MyWallpaperEngine() {


}
}
}

We create Base LiveWallpaper Class and extend it from WallpaperServiece Which return whole Engine of Wallpaper. Now we add the object of my engine inner class NyWallpaperEngine which are extended from Engine class.
Now you run your appliction if it successfully install than we start to work on MyWallpaper class methods (onOffsetChange(), onSurfaceChange(), onTouchEvent() … etc ) to add rich feature to Complete my Live Wallpaper Projects.


Now First of All I override the onCreate() method.
@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);

setTouchEventsEnabled(true);


}
In this method we only enable Touch Event of Wallpaper. When your Touch the Wallpaper it call touch method where your customized task on touch motion and drag event.

Now In Wallpaper we draw images and different geometrical shapes .So we add method DrawFrame() . where we to all this. Here simply we darw a circle
public void drawFrame() {

final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
// draw here
c.drawCircle(20,20,10,null);
}
} finally {
if (c != null) holder.unlockCanvasAndPost(c);
}


Now we override the method from where we control all of my wallpaper . I my wallpaper is Double in size of Android Phone Size. Here we Add Parallax Scrolling to my wallpaper to change with changing of virtual screen of android phone
@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xStep, float yStep, int xPixels, int yPixels) {

this.mmPixel=xPixels;
drawFrame();

}
In local variable mmPixel we store the pixel value which are change with android finger moments . and set X-co ordinate of my wallpaper to mmPixel

c.drawBitmap(img,mmPixel,0,null);


I suggest that you are able to develop Live Wallpaper now i add my Live wallpaper Full Java Code.


import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class LiveWallpaper extends WallpaperService {
private final Handler mHandler = new Handler();

@Override
public void onCreate() {
super.onCreate();
}

@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public Engine onCreateEngine() {
// TODO Auto-generated method stub
return new WallEngine();
}
class MyLiveWallpaper extends Engine
{
private final Runnable mDrawCube = new Runnable() {
public void run() {

drawFrame();

}
};
private final Paint paint = new Paint();
private float mOffset;
private float mTouchX = -1;
private float mTouchY = -1;
private long mStartTime;
private float mCenterX;
private float mCenterY;
private int width;
private int height;
private float mmOffset;
private float mmPixel;
private boolean mVisible;
private Bitmap img;


public WallEngine() {
// TODO Auto-generated constructor stub
img= Bitmap.BitmapFactory.decodeResource(getResources(), R.drawable.new_Wally);


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

setTouchEventsEnabled(true);


}

@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mDrawCube);
}

@Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible) {
drawFrame();
} else {
mHandler.removeCallbacks(mDrawCube);
}
}
// set width of wallpaper double to android phone width in onSurfaceChange()
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format,
int width, int height)
{
super.onSurfaceChanged(holder, format, width,height);
this.width= width;
this.height = height;
bm= Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.new_vally), this.width*2, this.height, true);
drawFrame();

}

@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);

}

@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(mDrawCube);}

@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xStep, float yStep, int xPixels, int yPixels) {

this.mmPixel=xPixels;
drawFrame();

}

/*
* Store the position of the touch event so we can use it for drawing later
*/
@Override
public void onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
mTouchX = event.getX();
mTouchY = event.getY();

} else {
mTouchX = -1;
mTouchY = -1;
} super.onTouchEvent(event);


}
void drawFrame() {
final SurfaceHolder holder = getSurfaceHolder();

Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
c.drawBitmap(img,mmPixel,0,paint);
}
} finally {
if (c != null) holder.unlockCanvasAndPost(c);
}

// Reschedule the next redraw
mHandler.removeCallbacks(mDrawCube);
if (mVisible) {
mHandler.postDelayed(mDrawCube, 1000 /25);
}
}


}
}
Now Enjoy to Develope Live Wallpaer...
If there is some error on my code or you think better then feel free to comments.
thanks............ and remember me in your prayers

Post a Comment

1 Comments