Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use correct dragging of a view on android

it seems that there are a lot of solutions of how to use dragging of views on android .

however, they are either too slow or buggy.

a known solution is to have a framelayout , and the view simply changes its layoutParams as it moves using the OnTouchListener ,as described here: http://www.anddev.org/novice-tutorials-f8/android-drag-and-drop-of-normal-views-not-bitmaps-t13404.html however, if the view is an imageView , when reaching the most right (or most bottom) side of the framelayout , the image shrinks .

another possible solution is to use the canvas in order to show the dragged content , as described here: http://www.anddev.org/basic_drag_and_drop-t3095.html however, i'm not sure how to use it for complex views.

does anyone know of a good solution , one that let the size of the dragged view stay the same , no matter where it is in its container?

please help me .

like image 879
android developer Avatar asked Oct 25 '11 16:10

android developer


People also ask

How do you drag views on Android?

You can call startDragAndDrop() on any View in the current layout. The system uses the View object only to get access to global settings in the layout. During the drag and drop operation, the system sends drag events to the drag event listeners or callback methods of the View objects in the layout.

How do you drag an image on Android?

There is no direct way to do drag and drop in Android. You have to write some classes. Look into DragController. java, DragLayer.

Does Android have drag and drop?

The Android drag and drop framework enables you to add interactive drag and drop capabilities to your app. With drag and drop, users can copy or move text, images, objects—any content that can be represented by a URI—from one View to another within an app or, in multi-window mode, between apps.


1 Answers

Here's a complete solution for a simple imageView dragging:

findViewById(R.id.imageView).setOnTouchListener(new OnTouchListener()
  {
    int prevX,prevY;

    @Override
    public boolean onTouch(final View v,final MotionEvent event)
      {
      final FrameLayout.LayoutParams par=(FrameLayout.LayoutParams)v.getLayoutParams();
      switch(event.getAction())
        {
        case MotionEvent.ACTION_MOVE:
          {
          par.topMargin+=(int)event.getRawY()-prevY;
          prevY=(int)event.getRawY();
          par.leftMargin+=(int)event.getRawX()-prevX;
          prevX=(int)event.getRawX();
          v.setLayoutParams(par);
          return true;
          }
        case MotionEvent.ACTION_UP:
          {
          par.topMargin+=(int)event.getRawY()-prevY;
          par.leftMargin+=(int)event.getRawX()-prevX;
          v.setLayoutParams(par);
          return true;
          }
        case MotionEvent.ACTION_DOWN:
          {
          prevX=(int)event.getRawX();
          prevY=(int)event.getRawY();
          par.bottomMargin=-2*v.getHeight();
          par.rightMargin=-2*v.getWidth();
          v.setLayoutParams(par);
          return true;
          }
        }
      return false;
      }
  });
like image 134
android developer Avatar answered Sep 22 '22 11:09

android developer