Android4.0 Launcher Drag-and-drop principle analysis 1

Source: Internet
Author: User

In the Android4.0 source code comes with the launcher, the drag is controlled by the Dragcontroller.

1, the basic process:
    1. The corresponding view in the detection of user action after the judgment, if you can trigger drag and drop, then set their own corresponding state, and then drag and drop the object's bitmap object, the current position, drag source, to drag the object and other information to Dragcontroller StartDrag method start dragging.
    2. Next, Draglayer's onintercepttouchevent intercepts the touchscreen event and transfers it to the Dragcontroller ontouchevent method.
    3. In the Dragcontroller ontouchevent, call Dragcontroller's handlemoveevent to move the object, and inform the corresponding drag object state changes.
    4. Finally, when lifting events are detected in Dragcontroller's ontouchevent, the drop method is called to release the object to be dragged, and the EndDrag method is called to end the drag.
2. Drag to start

In launcher, dragging is triggered in two ways:

    1. Drag and drop on the workspace;
    2. Select an app or widget from Appscustomizepagedview to place on workspace.
2.1 Drag and drop on Workspace 1) long Press an icon or widget

In this case, the Onlongclick method of the launcher is called, In turn, the StartDrag method for non-folder calls to workspace hides the view and draws the icon bounds (the blue border graphic displayed on the workspace to identify where the current drag icon is located), Finally, go to the Workspace.begindragshared method.

2) long Press an icon in the open folder

In launcher, for the long press of the element in the folder, it is processed in the Onlongclick of folders, so long Press an icon or widget , and for the folder, it is returned directly.

If drag is allowed, the begindragshared of workspace is called to process.


3) Begindragshared Method of workspace

As can be seen from the above, as long as the drag on the workspace to start the object, will eventually go to the Workspace.begindragshared method. In this method, the first drawing is drawn using createdragbitmap (including drawing a circle of red boundaries in the outer layer of the original), then calculating the position and boundary and passing it to Dragcontroller management.

2.2 Select an app or widget from Appscustomizepagedview to place on workspace

Appscustomizepagedview inherits from Pagedviewwithdraggableitems, the application drawer that we normally call. When you press and hold the app icon or widget, the Appscustomizepagedview is hidden, showing the workspace state, which is spring_loaded.

In the source code, this state conversion has three entrances, all are given in pagedviewwithdraggableitems, namely Onintercepttouchevent, Ontouchevent and Onlongclick. Eventually all go to Appscustomizepagedview's Begindragging method. But the author tried several times and found only Onlongclick was called.


1) Appscustomizepagedview's begindragging

Begindragging is similar to a proxy method, first the conversion of the launcher state, and then depending on the drag-and-drop object, the different drag-and-drop method is called.


2) Application drag-and-drop begindraggingapplication

For applications, drag from the drawer to the desktop, the interface hidden in the begindragging has been processed, appscustomizepagedview do not need to save any information about the dragged application (even if the drag-and-drop, You just need to re-display the Appscustomizepagedview, not the same as folder, you need to restore the shortcut. Therefore, just notify workspace to draw the icon boundary, and then start dragging.

3) widget/shortcut to drag Begindraggingwidget

For elements in the widget list, it is possible to have a shortcut, so you need to make a decision. Draw graphics and icon boundaries in different ways for different types of drag-and-drop objects.

3, Dragcontroller Drag control Process 3.1 StartDrag () Start dragging

As can be seen from the above, regardless of the way into the drag-and-drop, eventually call the Dragcontroller StartDrag method for processing.

In Dragcontroller, StartDrag is a polymorphic method, but in the end, it all goes to the following implementation.

1 public void StartDrag (Bitmap b, int draglayerx, int draglayery, DragSource source, Object draginfo, int dragaction, point Dragoffset, Rect dragregion)

The logic of the StartDrag is clearer, primarily by notifying the corresponding listener to drag and drop, and then creating a drag object and its view to move it to the current touch position.

3.2 Onintercepttouchevent ()

Draglayer inherits from ViewGroup, whose Onintercepttouchevent method returns True, indicating that a touch event needs to be intercepted, the subsequent sequence of events is passed to its own Ontouchevent method and no longer passed to its child controls.

Dragcontroller's onintercepttouchevent is called by Draglayer Onintercepttouchevent to intercept the handling of touch-screen events. When the user taps the screen, the Action_down event is triggered and the current touch position is recorded. When lifted, triggers the Action_up event and ends the drag. If the lift is in drag, release the dragged object at the current position (the call was not detected during the author's test). Finally, returns whether it is in a drag state.

Therefore, if this is a drag-and-drop, subsequent touchscreen events will only be passed to the Draglayer ontouchevent.

ontouchevent ()

Ontouchevent because the touch event is handled, returning TRUE indicates that the event is consumed and the event is no longer passed to the parent control's ontouchevent.

The Dragcontroller ontouchevent is called by Draglayer Ontouchevent to handle the movement of the dragged object.

When the StartDrag execution is complete, the Dragcontroller setting is set to true, so that the touch event will eventually go to Ontouchevent, where it is called handlemoveevent to move the object. The basic flow is as follows.

 

3.3 handlemoveevent () to move

Handlemoveevent is the main method of drag-and-drop, and when the user triggers the drag and pull, the Dragcontroller will move the dragged object view through this method, and inform the respective state changes of each release target. If you enter the sliding screen area and allow the slide screen, perform the corresponding slide screen operation. As shown in.

 

3.4 Drop () Releases the dragged object to its current position

When the user moves the dragged object to the appropriate location, you can move the finger away from the screen. At this point, the drop method is called in onintercepttouchevent (not tried out) and ontouchevent to release the dragged object.

Its main function is to find the object of drag and Drop (droptarget), if found and accept the release, to inform the object is put into the drag. Finally, you are notified of the drag-and-drop source (the container where the dragged object was originally located) and dragged the result.

Finddroptarget () to find the drag target object that corresponds to the current position

In both handlemoveevent and drop, the finddroptarget is used to find the drag object of the current position, and the basic principle is to traverse all the registered drag objects, if they are supported and the current position is within the trigger area of the object. The match returns the object successfully.

3.5 Dragcontroller Drag-and-drop control process Summary

Workspace.begindragshared ()/appscustomizepagedview.begindragging (), StartDrag () onintercepttouchevent () Ontouchevent (), Handlemoveevent (), Drop (), Finddroptarget ()

In general, Dragcontroller drag control is:

    1. Enter State : Use StartDrag to enter the dragging state;
    2. Response Action : Use Onintercepttouchevent and ontouchevent to respond to user's touch screen action;
    3. Handling Movement : Use handlemoveevent to handle the movement of the dragged object;
    4. release Move : Use drop to release the dragged object to the appropriate location.

http://johnsonxu.iteye.com/blog/1933655

Android4.0 Launcher Drag-and-drop principle analysis 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.