Dynamic Wallpaper Example of Android programming _android

Source: Internet
Author: User
Tags visibility

This example describes the dynamic wallpaper of Android programming. Share to everyone for your reference, specific as follows:

Since the Android 2.1 version introduced the concept of dynamic wallpaper, people familiar with Android will not be unfamiliar. Here explains how a dynamic wallpaper is formed and how it works.

First, dynamic desktop dynamic shows that the component is real-time change, that is, there is a background in the constant refresh of this component. Lenovo to the background component first thought is service, from the code point of view, sure enough. Each dynamic desktop inherits from Wallpaperservice, which must implement the abstract method Oncreateengine, returns a engine object, in fact all the drawings and refreshes are completed by the engine, the service is to provide the engine part.

For example, there is a preview when we set up a dynamic wallpaper, when we start a preview of the engine to draw with the refresh, when we set up the wallpaper and then start a fill up the entire Desktop Engine in real time drawing and refreshing. So the focus of the dynamic wallpaper is in engine, which is mentioned here.

Now start creating a dynamic wallpaper.

The first step is to create our Wallpaperservice class, which also creates the engine class in which engine is the wallpaperservice inner class. One of the key methods in engine is

Reference

Copy Code code as follows:
OnCreate (Surfaceholder Surfaceholder)

With Surfaceholder we can get canvas objects, and with canvas we can draw

Reference

Copy Code code as follows:
Onoffsetschanged (float xoffset, float yoffset, float xoffsetstep,float yoffsetstep, int xpixeloffset, int ypixeloffset)

This method is triggered when the screen slides

Xoffset can be used to determine the format of the screen ordinal percentage, for example, if you have 5 split screen on your phone, the first screen here is 0.000, the second screen is 0.2000 the third screen is 0.4000, etc.
Xoffsetstep from the literal sense that the stepping is also related to your screen number if your screen number is 5, each step xoffsetstep is 0.20000.
The Xpixeloffset is actually the moving distance of the pixel, that is, how many pixels are moved, and the strange thing is that the offset of the pixels moving around here is negative.

Reference

Copy Code code as follows:
public void Onvisibilitychanged (Boolean visible)

This method is triggered when the visibility of the dynamic wallpaper changes, for example, when the visibility of the dynamic wallpaper is true on the desktop, and when you run a program, the visibility of the dynamic wallpaper changes to false, which is well understood here

Reference

Copy Code code as follows:
Public Bundle OnCommand (String action, int x, int y, int z, Bundle extras, Boolean resultrequested)

can monitor the click event, click to trigger this method, action for android.wallpaper.tap,x recorded the horizontal axis, Y recorded the ordinate, Z role unknown, may be reserved for the 3d desktop? Not tested, Z is generally 0
There are also important life-cycle approaches, similar to activity, that are not much discussed.

When you want to define an app as a dynamic wallpaper, you first need to specify it in Androidmanifest.xml, as mentioned previously, dynamic wallpaper is supported from Android 2.1, so you need to add

Reference

Copy Code code as follows:
<USES-SDK android:minsdkversion= "7"/>

Since we also need to explain to the system that this is a dynamic wallpaper, we also have to join

Copy Code code as follows:
<uses-feature android:name= "Android.softwate.live_wallpaper"/>

In addition, as we run the service, we also need to configure the service
Reference

<service android:name= "Livewallservice" android:label= "My_live _wallpaper" 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/livepaper "/> 
</service>

The more important part first is the permission android:permission= "Android.permission.BIND_WALLPAPER"
Second service needs to respond to action

Copy Code code as follows:
Android:name= "Android.service.wallpaper.WallpaperService

Then there's the config file.
Copy Code code as follows:
<meta-data android:name= "Android.service.wallpaper" android:resource= "@xml/livepaper"/>

Next receive the configuration file. First, create an XML directory under the Res folder, just as you would write Appwidget.
In the directory we create an XML file

<?xml version= "1.0" encoding= "Utf-8"?> <wallpaper xmlns:android= 
"http://schemas.android.com/apk/res/" Android " 
 android:settingsactivity=" livewallpreference " 
 android:thumbnail=" @drawable/ic_launcher " 
 android:description= "@string/wallpaper_description" 
 /> 

Wallpaper is what the system requires and takes a namespace.

Reference

Copy Code code as follows:
Android:settingsactivity= "Livewallpreference"

Specifies the preferenceactivity that configures the dynamic wallpaper, and this preferenceactivity also needs to be registered in Androidmanifest.xml, but just as with normal activity. When we click on the setting button of the dynamic wallpaper, the activity is directed. Indispensable, otherwise click the settings will be an error.

Reference

Android:thumbnail= "@drawable/ic_launcher" 
android:description= "@string/wallpaper_description" 

The first icon corresponds to the icon in the dynamic wallpaper list, and the second description is the name of the dynamic wallpaper you created on the right side of the icon.

The framework for this dynamic wallpaper is complete. The next step is to add a refresh mechanism to the Wallpaperservice class, adding dynamic content, and a dynamic desktop is complete.

I hope this article will help you with the Android program.

Related Article

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.