⬆️ ⬇️

Android live wallpaper. How to do?

Regardless of what wallpaper you want to do, there are 2 different approaches to the realization of your ideas: SGL (hereinafter referred to as Canvas) and OpenGL. When I created my first wallpaper, it took me several days to try various tools and methods, so this article will save you some time and quickly get you up to speed.





Canvas



If it works for you, don’t even think about OpenGL



What is a Canvas? This is the main way to draw GUI to android. All forms, buttons and switches are rendered using this technology. We are given a bitmap of a certain size (1024x600 for example), on which we must draw. We can draw using the Canvas class, which has a rich high-level drawing API. Say, drawing a circle of a given thickness of a certain color with smoothing on does not pose any difficulty. There is even support for shaders.

')

But then why is OpenGL being asked?



Canvas Disadvantages:



Benefits of Canvas:



We will return to the outline, now let's talk about an alternative approach.



Opengl



If the Canvas fails, then nothing else remains.



OpenGL, DirectX - we all heard these "cool" words. On Android, we have OpenGL available, but not simple (not like the desktop), but stripped down. And we even have a choice: OpenGL ES 1.0 / 1.1 (works everywhere) or OpenGL ES 2.0 (Android 2.2+). What is ES in the title? ES tells us that this is a stripped-down version of OpenGL, from which everything unnecessary has been removed, in order to make the API as compact as possible and not to drag things onto mobile devices that are easy to emulate on a central processor. That is, not all the examples from books on the usual OpenGL will work for you, but in principle, everything remains as well.



As noted, we have two versions: 1.0 / 1.1 and 2.0. You can read about their differences in Wikipedia (choose English). And not all devices equally support all OpenGL ES extensions, before using some, you need to make sure that it is supported (you can read it here ).



Disadvantages of OpenGL:



Advantages of OpenGL:



And what to do?



OpenGL. Making it easier.



When it is hard alone - ask for help.



It is clear that we are not the first who decided to use OpenGL and we all already played beautiful toys, cut the ropes and saved the stolen eggs. Rarely when a game is written completely from scratch, engines are usually used, which take on all the boring lessons and offer us a simpler API for realizing our fantasies. And to turn out, in relation to live wallpaper, the options to make it easier for us 2 ...



GLSurfaceView adapted for 3D Live Wallpapers



This is the title of the article by Robert Green, where he talks about the classes he created (the main one being GLWallpaperService), which do all the work of setting up for us. It remains for us to write the code that draws. After downloading the project files from github (the project is live and recently updated), you will get ready-made wallpapers that really don't do anything beautiful (just fill the entire background with color), but they are already working. You just have to write the required code for drawing. The interface of interaction with the system and the basic configuration of OpenGL have already been taken care of. Robert uses OpenGL 1.0 / 1.1, but switching to 2.0 does not pose any difficulty. It is also worth noting that no additional funds for drawing are provided, you will have to draw all the same OpenGL ES API.



AndEngine



AndEngine is a real 2d game creation engine. The engine supports several extensions, among which physicsbox2d to create physics. The engine itself is quite popular with open source (Java). All means of the engine can be used to create wallpaper. There are many examples that are the only “documentation” of the engine. If you need to do something, we recall in what example we saw it and see how it is implemented there. There is also a forum where you can ask for help (the forum is active, they quickly help).



Now about the bad.



Versions of the engine 2. The first (GLES1) uses OpenGL ES 1.0 / 1.1, the second (GLES2) - ES 2.0. The API in GLES2 is a lot more advanced, so it's better to use GLES2, but then we lose Android version 2.1 users (GLES2 doesn't work on it, but live wallpaper works great). I managed to launch the wallpaper using both versions of AndEngine. But in GLES1 I immediately encountered the impossibility of changing the camera settings when the screen orientation was changed (you have to change the scale of the scene along one of the axes, in the GLES2 API it allows it).



The engine is designed for 2d games, so no API for 3d support is provided (no one forbids using OpenGL directly). Also, it would seem that such necessary functions as drawing a circle and even a triangle are missing. The engine is more focused on sprites. It is rather hard to add the drawing function of the same circle so that it fits into the engine class system - there is no documentation. Therefore it is necessary to shovel a lot of code and understand what's what.



Conclusion



Let us begin to draw conclusions from the fact that I will give an example: one frame of the wallpapers I created using Canvas is drawn in 17ms on the Kindle Fire (6 of which “break” and “unlock” the outline). This is a lot to fill the background with 16x16px texture, display the image at 350x180px and draw 4x circles of various thickness and radius. When using OpenGL, this is done an order of magnitude faster. About the good: I wrote the rendering code in an hour and a little, 17ms a bit - the animation is smooth.



In the rest.



Use canvas if you don’t care about 3d, if your animation is not very complex and does not require large computational resources.



Use AndEngine if you are doing 2d wallpaper. After studying the examples and reading the forum, you will quickly start creating your animation, and additional engine extensions will allow you to easily implement effective solutions (animation, particle system, etc.) in your animation.



Use Robert Green's GLWallpaperService. If you own OpenGL well and do 3d wallpaper, it will allow you, without being distracted by the routine, quickly start coding your animation.



You can also customize many of your favorite OpenGL graphics engines to create live wallpapers. Using the code GLWallpaperService and / or AndEngine as an example.

Source: https://habr.com/ru/post/137275/



All Articles