Android dynamic wallpaper creation tutorial

Source: Internet
Author: User

Dynamic wallpaper is a new feature in Android 2.1. Dynamic wallpapers can be added to the Android desktop with interactive animated background effects. In this tutorial, we will teach you how to create an interactive dynamic wallpaper.

Dynamic wallpaper is an Android application, including a service (WallpaperService ). This service must contain a WallpaperService. Engine ). This engine is a bridge between users, desktops, and systems. It can also draw desktop wallpapers.

First, you must create a WallpaperService class from the internal Engine class. The service must be declared as "android. service. wallpaper. WallpaperService" in AndroidManifest. xml so that it can be recognized as a live wallpaper by mobile phones. In addition, you must add the permission "android. permission. BIND_WALLPAPER" to the service configuration:

 

<Service
Android: name = "LiveWallpaperService"
Android: enabled = "true"
Android: icon = "@ drawable/icon"
Android: label = "@ string/app_name"
Android: permission = "android. permission. BIND_WALLPAPER">

<Intent-filter android: priority = "1">
<Action android: name = "android. service. wallpaper. WallpaperService"/>
</Intent-filter>
<Meta-data
Android: name = "android. service. wallpaper"
Android: resource = "@ xml/wallpaper"/>

</Service>

 

 

Create an XML file and place it in the application directory/Res/xml/. It is used to describe your dynamic wallpaper.

 

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<Wallpaper
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: thumbnail = "@ drawable/thumbnail"
Android: description = "@ string/description"
Android: settingsActivity = "PreferenceActivity"/>

 

Create an xml property FileAttrs. xmlThe Code is as follows:

<Declare-styleable name = "Wallpaper">
<! -- Component name of an activity that allows the user to modify
The current settings for this wallpaper. -->
<Attr name = "settingsActivity"/>
 
<! -- Reference to a wallpaper's thumbnail bitmap. -->

<Attr name = "thumbnail" format = "reference"/>
 
<! -- Name of the author of this component, e.g. Google. -->
<Attr name = "author" format = "reference"/>

 
<! -- Short description of the component's purpose or behavior. -->
<Attr name = "description"/>
</Declare-styleable>

 

The dynamic wallpaper Service Code is as follows:

 

Package net. androgames. blog. sample. livewallpaper;
 
Import android. content. SharedPreferences;
Import android. service. wallpaper. WallpaperService;
Import android. view. MotionEvent;
Import android. view. SurfaceHolder;
 
/**
* Android Live Wallpaper Archetype
* @ Author antoine vianey
* Under GPL v3: http://www.gnu.org/licenses/gpl-3.0.html
*/
Public class LiveWallpaperService extends WallpaperService {
 
@ Override
Public Engine onCreateEngine (){
Return new SampleEngine ();
}
 
@ Override
Public void onCreate (){
Super. onCreate ();
}
 
@ Override
Public void onDestroy (){
Super. onDestroy ();
}
 
Public class SampleEngine extends Engine {
 
Private LiveWallpaperPainting painting;
 
SampleEngine (){
SurfaceHolder holder = getSurfaceHolder ();
Painting = new LiveWallpaperPainting (holder,
GetApplicationContext ());
}
 
@ Override
Public void onCreate (SurfaceHolder surfaceHolder ){
Super. onCreate (surfaceHolder );
// Register listeners and callbacks here
SetTouchEventsEnabled (true );
}
 
@ Override
Public void onDestroy (){
Super. onDestroy ();
// Remove listeners and callbacks here
Painting. stopPainting ();
}
 
@ Override
Public void onVisibilityChanged (boolean visible ){
If (visible ){
// Register listeners and callbacks here
Painting. resumePainting ();
} Else {
// Remove listeners and callbacks here
Painting. pausePainting ();
}
}
 
@ Override
Public void onSurfaceChanged (SurfaceHolder holder, int format,
Int width, int height ){
Super. onSurfaceChanged (holder, format, width, height );
Painting. setSurfaceSize (width, height );
}
 
@ Override
Public void onSurfaceCreated (SurfaceHolder holder ){
Super. onSurfaceCreated (holder );
// Start painting
Painting. start ();
}
 
@ Override
Public void onSurfaceDestroyed (SurfaceHolder holder ){
Super. onSurfaceDestroyed (holder );
Boolean retry = true;
Painting. stopPainting ();
While (retry ){
Try {
Painting. join ();
Retry = false;
} Catch (InterruptedException e ){}
}
}
 
@ Override
Public void onOffsetsChanged (float xOffset, float yOffset,
Float xStep, float yStep, int xPixels, int yPixels ){
}
 
@ Override
Public void onTouchEvent (MotionEvent event ){
Super. onTouchEvent (event );
Painting. doTouchEvent (event );
}
 
}
 
}

 

