Android Live Wallpaper Example
In Android Live Wallpaper Creation we have to use WallpaperService class.
WallpaperService :
A wallpaper service is responsible for showing a live
wallpaper behind applications that would like to sit on top of it. This service
object itself does very little -- its only purpose is to generate instances of
But WallpaperService have several instance to maintain each instance We use Engine class.
WallpaperService.Engine
as needed. Implementing a wallpaper thus involves subclassing from this,
subclassing an Engine implementation, and implementing onCreateEngine()
to return a new instance of your engine.But WallpaperService have several instance to maintain each instance We use Engine class.
WallpaperService.Engine :
The actual implementation of a wallpaper. A wallpaper service may have multiple
instances running (for example as a real wallpaper and as a preview), each of
which is represented by its own Engine instance. You must implement
onCreateEngine()
to return your concrete Engine implementation.
Now Create an Android project name Android-Live-Wallpaper. Your application directory is shown below.
put two image named background.png and fish.png in drawable folder.
Now Source Code for Activity java file:
package com.example.livewallpaper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.SurfaceHolder;
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,150, 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
}
}
}
}
Create a xml file named mywallpaper.xml with in xml folder under resource folder.
Code for mywallpaper.xml file:-
xmlns:android="http://schemas.android.com/apk/res/android"
android:thumbnail="@drawable/fish"
android:description="@string/wallpaper_description"
/>
code for string.xml file that present in style folder under res:
Code for manifest.xml file
package="com.example.livewallpaper"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="17" />
android:name="android.software.live_wallpaper"
android:required="true" >
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="LiveWallpaperService"
android:enabled="true"
android:label="Wallpaper Example "
android:permission="android.permission.BIND_WALLPAPER" >
android:name="android.service.wallpaper"
android:resource="@xml/mywallpaper" >
Now your Application is ready for test. For test on Emulator you hold right click on home screen. Then a dialogue menu appear then choose wallpaper.
After ward new dialogue menu appear in which you select live wallpaper.
Now you choose your own live paper.
Now Set As wallpaper after clicking set wallpaper.......
You all learner must comment How is it?
thanks... Keep learning with Shivendra (Android Trainer at APSMIND Technology...).
No comments:
Post a Comment