Android Tutorial Android Live Wallpaper tutorial

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.

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:
B4X:
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), _
      300dip, 100dip, Typeface.DEFAULT_BOLD, 40, Colors.White, "LEFT")
   Engine.RefreshAll
End Sub
SS-2011-11-15_11.23.06.png


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

Manifest editor:
B4X:
AddApplicationText(
<service
        android:label="My Livewallpaper"
        android:name="anywheresoftware.b4a.objects.WallpaperInternalService"
        android:permission="android.permission.BIND_WALLPAPER"
        android:exported="true">
        <intent-filter >
            <action android:name="android.service.wallpaper.WallpaperService" />
        </intent-filter>
        <meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper" />
</service>
)
CreateResource(xml, wallpaper.xml,
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android"
  android:thumbnail="@drawable/icon"
/>
)

Updates

- v1.10 - fixes issues related to targetSdkVersion=34. Previous versions will not work.
- Example was updated. It is based on B4XPages. The XML resource is created in the manifest editor directly.

1728281678475.png


Copy the updated library to the internal libraries folder.
 

Attachments

  • WallpaperExample.zip
    18 KB · Views: 108
  • LiveWallpaper.zip
    8.9 KB · Views: 106
Last edited:

boten

Active Member
Licensed User
Longtime User
It is the same error (wallpaper.livepicker).
I unmarked the "do not overwrite..." so the manifest file will include reference to the "parm" activity - Yes it is there.

Then I marked the "do not overwrite..." and changed it as prescribed in the tutuorial (minsdkversion, service declarayion,...). and compiled & run again.

LWP does work, but pressing the "setting..." button cause the error.
Remember, the error does NOT occur when the settingsActivity is main (in wallpapwer.xml)

note: the zipping removed the readonly from strings.xml and wallpaper.xml)
 

Attachments

  • stam2.zip
    7.3 KB · Views: 455

FJS

Active Member
Licensed User
Longtime User
Mading live wallpaper with fluid animations

Good evening Erel,

Please, I would like to know two things:

1-How to created fluid movements with live wallpaper?
I used Engine.Canvas.DrawBitmapRotated(luz, Null, Engine.Rect, Degrees) each 100milisec but it doesnt look fluid...: confused:
there are little jumpings...

2-How to use animation library with live wallpaper?
Could you show us several examples?

Any clue and help will be wellcome

Thank you very much for your help and your support

Best regards
 
Last edited:

FJS

Active Member
Licensed User
Longtime User
Good afternoon Erel,

Regarding the first point, I have just found the problem, I put the variables as int, but it works with variables as double. Right now there is not jumpings!!
But What do you recomend to refresh? I mean, with 80miliseconds is enough?, it seems ok to obtain fluid moment, but I dont know if this way reduce the battery so much

On the other hand, it is a pety, maybe is possible use Open GL to create better applications?

Thank you for your help!!
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
100 ms wouldn't be very fluid. That's only a framerate of 10.

Try 33 ms.
 

FJS

Active Member
Licensed User
Longtime User
A error with the sources of texts and problems with rotating the tablet

Good evening everyone,

I have two questions:

First of all, I think that I have a problem with the sources of the texts...
This is the message:


"Compiling code. 0.02
Using existing AndroidManifest.xml.
(Project - Do Not Overwrite Manifest option is checked)
AndroidManifest-Example.xml file will be created instead.
Generating R file. 0.00
Compiling generated Java code. Error
B4A line: 107
Engine.Canvas.DrawText(sentence, X1,Y1,Typeface.STYLE_BOLD,size,Colors.black,\
javac 1.6.0_26
src\anywheresoftware\b4a\samples\fondovivob\wallpaperservice.java:195: inconvertible types
found : int
required: android.graphics.Typeface
_engine.getCanvas().DrawText(processBA,_sentence,_x1,_y1,(android.graphics.Typeface)(anywheresoftware.b4a.keywords.Common.Typeface.STYLE_BOLD),_size,anywheresoftware.b4a.keywords.Common.Colors.Black,BA.getEnumFromString(android.graphics.Paint.Align.class,"CENTER"));
^
1 error
"
then when I change my tablet from portrait to landscape the tablet says: aplication stops (what is happening?)

And on the other hand, I dont know how to dont allow the picture rotates when I rotate the tablet, and nether How to change the layout when I rotate the tablet

Thank you very much for your help, like always!!

Best regards
 

NeoTechni

Well-Known Member
Licensed User
Longtime User
How do we use the manifest editor to add the XML?
The first post should be altered to show it

I tried:

AddServiceText(WallpaperService,
android:label="PUTYOURWALLPAPERNAMEHERE"
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" />
)
that worked
 
Last edited:

NeoTechni

Well-Known Member
Licensed User
Longtime User
Yeah, I used the manifest editor on a known-working livewallpaper and it broke it. I manually edited the xml file myself and it worked again.

Please update the tutorial with directions on how to use the manifest editor
 

FJS

Active Member
Licensed User
Longtime User
Thank you very much, it works!


I have another question... how to save several parameter of a live screen configuration, to use it when the tablet restarts.
I have a problem with the configuration, all work but when I restart my table, the initial configuration appears again but no my last configuration..

Thank you!
 

FJS

Active Member
Licensed User
Longtime User
You need to save your config to a file, like a map or ini file.

Sorry... but how to do that in the code?, for example in the LiveWallpaperImage code, to save the imagen, so I will not need to select the imagen each time I restart the tablet, isnt?

PS: I dont have to much experience with the code...

Thank you!
 

walterf25

Expert
Licensed User
Longtime User
Live Wall Paper capability

Hello all is it possible to create a wall paper like this with this library?

If so can you guys maybe help me out, I need to make something like this!

thanks all
Walter
 
Top