Android drag and drop image

Android — Drag and Drop

Android drag/drop framework allows your users to move data from one View to another View in the current layout using a graphical drag and drop gesture. As of API 11 drag and drop of view onto other views or view groups is supported.The framework includes following three important components to support drag & drop functionality −

Drag event class.

Helper methods and classes.

The Drag/Drop Process

There are basically four steps or states in the drag and drop process −

Started − This event occurs when you start dragging an item in a layout, your application calls startDrag() method to tell the system to start a drag. The arguments inside startDrag() method provide the data to be dragged, metadata for this data, and a callback for drawing the drag shadow.

The system first responds by calling back to your application to get a drag shadow. It then displays the drag shadow on the device.

Next, the system sends a drag event with action type ACTION_DRAG_STARTED to the registered drag event listeners for all the View objects in the current layout.

To continue to receive drag events, including a possible drop event, a drag event listener must return true, If the drag event listener returns false, then it will not receive drag events for the current operation until the system sends a drag event with action type ACTION_DRAG_ENDED.

Continuing − The user continues the drag. System sends ACTION_DRAG_ENTERED action followed by ACTION_DRAG_LOCATION action to the registered drag event listener for the View where dragging point enters. The listener may choose to alter its View object’s appearance in response to the event or can react by highlighting its View.

The drag event listener receives a ACTION_DRAG_EXITED action after the user has moved the drag shadow outside the bounding box of the View.

Dropped − The user releases the dragged item within the bounding box of a View. The system sends the View object’s listener a drag event with action type ACTION_DROP.

Ended − Just after the action type ACTION_DROP, the system sends out a drag event with action type ACTION_DRAG_ENDED to indicate that the drag operation is over.

The DragEvent Class

The DragEvent represents an event that is sent out by the system at various times during a drag and drop operation. This class provides few Constants and important methods which we use during Drag/Drop process.

Constants

Following are all constants integers available as a part of DragEvent class.

Signals the start of a drag and drop operation.

Signals to a View that the drag point has entered the bounding box of the View.

Sent to a View after ACTION_DRAG_ENTERED if the drag shadow is still within the View object’s bounding box.

Signals that the user has moved the drag shadow outside the bounding box of the View.

Signals to a View that the user has released the drag shadow, and the drag point is within the bounding box of the View.

Signals to a View that the drag and drop operation has concluded.

Methods

Following are few important and most frequently used methods available as a part of DragEvent class.

Sr.No. Constants & Description
1

Inspect the action value of this event..

Returns the ClipData object sent to the system as part of the call to startDrag().

Returns the ClipDescription object contained in the ClipData.

Returns an indication of the result of the drag and drop operation.

Gets the X coordinate of the drag point.

Gets the Y coordinate of the drag point.

Returns a string representation of this DragEvent object.

Listening for Drag Event

If you want any of your views within a Layout should respond Drag event then your view either implements View.OnDragListener or setup onDragEvent(DragEvent) callback method. When the system calls the method or listener, it passes to them a DragEvent object explained above. You can have both a listener and a callback method for View object. If this occurs, the system first calls the listener and then defined callback as long as listener returns true.

The combination of the onDragEvent(DragEvent) method and View.OnDragListener is analogous to the combination of the onTouchEvent() and View.OnTouchListener used with touch events in old versions of Android.

Starting a Drag Event

You start with creating a ClipData and ClipData.Item for the data being moved. As part of the ClipData object, supply metadata that is stored in a ClipDescription object within the ClipData. For a drag and drop operation that does not represent data movement, you may want to use null instead of an actual object.

Next either you can extend extend View.DragShadowBuilder to create a drag shadow for dragging the view or simply you can use View.DragShadowBuilder(View) to create a default drag shadow that’s the same size as the View argument passed to it, with the touch point centered in the drag shadow.

Example

Following example shows the functionality of a simple Drag & Drop using View.setOnLongClickListener(), View.setOnTouchListener()and View.OnDragEventListener().

Sr.No. Constants & Description
1
Step Description
1 You will use Android studio IDE to create an Android application and name it as My Application under a package com.example.saira_000.myapplication.
2 Modify src/MainActivity.java file and add the code to define event listeners as well as a call back methods for the logo image used in the example.
3 Copy image abc.png in res/drawable-* folders. You can use images with different resolution in case you want to provide them for different devices.
4 Modify layout XML file res/layout/activity_main.xml to define default view of the logo images.
5 Run the application to launch Android emulator and verify the result of the changes done in the application.

Following is the content of the modified main activity file src/MainActivity.java. This file can include each of the fundamental lifecycle methods.

Following will be the content of res/layout/activity_main.xml file −

In the following code abc indicates the logo of tutorialspoint.com

Following will be the content of res/values/strings.xml to define two new constants −

Following is the default content of AndroidManifest.xml

Let’s try to run your My Application application. I assume you had created your AVD while doing environment setup. To run the app from Android Studio, open one of your project’s activity files and click Run icon from the toolbar. Android studio installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display following Emulator window −

Now do long click on the displayed TutorialsPoint logo and you will see that logo image moves a little after 1 seconds long click from its place, its the time when you should start dragging the image. You can drag it around the screen and drop it at a new location.

Источник

How to Use Drag-and-Drop in Android Apps?

