Two drag and drop images

I create a module in which I have two images whenever I touch one image, it must follow the finger or mouse (in the emulator) when dragging and if it happens on a different image, then they change their positions, where the first first touch image (ACTION_DOWN). I wrote the following code in which the views move, but when I drag the first image, the second also drags. Next, I would like to have an idea of ​​how to change positions.

.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/vg" > <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 

activity file

 public class MainActivity extends Activity { private View selected_item = null; private int offset_x = 0; private int offset_y = 0; Canvas can; Paint paint; ImageView img; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewGroup vg = (ViewGroup)findViewById(R.id.vg); vg.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getActionMasked()) { case MotionEvent.ACTION_MOVE: if(selected_item == img) { int x = (int)event.getX() - offset_x; int y = (int)event.getY() - offset_y; int w = getWindowManager().getDefaultDisplay().getWidth() - 100; int h = getWindowManager().getDefaultDisplay().getHeight() - 100; if(x > w) x = w; if(y > h) y = h; LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( new ViewGroup.MarginLayoutParams( 100, 100)); lp.setMargins(x, y, 0, 0); selected_item.setLayoutParams(lp); } break; default: break; } return true; } }); img = (ImageView)findViewById(R.id.img); //timerDelayRemoveView(500, img); BitmapDrawable drawable = (BitmapDrawable)getResources().getDrawable(R.drawable.imagesl_02); Bitmap bitmap = drawable.getBitmap(); Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true); img.setImageBitmap(scaledBitmap); LinearLayout.LayoutParams lp0 = new LinearLayout.LayoutParams(100, 100); lp0.leftMargin = 0; lp0.topMargin = 0; img.setLayoutParams(lp0); //vg.addView(img, lp1); // vg.addView(img, 1); img.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getActionMasked()) { case MotionEvent.ACTION_DOWN: offset_x = (int)event.getX(); offset_y = (int)event.getY(); selected_item = v; Toast.makeText(MainActivity.this, "down",Toast.LENGTH_SHORT).show(); break; default: break; } return false; } }); ImageView img1 = (ImageView)findViewById(R.id.img1); BitmapDrawable drawable1 = (BitmapDrawable)getResources().getDrawable(R.drawable.realimage); Bitmap bitmap1 = drawable1.getBitmap(); Bitmap scaledBitmap1 = Bitmap.createScaledBitmap(bitmap1, 100, 100, true); img1.setImageBitmap(scaledBitmap1); LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(100, 100); lp1.leftMargin = 100; lp1.topMargin = 100; img1.setLayoutParams(lp1); //img.setImageBitmap(scaledBitmap1); img1.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getActionMasked()) { case MotionEvent.ACTION_DOWN: // offset_x = (int)event.getX(); // offset_y = (int)event.getY(); selected_item = v; break; default: break; } return false; } }); } } 
+6
source share
2 answers

The second image is being dragged because you have two in LinearLayout. The second parameters of the image layout depend on the first, which means that when you adjust them to the right, the second image is tied to the right edge of the first.

If I approached this problem (assuming that I could not use the drag and drop API introduced in Honeycomb), I would first put everything in a RelativeLayout with 2 ImageView s. When you β€œpick” one of the images, you adjust its layout settings when you move it, and then when you drop it, you configure LayoutParams to satisfy the desired layout.

+2
source

findViewById(R.drawable.realimage) must be findViewById(R.id.realimage) .

I think you should use id instead of drawable .

+1
source

Source: https://habr.com/ru/post/922544/


All Articles