Alright? I have a code where I can move several images with the mouse but when dragging an image it goes over the others. I want to move it without going over it.
Follow the codes:
package br.com.example.teste8;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends Activity implements View.OnTouchListener {
private ImageView mImageView;
private ViewGroup mRrootLayout;
private int _xDelta;
private int _yDelta;
RelativeLayout.LayoutParams layoutParams;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRrootLayout = (ViewGroup) findViewById(R.id.marco);
mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50,50);
layoutParams.leftMargin = 0;
layoutParams.topMargin = 10;
mImageView.bringToFront();
mImageView.setLayoutParams(layoutParams);
mImageView.setOnTouchListener(this);
mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView2);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(50,50);
layoutParams1.leftMargin = 50;
layoutParams1.topMargin = 10;
mImageView.setLayoutParams(layoutParams1);
mImageView.setOnTouchListener(this);
mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView3);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(50,50);
layoutParams2.leftMargin = 100;
layoutParams2.topMargin = 10;
mImageView.setLayoutParams(layoutParams2);
mImageView.setOnTouchListener(this);
}
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -50;
layoutParams.bottomMargin = -50;
view.setLayoutParams(layoutParams);
break;
}
mRrootLayout.invalidate();
return true;
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/marco"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:clickable="true">
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:gravity="top" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"
android:contentDescription="@null"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/border"
android:contentDescription="@null"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="48dp"
android:background="@drawable/border"
android:contentDescription="@null"
android:src="@drawable/ic_launcher" />
</RelativeLayout>