Last winter (most likely on a sunny day :) I became interested in the AndEngine graphic library, because I wanted to work with two-dimensional graphics on my mobile phone (using OpenGL), and this solution seemed to me the most interesting and accessible. Having made several graphic applications, I decided to create live wallpapers, all the more so in the delivery with AndEngine there is a special library for creating those. Now I will share my experience in creating live wallpapers with you.
Especially for this, I prepared a
project (has abundant comments), a “template” for showing the principle of live wallpaper.
TrainingFirst of all, you need to install AndEngine itself and, in particular, the AndEngineLiveWallpaperExtension library (I recommend to immediately install all available libraries) since it will be the base. Since this article is not a base for studying AndEngine, I will not tell you how to install libraries, especially since you can easily find this information, for example, here -
http://www.matim-dev.com/how-to-setup-andengine .html . Now you need to open the attached project and run it. If it does not start right away, then most likely you need to check the connected libraries to the project. In the end, you should have something like this -
http://youtu.be/HRKzBma9vz0FunctionalAnd now briefly and in order (most of the functionality also applies to all other AndEngine GLES2 applications). The ancestor of the main class of the project is BaseLiveWallpaperService, this is the class that is responsible for all the work with GLES2. The following functions are required for overriding:
onSurfaceChanged onCreateEngineOptions onCreateResources onCreateScene
public void onSurfaceChanged (final GLState pGLState, final int pWidth, final int pHeight) is a function for determining the screen size and installing the camera.
public EngineOptions onCreateEngineOptions () is the function for setting the live wallpaper engine. This function returns the selected parameters to the system for setting up GLES2.
public void onCreateResources (OnCreateResourcesCallback pOnCreateResourcesCallback) - as the name implies, this function is designed to create all (media) resources that will participate in the application.
public void onCreateScene (OnCreateSceneCallback pOnCreateSceneCallback) is the function of creating a scene and placing all the desired objects on it.
')
Download testuresLoading texture occurs in three stages: the definition of the atlas; texture definition; texture loading into satin Consider them on the example of loading the background:
this.mOnScreenControlTexture0 = new BitmapTextureAtlas(this.getTextureManager(), 1930, 1050, TextureOptions.BILINEAR_PREMULTIPLYALPHA); this.bg_TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture0,this,"gfx/bg.jpg", 0, 0); this.getEngine().getTextureManager().loadTexture(this.mOnScreenControlTexture0);
The first line defines the size for the texture atlas. It is necessary to determine in advance which textures and in what order they will be located on this atlas and calculate its size. If it is wrong to calculate the position of the textures in the atlas or to make it smaller than the required size, then the program will not work correctly. In our case, only one image (sprite) will be located in the atlas and therefore the size of the atlas will be the same size as the image.
If you want to load two pictures (sprite) and more, then the size of the atlas will have to be calculated. For example: We want to load two pictures with the size of 60 * 60 and 80 * 75 pixels. To find the width of the atlas, we add the width of the first and second pictures, we get 140. At the same time, the height of the atlas should be leveled by the largest of these pictures. If necessary, the layout of the atlas can be of any complexity, everything is limited only by the imagination of the programmer and the maximum size (2048 * 2048).

The second line defines the object through which in the future you can get the texture of interest. The constructor arguments are as follows: atlas for storing textures; context; texture location; X coordinate starting from which the texture is located; Y coordinate, starting from which the texture is located. All textures should be stored in the “gfx” park (
project / assets / gfx ).
The third line contains the loader itself, with which the entire graphic is loaded into the atlas.
Scene creationThe scene is created in just one line - mScene = new Scene (), then the most interesting thing happens - the placement of all the necessary objects on the stage. The main building unit of all applications built using AndEngine are sprites (since the library works exclusively with two-dimensional objects). Consider an example of creating a simple sprite:
bg = new Sprite(0, 0, this.bg_TextureRegion, getVertexBufferObjectManager()); bg.setSize(bg.getWidth() * 2, bg.getHeight() * 2); bg.setPosition(10, 20); mScene.attachChild(bg);
The first line creates a sprite. The constructor has the following arguments: position on the stage along the X axis; position on the stage along the Y axis; texture object; buffer manager. The second line overrides the size of the sprite, by default it is equal to the size of the texture. The third line overrides the position of the sprite on the screen. In the fourth line, the sprite is directly added to the screen.
If you look at the code for the attached example, you will notice that the Sun class is defined in it. It is a descendant of the Sprite class. This class contains the logic of the animation of an object, which is to rotate an object relative to its center.
Also defined class LiveWallpapperSettings, which is responsible for setting the live wallpaper. Since this is a fairly standard thing, I will not describe the logic of the settings (you can easily find the information of interest in Yandex or in other search engines).
ConclusionThe library is very easy to use and functional. Its main drawback is the freezing mechanism, which is triggered when wallpaper is hidden and does not always turn off when you continue to display it. This minus is clearly seen when the wallpapers have a relatively large number of sprites on the scene (like for example here
https://play.google.com/store/apps/details?id=com.livewallpaper.summermeado_wfull ). If someone solved the problem with freezing, please share the solution.
I deliberately did not inflate the example and add the maximum amount of functionality to show that Android graphics applications are quick and easy.
Have a good test!
Live Wallpaper Project -
http://www.anprog.com/documents/AE_gles2_LiveWallpaper.zip