Android Tutorial Android Live Wallpaper tutorial

Discussion in 'Tutorials & Examples' started by Erel, Nov 15, 2011.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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
    [​IMG]

    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
    [​IMG]

    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 add the inner service declaration to the manifest editor (Project -> Manifest Editor):
    Code:
    AddApplicationText(
    <!-- ******** 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>
    )
    It is also recommended to change minSdkVersion to 7 (at the beginning of the manifest editor code).

    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.WallpaperInternalService. 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.

    Note that it is recommended to test live wallpapers in Release mode (the wallpaper will fail in debug mode when the process is started by the OS).
     

    Attached Files:

    Last edited: Mar 6, 2014
    anOparator likes this.
  2. susu

    susu Well-Known Member Licensed User

    Great! It seems not so difficult. Thanks Erel.
     
  3. susu

    susu Well-Known Member Licensed User

    I got error when compiling LiveWallpaperImage example:

     
  4. susu

    susu Well-Known Member Licensed User

    OK, fixed it
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    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.
     
  6. Gigatron

    Gigatron Member Licensed User

    Thank you EREL

    Like i said when i discovered B4A ...... you are a MASTER :)
     
  7. Highwinder

    Highwinder Active Member Licensed User

    I'd like to second this sentiment. Thanks again, Erel, you've made my week! :sign0060::icon_clap:

    - Highwinder
     
  8. Gigatron

    Gigatron Member Licensed User

    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 :):icon_clap::sign0098:
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    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.
     
  10. Highwinder

    Highwinder Active Member Licensed User

    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
     
  11. Gigatron

    Gigatron Member Licensed User

    Master EREL ; I do not want to bother you, but is it possible to add a preference in a live wallpaper ?

    In my example i have done a rotating bouncing and zooming text, can the user set he's text when he choise the live wallpaper ?

    Thanks in advance .
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Yes, it is possible.
    You have two options:
    - You can update the live wallpaper from a regular activity as done in LiveWallpaperImage.
    - You can also add a special settings activity and then the user will have a "settings" button in the preview screen.
    You should edit wallpaper.xml and add the full activity name:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <wallpaper xmlns:android=
    "http://schemas.android.com/apk/res/android"
      android:thumbnail=
    "@drawable/icon"
      android:settingsActivity=
    "anywheresoftware.b4a.samples.livewallpaperimage.main"
    />
    Don't forget to set wallpaper.xml as readonly.
     
  13. Gigatron

    Gigatron Member Licensed User

    OK..... :) User Set His text Now...

    Erel you know ? I can not say enough... You are really a MASTER :))

    I have drop java learning since livewallpaper lib out :)))))

    Thank you again...

    Code:
    Sub EditText1_EnterPressed
    wallpaperservice.txt=edittext1.Text       <-- This 
    part send parameter to txt variable on wallpaperservice module.
       
    'CallSub(WallpaperService, "textchanged")  ' Sous routine textchanged ... pas besoin ici 
       activity.Finish
    End Sub
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    You must also save the text in a file, and read this file when the service is created. See LivewallpapaerImage example.
     
  15. XverhelstX

    XverhelstX Well-Known Member Licensed User

    Great Erel! Really amazing.

    For my app, i'm going to need multiple LiveWallpapers, is it possible that we can have more than one LiveWallpaper in 1 app? :s
    Do I have to modify things in the Android Manifest then?

    I also updated my LiveWallpaper where users are able to load gif files and display them as their animated wallpaper.

    http://www.basic4ppc.com/forum/addi.../11636-livewallpaper-library-3.html#post71183

    Thanks!
    Tomas
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    You can only have one wallpaper service in your application. However you can configure it to show completely different wallpapers.

    See post #12 for the formal way to add a settings activity.
     
  17. susu

    susu Well-Known Member Licensed User

    Can I add Panel, ImageView... to LiveWallpaper?
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    You can only draw on LiveWallpapers.
     
  19. Highwinder

    Highwinder Active Member Licensed User

    Java


    DITTO. Live Wallpapers (or the previous lack thereof in B4A) are what had me sinking far enough into the depths of despair that I was in the process of giving up on B4A and loading up Eclipse ("Eclipse", as in never seeing daylight) and pulling out all my unread Java books and CBT, grumbling my way into going the "crash course" route so I could produce LWPs.

    This new Live Wallpaper library has totally rescued me from that ridiculous curly braced hell (no offense, but I absolutely HATE Java and anything resembling C++ :BangHead:).

    Erel - again, my most profound thanks. :icon_clap:

    - Highwinder

    PS: I knew this would be a popular library. This is the library that will ensure B4A sells well.
     
  20. NeoTechni

    NeoTechni Well-Known Member Licensed User

    I too was considering loading up that horrible eclipse program to do a LWP. I have to say, this library is extremely easy. I can't wait till the program manages all the XML stuff on it's own though.
     
Loading...