First of all, use a RelativeLayout in your xml file:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
android:id="@+id/my_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<imageview>
android:id="@+id/image1"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/splash0"/>
<imageview>
android:id="@+id/image2"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/splash0"
android:layout_x="110dp"
android:layout_y="152dp"/>
</imageview></imageview></relativelayout>
Then change your activity, see my example:
public class MyActivity extends Activity implements View.OnTouchListener {
private float oldXvalue;
private float oldYvalue;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image1 = (ImageView) findViewById(R.id.image1);
ImageView image2 = (ImageView) findViewById(R.id.image2);
image1.setOnTouchListener(this);
image2.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
oldXvalue = me.getX();
oldYvalue = me.getY();
Log.i("Omid", "Action Down " + oldXvalue + "," + oldYvalue);
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.my_relative_layout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());
params.leftMargin = (int) (me.getRawX() - (v.getWidth() / 2));
params.topMargin = (int) (me.getRawY() - (v.getHeight()));
v.getHeight(), (int) (me.getRawX() - (v.getWidth() / 2)), (int) (me.getRawY() - (v.getHeight())));
v.setLayoutParams(params);
}
return true;
}
}
I tested it, it worked well. I hope it'll be fine for your :-)