Live wallpapers - are richer, animated, interactive backgrounds on Homescreen.
In this post I will describe step by step "How to Create LiveWallpaper " .
In this example I have created a LiveWallpaper in which a Fish is moving left to right.
The example is too simple and has been described in manner that you can easily understand.
Step 2: edit your manifest file add service and feature.
Step 3: create a service class which extends WallpaperService class.
Step 1: create a xml folder inside res folder.
inside xml folder create mywallpaper.xml file
<?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"
/>
here thumbnail is the image that will be shown in list of livewallpapers and description attribute is the description of your live wallpaper.
Also add following in your string.xml file inside values folder
<string name="wallpaper_description">Fish Aquarium</string>
Step 2: Edit your Manifest.xml
<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: create a service class which extends WallpaperService.
this class is the base class for all live wallpapers in the system. You must implement the
The
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
}
}
}
}
Run the application and set the Wallpaper.( Long Press on Screen ->Set Wallpaper -> HomeScreen -> Live Wallpapers -> select your Live wall paper)
In this post I will describe step by step "How to Create LiveWallpaper " .
The Android Development Tutorials blog contains Basic as well as Advanced android tutorials.Go to Android Development Tutorials to get list of all Android Tutorials.
Android Live Wallpaper Example
In this example I have created a LiveWallpaper in which a Fish is moving left to right.
The example is too simple and has been described in manner that you can easily understand.
How to create a Live Wallpaper
Step 1: create a xml file which will describe your wallpaper.Step 2: edit your manifest file add service and feature.
Step 3: create a service class which extends WallpaperService class.
Step 1: create a xml folder inside res folder.
inside xml folder create mywallpaper.xml file
<?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"
/>
here thumbnail is the image that will be shown in list of livewallpapers and description attribute is the description of your live wallpaper.
Also add following in your string.xml file inside values folder
<string name="wallpaper_description">Fish Aquarium</string>
Step 2: Edit your Manifest.xml
<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: create a service class which extends WallpaperService.
this class is the base class for all live wallpapers in the system. You must 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
}
}
}
}
Run the application and set the Wallpaper.( Long Press on Screen ->Set Wallpaper -> HomeScreen -> Live Wallpapers -> select your Live wall paper)
More Android Topics
New Advance Topics:Android ImageSwitcher Android TextSwitcher Android ViewFlipper
Android Gesture Detector Handling/Detecting Swap Events Gradient Drawable
Detecting Missed Calls Hide Title Bar GridView Animation
Beginning With Android
Android : Introduction Configuring Eclipse for Android Development
Creating Your First Android Project Understanding Android Manifest File of your android app
Advance Android Topics Customizing Android Views
Working With Layouts Working With Views
Understanding Layouts in Android Using Buttons and EditText in Android
Working with Linear Layout (With Example) Using CheckBoxes in Android
Nested Linear Layout (With Example) Using AutoCompleteTextView in Android Grid View
Relative Layout In Android ListView
Table Layout Android ProgressBar
Frame Layout(With Example) Customizing ProgressBar
Absolute Layout Customizing Radio Buttons
Grid Layout Customizing Checkboxes In Android
Android Components Dialogs In Android
Activity In Android Working With Alert Dialog
Activity Life Cycle Adding Radio Buttons In Dialog
Starting Activity For Result Adding Check Boxes In Dialog
Sending Data from One Activity to Other in Android Creating Customized Dialogs in Android
Returning Result from Activity Creating Dialog To Collect User Input
Android : Service DatePicker and TimePickerDialog
BroadcastReceiver Using TimePickerDialog and DatePickerDialog In android
Menus In Android ListView:
Creating Option Menu Populating ListView With DataBase
Creating Context Menu In Android Populating ListView with ArrayList
ListView with Custom Adapter
Toast Working With SMS
Customizing Toast In Android How to Send SMS in Android
Customizing the Display Time of Toast How To Receive SMS
Customizing Toast At Runtime Accessing Inbox In Android
Adding Image in Toast
Showing Toast for Longer Time
TelephonyManager Storage: Storing Data In Android
Using Telephony Manager In Android SharedPreferences In Android
Reading and Writing files to Internal Stoarage
Working With Incoming Calls DataBase
How To Handle Incoming Calls in Android Working With Database in Android
How to Forward an Incoming Call In Android Creating Table In Android
CALL States In Android Inserting, Deleting and Updating Records In Table in Android
Miscellaneous
Notifications In Android
How To Vibrate The Android Phone
Sending Email In Android
Opening a webpage In Browser
How to Access PhoneBook In Android
Prompt User Input with an AlertDialog
No comments:
Post a Comment