📜 ⬆️ ⬇️

How to create Scrollable Live Wallpaper for Android 2.1 (part one, it's the penultimate one)

Hello. Today I would like to share the technique of creating a live wallpaper for Android 2.1 with the ability to scroll through the image.
I propose to determine the environment without delay and start coding in Eclipse, testing in the Android SDK and AVD Manager emulator, and uploading to a phone that is interactive (fewer taps for a call, quick navigation via GoogleMaps with the ability to call) now reminds me more navigator phone. Who knows, maybe after the next iteration, codenamed froyo, Google will think about creating a 2D notebook book, expanding it on a map?
I will describe the creation of an application from scratch for those who are newbie. You will need Android JDK and also ADT and Eclipse by itself. Difficulties with installing a plugin that ADT for Eclipse I did not have, and you will not mean. After restarting the IDE, the “Android SDK and AVD Manager” will appear in the Windows tab. Create a new device emulator with API Level 7, that is, Android 2.1 or API Level 8, Android 2.2, but not lower, since the Live Wallpaper feature in earlier versions simply did not exist.
In our application, jpg in size 640x533 is used as a picture. It is impossible to cover the entire screen with it, in such cases scrolling will help. I propose to focus only on the horizontal scrolling, which you organize both within, for example, the main homescreen, and the transition to the neighboring ones. In the testing device HTC Desire, by the way, there are 7 homescreens, so it probably does not make sense to be limited to the frontal one. Of course, you understand that the same image will have a different appearance on the device with density 240, 160 and 120, therefore it is important to know in advance the screen diagonal and density, if your applique is not able to determine these parameters well, or if you don’t set yourself task, targeting a specific model. In both cases, the relationship between the physical and displayed dimensions is calculated by the formulas:
realScreenWidht = screenWidth / (density / 160)
realScreenHeight = screenHeight / (density / 160),
that is, for HTC Desire with WVGA dimension (480x800), and a diagonal of 3.3 "-4.0", (with high density, hdpi, 240), the size of the image we need, which occupies the entire height of the screen, looks like this:
density / 160 = 1.5
realScreenHeight = 800 / 1.5 = 533
The width of the picture size is 2 homescreen, therefore
realScreenWidht = 480 * 2 / 1.5 = 640
That is, with the theory and the environment have figured out, I propose to create an Android project called ScrollableLiveWallpaper, choosing Build Target as Android 2.1. Application name is set as ScrollableLiveWallpaper, package - com.scrollable.live.wallpaper. The name of the Activity is entered in the name (of the project, in our case) with a postfix, that is, ScrollableLiveWallpaperActivity, according to the unspoken name convention.
Next, let's transform a cocoon (Activity) into a butterfly (Live Wallpaper), for which we open AndroidManifest.xml, completely forgetting about ScrollableLiveWallpaperActivity.java. In the Application tab, we are interested to make sure that the ScrollableLiveWallpaperActivity class is specified in the application nodes field; we delete it with all attributes and filters of system events. Live wallpaper is a service that is created simply:
in the new class ScrollableLiveWallpaperService we inherit from WallpaperService. The method required for implementation is needed only to start the ScrollableLWEngine engine.

 public class ScrollableLiveWallpaperService extends WallpaperService {
	 public static final String SHARED_PREFS_NAME = "settings";
	
	 @Override
	 public Engine onCreateEngine () {
		 return new ScrollableLWEngine ();
	 }
	
	 class ScrollableLWEngine extends Engine implements OnSharedPreferenceChangeListener {
		 @Override
		 public void onSharedPreferenceChanged (SharedPreferences arg0, String arg1) {
		 }
	 }
 }

Let's go back to AndroidManifest.xml and add a service to the nodes:
one

filter
2
')
and metadata needed to create Preference Screen
3

In the res folder, create an xml / scrollable.xml:
 <? xml version = "1.0" encoding = "utf-8"?>
 <wallpaper xmlns: android = "http://schemas.android.com/apk/res/android"
	 android: thumbnail = "@ drawable / icon"
	 android: description = "@ string / app_name"
	 android: settingsActivity = "com.scrollable.live.wallpaper.preferences.ScrollablePreferences" />

Create the ScrollablePreferences class in the com.scrollable.live.wallpaper.preferences package:
 public class ScrollablePreferences extends PreferenceActivity implements 
                    OnSharedPreferenceChangeListener {

	 @Override
	 protected void onCreate (Bundle icicle) {
		 super.onCreate (icicle);
		 getPreferenceManager ().
                        setSharedPreferencesName (ScrollableLiveWallpaperService.SHARED_PREFS_NAME); 
  addPreferencesFromResource (R.xml.preferences); 
		 getPreferenceManager ().
                        getSharedPreferences (). registerOnSharedPreferenceChangeListener (this);
	 }

	 @Override
	 public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {
	 }
	
	 @Override
	 protected void onResume () {
		 super.onResume ();
	 }

	 @Override
	 protected void onDestroy () {
		 getPreferenceManager ().
                        getSharedPreferences (). unregisterOnSharedPreferenceChangeListener (this);
		 super.onDestroy ();
	 }
 }

Preferences Screen Activity is almost bolted to the service. It remains to create preferences.xml in res / xml:
 <PreferenceScreen xmlns: android = "http://schemas.android.com/apk/res/android"
         android: title = "@ string / settings"
         android: key = "Scrollable_settings">
     <ListPreference
             android: key = "key_speed_scroll"
             android: title = "@ string / settings_scroll_speed_title"
             android: summary = "@ string / settings_scroll_speed_summary"
             android: entries = "@ array / speed_scroll_keynames"
             android: entryValues ​​= "@ array / speed_scroll_keyvalues" />
 </ PreferenceScreen>

Add to strings.xml
	 <string name = "settings"> Settings </ string>
	 <string name = "settings_scroll_speed_title"> Select Scroll Speed ​​</ string>
	 <string name = "settings_scroll_speed_summary"> Choose speed of the scrolling </ string>

The Preferences Screen will contain a vertical list of 7 radiobutton elements, the name and value of each of which are stored in a resource called values ​​/ array.xml:
  <? xml version = "1.0" encoding = "utf-8"?>
 <resources xmlns: xliff = "urn: oasis: names: tc: xliff: document: 1.2">
     <string-array name = "speed_scroll_keynames">
         <item> "First" </ item>
         <item> "Second" </ item>
         <item> "Third" </ item>
         <item> "Fourth" </ item>
         <item> "Fifs" </ item>
         <item> "Sixth" </ item>
         <item> "Seventh" </ item>
     </ string-array>

     <string-array name = "speed_scroll_keyvalues">
         <item> "1" </ item>
         <item> "2" </ item>
         <item> "3" </ item>
         <item> "4" </ item>
         <item> "5" </ item>
         <item> "6" </ item>
         <item> "7" </ item>
     </ string-array>
 </ resources>

Before launching live wallpaper, add a class of presets to the list of application nodes:
four
Obina is ready to launch

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


All Articles