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.
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.
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
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
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() {
}
}
}
@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.public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
setTouchEventsEnabled(true);
}
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);
}
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 mmPixelpublic void onOffsetsChanged(float xOffset, float yOffset,
float xStep, float yStep, int xPixels, int yPixels) {
this.mmPixel=xPixels;
drawFrame();
}
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);
}
}
}
}
If there is some error on my code or you think better then feel free to comments.
thanks............ and remember me in your prayers
1 Comments
This comment has been removed by the author.
ReplyDelete