Note that there are some explanatory texts on larger screens.

plurals
  1. POtwo image dragging
    primarykey
    data
    text
    <p>I am making a module in which I have two images whenever i touch one image it should follow the finger or mouse(in emulator) on drag and if it comes over the other image then they change their positions where the first image was on first touch(ACTION_DOWN) . i have written the following code in which the views are moving but when i drag the first image second is also dragged. Further would like to have idea on how to change the positions. </p> <p>.xml</p> <pre><code> &lt;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" &gt; &lt;ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;ImageView android:id="@+id/img1" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>activity file</p> <pre><code>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 &gt; w) x = w; if(y &gt; 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; } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload