Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Android Live Wallpaper tutorial

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-15-2011, 08:59 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Android Live Wallpaper tutorial

Using the new LiveWallpaper library you can now create your own live wallpapers. The user can set live wallpapers by long pressing on the home screen and choosing Wallpapers - Live Wallpapers.

Creating a live wallpaper is not too difficult.

A service is responsible for handling the wallpaper events and drawing the wallpaper.

There can be several instances of the same wallpaper at the same time. For example the user can set your wallpaper as the home screen wallpaper and also see a demo of your wallpaper in the wallpaper preview dialog.

LiveWallpaper library contains two objects: LWManager and LWEngine.
LWManager is responsible for raising the events.
The first parameter of each event is of type LWEngine. LWEngine represents a specific wallpaper instance.
LWEngine includes a Canvas property which is used to draw the wallpaper.
When you finish drawing you must call LWEngine.Refresh or LWEngine.RefreshAll. Otherwise the drawing will not appear.

The following example draws a blue background. Red circles are drawn when the user touches the screen:
Code:
Sub Process_Globals
    
Dim lwm As LWManager
End Sub

Sub Service_Create
    lwm.Initialize(
"lwm"True)
End Sub

Sub LWM_SizeChanged (Engine As LWEngine)
    Engine.Canvas.DrawColor(
Colors.Blue)
    Engine.RefreshAll
End Sub

Sub LWM_Touch (Engine As LWEngine, Action As Int, X As Float, Y As Float)
    Engine.Canvas.DrawCircle(X, Y, 
20dipColors.Red, True0)
    Engine.Rect.Left = X - 
20dip
    Engine.Rect.Right = X + 
20dip
    Engine.Rect.Top = Y - 
20dip
    Engine.Rect.Bottom = Y + 
20dip
    Engine.Refresh(Engine.Rect)
End Sub


As there could be several different engines, it is convenient to work with the local engine.
LWEngine includes a Tag property which you can use to store data specific to that engine.

LWManager includes an internal timer that you can use if you need to do animations. The Tick event will only be raised for visible wallpapers. This is important to conserve battery.

For example the following code draws the time on the wallpaper:
Code:
Sub Process_Globals
    
Dim lwm As LWManager
End Sub

Sub Service_Create
    lwm.Initialize(
"lwm"True)
    lwm.StartTicking(
1000'tick every second
End Sub

Sub LWM_Tick (Engine As LWEngine)
    Engine.Canvas.DrawColor(
Colors.Black) 'Erase the background
    Engine.Canvas.DrawText(DateTime.Time(DateTime.Now), _ 
        
300dip100dipTypeface.DEFAULT_BOLD, 40Colors.White, "LEFT")
    Engine.RefreshAll
End Sub


Offsets
On most devices the wallpaper virtual size is wider than a single screen. When the user moves to a different screen the offset changes.
You can use the OffsetChanged event to handle those changes.
LWEngine.FullWallpaperWidth / Height return the full size of the wallpaper.
LWEngine.CurrentOffsetX / Y return the current position.
Note that the wallpaper scrolls less than the foreground layer with the icons.

The LiveWallpaperImage demonstrates how to use those properties to display an image over the full wallpaper.

LWManager events

SizeChanged - Raised when the engine is first ready and when the screen size changes (for example when the orientation changes).
VisibilityChanged - Raised when a wallpaper becomes visible or invisible.
Touch - Raised when the user touches the wallpaper.
Tick - The internal timer tick event.
OffsetChanged - Raised when the wallpaper offsets change.
EngineDestroyed - Raised when an engine is destroyed.

Configuration
Live wallpapers require some configuration.
1. You should modify the manifest file (Objects\AndroidManifest.xml) with the following changes (without any changes except for android:label which will appear in the live wallpapers list):
Code:
    <!-- ******** Add the internal service declaration (you can change android:label)  ******* -->
<service
        android:
label="My Livewallpaper" 
        android:name=
"anywheresoftware.b4a.objects.WallpaperInternalService"
        android:permission=
"android.permission.BIND_WALLPAPER">
        <
intent-filter>
            <action android:name=
"android.service.wallpaper.WallpaperService" />
        </
intent-filter>
        <meta-data android:name=
"android.service.wallpaper" android:resource="@xml/wallpaper" />
</service>
<!-- 
End of internal service declaration -->
You should also change minSdkVersion to 7 (Android 2.1).
The manifest should look like:
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
      package=
"anywheresoftware.b4a.samples.livewallpaperball"
      android:versionCode=
"1"
      android:versionName=
""
      android:installLocation=
"internalOnly">
      <uses-sdk android:minSdkVersion=
"7" /> <!-- **** change minimum version to 7  **** -->
      <supports-screens
          android:largeScreens=
"true"
          android:normalScreens=
"true"
          android:smallScreens=
"true"
          android:anyDensity=
"true"/>
    <application android:icon=
"@drawable/icon" android:label="LiveWallpaper Ball">
        
        <activity android:windowSoftInputMode=
"stateHidden" android:launchMode="singleTop" android:name=".main"
                  android:
label="LiveWallpaper Ball" android:screenOrientation="unspecified">
            <
intent-filter>
                <action android:name=
"android.intent.action.MAIN" />
                <category android:name=
"android.intent.category.LAUNCHER" />
            </
intent-filter>
        </activity>
        <service android:name=
"wallpaperservice"></service>
<receiver android:name=
"wallpaperservice$wallpaperservice_BR">
</receiver>

    <!-- ******** Add the internal service declaration (you can change android:
label)  ******* -->
    <service
            android:
label="My Livewallpaper" 
            android:name=
"anywheresoftware.b4a.objects.WallpaperInternalService"
            android:permission=
"android.permission.BIND_WALLPAPER">
            <
intent-filter>
                <action android:name=
"android.service.wallpaper.WallpaperService" />
            </
intent-filter>
            <meta-data android:name=
"android.service.wallpaper" android:resource="@xml/wallpaper" />
    </service>
    <!-- 
End of internal service declaration -->
    
    </application>
    

</manifest>
Don't forget to check Project - Do Not Overwite Manifest File
For more information about manual changes to the manifest file: PreferenceActivity tutorial

2. Add wallpaper.xml to Objects\res\xml folder and set it to be readonly (otherwise it will be deleted during compilation).
You can find this file in both attached examples.

3. Your service must be named WallpaperService. Note that there is an additional service inside the library named anywheresoftware.b4a.objects.WallpaperInternalServ ice. The internal service connects to your service.

Examples
LiveWallpaperImage - The user can select an image from the activity. This image is set as the wallpaper. When the user changes the wallpaper offset the image is updated accordingly.

LiveWallpaperBall - A bouncing smiley. Clicking on the smiley will change its direction.
Before running the examples you need to set Object\res\xml\wallpaper.xml to be readonly. Otherwise it will be deleted.

The library is available here: http://www.basic4ppc.com/forum/addit...wallpaper.html
Attached Files
File Type: zip LiveWallpaperImage.zip (7.5 KB, 247 views)
File Type: zip LiveWallpaperBall.zip (7.9 KB, 198 views)
Reply With Quote
  #2 (permalink)  
Old 11-15-2011, 09:12 AM
Basic4ppc Expert
 
Join Date: May 2008
Posts: 550
Default

Great! It seems not so difficult. Thanks Erel.
__________________
I'm not good at English, please understand. Thank you.
Reply With Quote
  #3 (permalink)  
Old 11-15-2011, 09:34 AM
Basic4ppc Expert
 
Join Date: May 2008
Posts: 550
Default

I got error when compiling LiveWallpaperImage example:

Quote:
Generating R file. Error
AndroidManifest.xml:33: error: Error: No resource found that matches the given name (at 'resource' with value '@xml/wallpaper').
__________________
I'm not good at English, please understand. Thank you.
Reply With Quote
  #4 (permalink)  
Old 11-15-2011, 09:38 AM
Basic4ppc Expert
 
Join Date: May 2008
Posts: 550
Default

OK, fixed it
Quote:
Add wallpaper.xml to Objects\res\xml folder and set it to be readonly
__________________
I'm not good at English, please understand. Thank you.
Reply With Quote
  #5 (permalink)  
Old 11-15-2011, 09:39 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

I see, the readonly property of wallpaper.xml is removed when it is zipped. I've added a comment about it in the first post.

You will need to unzip the project again and set wallpaper.xml to be readonly.
Reply With Quote
  #6 (permalink)  
Old 11-15-2011, 12:24 PM
Knows the basics
 
Join Date: Aug 2011
Posts: 66
Default

Thank you EREL

Like i said when i discovered B4A ...... you are a MASTER
Reply With Quote
  #7 (permalink)  
Old 11-15-2011, 03:23 PM
Knows the basics
 
Join Date: Aug 2011
Posts: 71
Default

Quote:
Originally Posted by Gigatron View Post
Thank you EREL

Like i said when i discovered B4A ...... you are a MASTER
I'd like to second this sentiment. Thanks again, Erel, you've made my week!

- Highwinder
Reply With Quote
  #8 (permalink)  
Old 11-15-2011, 04:01 PM
Knows the basics
 
Join Date: Aug 2011
Posts: 66
Default

This library is realy Amazing ..

10 Minutes to adapt my Starfield
10 minutes to make parallax 7 plane Starfield Livewallpaper running at 60fps ...

I will post it to Android market

Retanks to Master Erel
Reply With Quote
  #9 (permalink)  
Old 11-15-2011, 04:27 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Thank you. It is great to see that you find it useful.

As I wrote in some other thread, this library was pretty difficult to develop as it required a different design than other libraries.
Reply With Quote
  #10 (permalink)  
Old 11-15-2011, 06:57 PM
Knows the basics
 
Join Date: Aug 2011
Posts: 71
Default

Quote:
Originally Posted by Erel View Post
Thank you. It is great to see that you find it useful.

As I wrote in some other thread, this library was pretty difficult to develop as it required a different design than other libraries.
I think you'll find that it was worth it. Once the word gets out that B4A can finally do real live wallpapers, I'm willing to bet that sales jump. I'm very confident that this will be a very popular library.

- Highwinder
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Android FTP tutorial Erel Basic4android Getting started & Tutorials 104 Yesterday 05:51 PM
First Live Wallpaper - Bouncing smiley Erel Basic4android Updates and Questions 5 04-01-2012 04:34 PM
WISH: Live wallpaper creation Jim Brown Bugs & wishlist 8 11-15-2011 09:11 AM
Live Wallpaper with a purpose mccorkle Bugs & wishlist 1 08-16-2011 05:31 AM
Live Long, Live Prosper, Be Happier!!!! Cableguy Chit Chat 7 08-21-2009 07:40 AM


All times are GMT. The time now is 10:01 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0