This article describes the dynamic wallpaper of Android programming. Share it for your reference, as follows:
Since Android 2.1 version, the concept of live wallpaper has been introduced, and people who are familiar with Android must be familiar with it. Here we explain how a dynamic wallpaper is formed and how it works.
First of all, the dynamic desktop dynamics reflect that this component changes in real time, which means that there is a background that keeps refreshing this component. When you think of background components, the first thing you think of is service. From a code perspective, that's true. Each dynamic desktop inherits from WallpaperService, where the abstract method onCreateEngine must be implemented returns an Engine object. In fact, all drawings and refreshes are completed by the engine, and the service is the part that provides the engine.
For example, when we set up a dynamic wallpaper, there is a preview engine, and then a preview engine is started to draw and refresh. After setting up the wallpaper, we start to draw and refresh the engine that fills the entire desktop in real time. So the focus of dynamic wallpaper is in engine, which is mentioned here later.
Now start creating a live wallpaper.
The first step is to create our WallpaperService class, and also create the engine class in it. Engine is the inner class of WallpaperService. Among them, the key methods in engine are
Quote
With surfaceholder we can get the canvas object, and with canvas we can draw
Quote
This method is triggered when the screen slides
xOffset can be used to determine the percentage of the screen number. For example, if you have 5 split screens on your phone, the first screen is 0.000, the second screen is 0.2000, the third screen is 0.4000, and so on.
xOffsetStep can be understood as stepping literally. It also has to do with your split screen number. If your split screen number is 5, then each step xOffsetStep is 0.20000
xPixelOffset is the actual movement distance of the pixel, that is, how many pixels have been moved. Strangely, the offsets of the pixels moving left and right here are negative
Quote
This method is triggered when the visibility of the dynamic wallpaper changes. For example, when on the desktop, the visibility of the dynamic wallpaper is true. When you run a program, the visibility of the dynamic wallpaper becomes false. It is easy to understand here
Quote
You can listen to click events, and this method is triggered when clicking. The action is , x records the horizontal coordinates, y records the vertical coordinates, and the function of z is unknown. Maybe it was reserved for the 3d desktop? Haven't tried it, z is generally 0
There are also some important life cycle methods, similar to activity, so I won’t say much.
When you want to define an app as a dynamic wallpaper, you need to specify it in it first. As mentioned earlier, dynamic wallpapers are supported since Android 2.1, so you need to add them.
Quote
Since we also need to explain to the system, this is a dynamic wallpaper, we also need to add
In addition, since we run the service, we also need to configure the service.
Quote
<service android:name="LiveWallService" android:label="My_live _wallpaper" android:permission=".BIND_WALLPAPER" > <intent-filter> <action android:name="" /> </intent-filter> <meta-data android:name="" android:resource="@xml/livepaper"/> </service>
The more important part is the permissions android:permission=".BIND_WALLPAPER"
Secondly, service needs to respond to action
Then there is the configuration file
Next, receive the configuration file. First, create an XML directory in the res folder, just like writing appwidget.
In the directory we create an xml file
<?xml version="1.0" encoding="utf-8"?> <wallpaper xmlns:andro android:settingsActivity="LiveWallPreference" android:thumbnail="@drawable/ic_launcher" android:description="@string/wallpaper_description" />
wallpaper is written like this by the system, with namespace on it.
Quote
Specify the PreferenceActivity to configure the dynamic wallpaper. This PreferenceActivity also needs to be registered in it, but it is the same as the general activity. When we click the Settings button for the Live Wallpaper, guide this activity. Is indispensable, otherwise clicking on settings will report an error.
Quote
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.
At this point, the frame of the dynamic wallpaper is completed. Next, you only need to add a refresh mechanism and dynamic content to the wallpaperservice class, and a dynamic desktop is completed.
I hope this article will be helpful to everyone's Android programming design.