Android Drag and Drop Images in One place to Another place


ACTIVITY

public class MainActivity extends Activity  {
  int windowwidth;
  int windowheight;  
  ImageView ima1,ima2;

  private android.widget.RelativeLayout.LayoutParams layoutParams ;       

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        windowwidth = getWindowManager().getDefaultDisplay().getWidth();
        windowheight = getWindowManager().getDefaultDisplay().getHeight();

        System.out.println("width" +windowwidth);
        System.out.println("height" +windowheight);          

        ima1 = (ImageView)findViewById(R.id.imageview1);
        ima1.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
      layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams();

        switch(event.getAction())                
           {
             case MotionEvent.ACTION_DOWN:                        
                   break;  

             case MotionEvent.ACTION_MOVE:
                   int x_cord = (int) event.getRawX();
                   int y_cord = (int) event.getRawY();

             System.out.println("value of x" +x_cord);
             System.out.println("value of y" +y_cord);        

                   if (x_cord > windowwidth) {
                       x_cord = windowwidth;
                      }
                   if (y_cord > windowheight) {
                       y_cord = windowheight;
                      }
            layoutParams.leftMargin = x_cord-25;
            layoutParams.topMargin = y_cord-25;        
            ima1.setLayoutParams(layoutParams);
                    break;
              default: break;
             }
              return true;
           }
        });

        ima2 = (ImageView)findViewById(R.id.imageview2);
        ima2.setOnTouchListener(new View.OnTouchListener() {      

    public boolean onTouch(View v, MotionEvent event) {
        layoutParams = (RelativeLayout.LayoutParams) ima2.getLayoutParams();
             switch(event.getActionMasked())
                {
                  case MotionEvent.ACTION_DOWN:
                      break;
                  case MotionEvent.ACTION_MOVE:
                      int x_cord = (int) event.getRawX();
                      int y_cord = (int) event.getRawY();

                      System.out.println("value of x1" +x_cord);
                  System.out.println("value of y1" +y_cord);                          

                       if (x_cord > windowwidth) {
                           x_cord = windowwidth;
                       }
                       if (y_cord > windowheight) {
                           y_cord = windowheight;
                       }
                       layoutParams.leftMargin = x_cord - 25;
                       layoutParams.topMargin = y_cord - 75;
                       ima2.setLayoutParams(layoutParams);
                       break;
                   default: break;
               }
               return true;
           }
       });
      }
  }

XML


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageview1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/imageview2"
        android:layout_width="100sp"
        android:layout_height="100sp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

6 comments:

HrishithaReddy said...

Thanks It is useful.Did you know the code for letter recognization through gesture or witting in the screen.

Hrishitha Reddy said...
This comment has been removed by a blog administrator.
Anonymous said...

Thanks it is useful

Hrishitha Reddy said...

Thanks it is useful. If possible please post a code for letter recognization through gesture or writting on screen.

NagarjunaReddy said...

no idea of that one.....

NagarjunaReddy said...

@Pavithra you welcome....

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...