You may utilize the Android drag/drop framework to enable your users to move data with a graphical drag and drop action. This can be from one View in your own program to another, or between your app and another when multi-window mode is enabled. A drag event class, drag listeners, and auxiliary methods and classes are all included in the framework.

Sample Use Cases for Drag & Drop

  1. Something that helps you drag a pointer on live data to get the color of the object or the thing
  2. In your mobile device’s launcher screen, where every program displays, we may effortlessly drag and drop app icons from one location to another.

Geek Tip: To notify the system that a drag event is being called, your application calls the startDrag() function. This technique also distributes data from one view to another.

Gestures Used in the Full Event

Drag and Drop framework in Android allows you to drag and drop one view to another, i.e. in an Activity, if you have two or more views, you may move the data of one view from one view to the other using the Android drag and drop framework. For example, if your Android activity contains two TextViews, one in one half of the screen and the other in the other, the contents from the first TextView may be dragged and dropped to the other TextView.

  1. Started: You begin by producing a ClipData and a ClipData. The item for the data that is being transferred. Provide metadata that is kept in a ClipDescription object within the ClipData as part of the ClipData object. You might wish to use null instead of a real object for a drag and drop action that does not reflect data transfer.
  2. Continuing: If the drag event listener returns true, the program can initiate the drag event. The drag event is in progress, which means it has begun but has not yet finished. For example, if you wish to drag a TextView called tv1 from position 1 to position 2, the intermediate states between the two are in the continuous state.
  3. Dropped: The dragged item is released within the bounding box of a View. The system sends a drag event with the action type ACTION DROP to the View object’s listener.
  4. Ended: When the user drops the item after dragging it and all the events connected with that drop state are called, the system sends a signal to the application that the drop action is complete and all the needed functions are performed. As a result, the system shows that the drag and drop event has ended.

Events Involving Drag

Refer to the table below to understand all the different events involving drag:

What does it mean?

ACTION_DRAG_ENTERED When a drag shadow enters the bounding box of a View object, the drag event listener receives this event action type. When the drag shadow enters the bounding box, the listener receives the first event action type. If the listener wants to receive drag events for this action in the future, it must return a boolean true to the system. ACTION_DRAG_LOCATION DRAG LOCATION This event action type is received by the drag event listener of a View object when it gets an ACTION DRAG ENTERED event while the drag shadow is still within the bounding box of the View. ACTION_DRAG_STARTED The drag event listener of a View object receives this event action type immediately after the application runs startDrag() and obtains a drag shadow. ACTION_DRAG_EXITED This event action type is received by the drag event listener of a View object when it receives an ACTION DRAG ENTERED and at least one ACTION DRAG LOCATION event, and after the user has dragged the drag shadow outside the bounding box of the View. ACTION_DROP

When the user releases the drag shadow over the View object, the drag event listener on the View object receives this event action type. This action type is only delivered to the listener of a View object if the listener responded true in response to the ACTION DRAG STARTED drag event. If the user releases the drag shadow on a View whose listener is not registered, or if the user releases the drag shadow on anything that is not part of the current layout, this action type is not transmitted.

If the drop is properly processed, the listener is supposed to return boolean true. If not, it should return false.

ACTION_DRAG_ENDED Notifies a View that the drag-and-drop action is complete.

Effects and Shadows in the Drags

The system shows an image that the user drags during a drag and drop operation. This graphic depicts the data being pulled in terms of data mobility. The picture illustrates some parts of the drag process for other procedures.

The picture is known as a drag shadow. You build it by declaring methods for a View.DragShadowBuilder object and then passing it to the system when you start a drag with startDrag (). The system uses the callback methods described in View.DragShadowBuilder to obtain a drag shadow as part of its answer to startDrag().

Example

In this example, we will drag and drop a text view within a bounding region from one location to another.

Step #1: First, add the application’s layout File

Источник

Android — Using drag and drop in your application — Tutorial

Using drag and drop in Android. This tutorial describes how to use drag and drop in Android. It is based on Eclipse 4.4 (Luna), Java 1.7 and Android 5.0.

1. Android drag and drop

1.1. Using drag and drop in Android

As of Android 4.0 drag and drop of view onto other views or view groups is supported.

1.2. Allowing a view to be dragged

To use dragging a view you register a OnTouchListener or a LongClickListener on the view which can be dragged.

The startDrag method of the View start a drag operation. In this method you also specify the data which is passed to the drop target via an instance of ClipData .

You also pass to the startDrag method an instance of DragShadowBuilder . This object specifies the picture used for the drag operation. For example you can pass in the view directly, that shows an image of the view during the drag operation.

The setup of this drag operation in a touch listener is demonstrated in the following example.

1.3. Defining drop target

The views which can be drop targets get an instance of OnDragListener assigned. In this drop listener you receive call backs in case of predefined drag and drop related events. * DragEvent.ACTION_DRAG_STARTED * DragEvent.ACTION_DRAG_ENTERED * DragEvent.ACTION_DRAG_EXITED * DragEvent.ACTION_DROP * DragEvent.ACTION_DRAG_ENDED

A view with OnDragListener which are used as a drop zone, gets a OnDragListener assigned via the setOnDragListener .

Источник

Читайте также:  Ниссан жук монитор андроид
Оцените статью