When the display, status, or size of the wallpaper changesOnCreate,OnDestroy,OnVisibilityChanged,OnSurfaceChanged,OnSurfaceCreatedAndOnSurfaceDestroyedMethod. With these methods, dynamic wallpapers can display animated effects. By settingSetTouchEventsEnabled (true ),And callOnTouchEvent (MotionEvent event)To activate the touch event.

We also use a separate painting thread when painting the wallpaper.

 

Package net. androgames. blog. sample. livewallpaper;
 
Import android. content. Context;
Import android. graphics. Canvas;
Import android. view. MotionEvent;
Import android. view. SurfaceHolder;
 
/**
* Android Live Wallpaper painting thread Archetype
* @ Author antoine vianey
* GPL v3: http://www.gnu.org/licenses/gpl-3.0.html
*/
Public class LiveWallpaperPainting extends Thread {
 
/** Reference to the View and the context */
Private SurfaceHolder surfaceHolder;
Private Context context;
 
/** State */
Private boolean wait;
Private boolean run;
 
/** Dimensions */
Private int width;
Private int height;
 
/** Time tracking */
Private long previousTime;
Private long currentTime;
 
Public LiveWallpaperPainting (SurfaceHolder surfaceHolder,
Context context ){
// Keep a reference of the context and the surface
// The context is needed if you want to inflate
// Some resources from your livewallpaper. apk
This. surfaceHolder = surfaceHolder;
This. context = context;
// Don't animate until surface is created and displayed
This. wait = true;
}
 
/**
* Pauses the live wallpaper animation
*/
Public void pausePainting (){
This. wait = true;
Synchronized (this ){
This. Policy ();
}
}
 
/**
* Resume the live wallpaper animation
*/
Public void resumePainting (){
This. wait = false;
Synchronized (this ){
This. Policy ();
}
}
 
/**
* Stop the live wallpaper animation
*/
Public void stopPainting (){
This. run = false;
Synchronized (this ){
This. Policy ();
}
}
 
@ Override
Public void run (){
This. run = true;
Canvas c = null;
While (run ){
Try {
C = this. surfaceHolder. lockCanvas (null );
Synchronized (this. surfaceHolder ){
CurrentTime = System. currentTimeMillis ();
UpdatePhysics ();
DoDraw (c );
PreviousTime = currentTime;
}
} Finally {
If (c! = Null ){
This. surfaceHolder. unlockCanvasAndPost (c );
}
}
// Pause if no need to animate
Synchronized (this ){
If (wait ){
Try {
Wait ();
} Catch (Exception e ){}
}
}
}
}
 
/**
* Invoke when the surface dimension change
*/
Public void setSurfaceSize (int width, int height ){
This. width = width;
This. height = height;
Synchronized (this ){
This. Policy ();
}
}
 
/**
* Invoke while the screen is touched
*/
Public void doTouchEvent (MotionEvent event ){
// Handle the event here
// If there is something to animate
// Then wake up
This. wait = false;
Synchronized (this ){
Notify ();
}
}
 
/**
* Do the actual drawing stuff
*/
Private void doDraw (Canvas canvas ){}
 
/**
* Update the animation, sprites or whatever.
* If there is nothing to animate set the wait
* Attribute of the thread to true
*/
Private void updatePhysics (){
// If nothing was updated:
// This. wait = true;
}
 
}

 

If the desktop wallpaper is visible and the system service notification has something new, this class will first draw it on the canvas. If there is no animation,UpdatePhysicsWill notify the thread to wait. Normally, SurfaceView will draw the last time on the canvas when there are two canvases in turn ......

If you want to configure your dynamic wallpaper, you only need to create a PreferenceActivity and declare it in the wallpaper. xml file. At the same time, let the SharedPreference object find your configuration options.

The tutorial is written here. If you do not understand anything, you can use Eclipse to browse the complete source code: SampleLiveWallpaper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.