Android's Launcher transforms the effect of imitating desktop publishing

 1. Background

1.1 Newly took over a lighting control project, its pages and effects are still relatively complicated, one of the functions is to layout lamps like the Apple desktop, and support dragging, grouping, and paging.

When dragging the icon, judge whether there is a blank space or there is already a space. If there is, combine the two icons into one folder. At the same time, you have to judge whether they are different types of lamps. They cannot be merged and are mutually exclusive. , will squeeze the original position of the icon

1.2 You can see the layout effect of the demand

f799b2c68c674d36ad61ba4a01f95500.jpeg

dba47d2c3bb84220a7e834f76ee0d7e2.jpeg

1.3 Scheme:

The key to this effect lies in the change of the position coordinates of the grid and the drag position. You can divide a page into several grids equally, record the position coordinates of the grid on the page, and judge whether it is in a certain grid when you drag the icon and let go. Within the scope of the grid, if it is within the grid, the icon will be displayed in the center of the grid

Two, google native desktop

2.1 Since you want to imitate the desktop, find a set of desktop programs for reference

Official source code:

https://android.googlesource.com/platform/packages/apps/Launcher3/

github project reference

https://github.com/luohaohaha/launcher3

2.2 Google official source code analysis

Desktop Effect Structure

689d962ac034437eaca4eb6edb2eb4d5.png

 2.3 Project directory structure

c029417b7b014f6483567dbf2dd866f1.png

2.4 Key functions

LauncherModel:
It is related to the data. It saves the status information of the desktop when it is running, and also provides an API for reading and writing the database. It has an internal class LoaderTask. When the desktop starts reading data from the database and adding icons and widgets to it It is he who is used.

BubblTextView:
Icons are based on him, but strangely, he is inherited from TextView

DragController:
DragLayer is just a ViewGroup, and the specific drag and drop processing is placed in DragController.

LauncherAppState:
singleton mode, mainly used at startup, it initializes some objects, and registers broadcast listeners and ContentObserver.

DragView:
The View that moves with the finger when dragging the icon is him.

DragSource, DropTarget:
interfaces related to drag and drop. DragSource indicates where the icon is dragged from, and DropTarget indicates where the icon can be dragged.

Folder:
The view when the folder is opened.

FolderIcon:
folder icon.

LauncherProvider:
database class, Launcher3 uses SQLite, the database file is saved in /data/data/package name/databases/launcher.db, interested students can copy this thing out, and use SQLite tools to see what is inside conserved.

ItemInfo:
The information of each item on the desktop is saved during runtime, including which screen the icon is on, which row and column, height and width, etc. Each ItemInfo object corresponds to a record in the database. Under the source code path of Launcher3, there will be many classes ending with Info. These classes are subclasses of ItemInfo, which specifically represent a certain item on the desktop. For example, FolderIcon corresponds to FolderInfo, BubbleTextView corresponds to ShortcutInfo, and AppWidgetHostView corresponds to LauncherAppWidgetInfo. With the corresponding relationship, you can get the ItemInfo object through the view like this:
ItemInfo info = (ItemInfo)bubbletextview.getTag();
In this way, the info here is actually the ShortcutInfo object.

LauncherProvider:
ContentProvider for desktop information.

LauncherSettings:
stores database-related constants, field names, field constants, etc.

DatabaseHelper:
The internal class of LaucherProvider, inherited from SQLiteOpenHelper, the creation of the database table is done in its onCreate method.

itemInfo: entity class

itemInfo type:

Widget: AppWidget
Shortcut: App Icon
Folder: Folder

ItemInfo important parameters:

container: indicates where the icon is placed, whether it is placed in Workspace or Hotseat, or in a folder. If it is placed on the Workspace, then the value is LauncherSettings.Favorites.CONTAINER_DESKTOP, if it is placed in the folder, then the value of the container is the id of the folder FolderInfo.
cellX, cellY: indicates the position of the screen, cellY indicates the row, and cellX indicates the column. If the gadget occupies multiple rows and multiple columns, record the position of its upper left corner.
spanX, spanY: Width and height, the width and height of shortcuts and folders are both 1, the width and height of widgets depends on the specific situation.
title: Title, displaying the name of the application, the name of the folder, and widgets do not need this attribute.
itemType: It is saved in the database to indicate which type of ItemInfo this ItemInfo is. When starting, it is better to generate a specific ItemInfo subclass object.

Three transformations

3.1 There are still a lot of official source code, delete or ban useless ones, and only keep the functions required by the project, such as negative one page, bottom navigation, search bar, slide up to display all applications, prohibit page zoom, wallpaper, group pieces etc.

3.2 Specific local files that need to be modified

f52eb4ff66664f0bbf2c84a41076d349.png

Fourth, integrate into your own project 

 4.1 The above layout transformation is completed, it should be deleted, it should be banned, and the following is to replace it with its own data source. The project mainly uses greenDao to maintain the data table, hierarchical relationship project -> page -> lamp group -> lamps, these are custom data

4.2 At the same time, there is also a Cell cell information table that saves icon-related information, icon coordinates, colors, names, lamp group ids, page ids, and lamp ids, and then traverses the Cell cell data, assigns them to the shortcut icon information ShortcutInfo, and then icon added to the desktop

The following is where the main source code data source is bound:

11a5a60ed04740369ea93d63f43ad59e.png1941f163d2e54c1fb65ce6e07abc3420.pngd0a03c188fe1406cac84c2b13fe55ce1.png

 4.3 Source code rendering

d24599d3de6c462a8b3a9440c0c7178b.jpeg

 4.4 Custom effects

34da904c285c446c87797d48a84fa2ce.jpeg

 4.5 Custom effect source code link, you can refer to it, everything in it can be changed, color, text, layout

MyApplication/customerlauncher3 at master · Biekangdong/MyApplication · GitHub

Guess you like

Origin blog.csdn.net/qq_29848853/article/details/130278271