Jul 4, 2011

Moving Images



import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.RelativeLayout.LayoutParams;

public class Main extends Activity implements OnTouchListener {
private ImageView mImg1, mImg2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mImg1 = (ImageView) findViewById(R.id.xImg1);
mImg2 = (ImageView) findViewById(R.id.xImg2);
mImg1.setOnTouchListener(this);
mImg2.setOnTouchListener(this);

}
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (v == mImg1) {
x1 = 0;
y1 = 0;
LayoutParams mLayoutParams = (LayoutParams) mImg1.getLayoutParams();
if (event.getAction() == MotionEvent.ACTION_MOVE) {
x1 = (int) event.getRawX();
y1 = (int) event.getRawY();
mLayoutParams.leftMargin = x1 - 5;
mLayoutParams.topMargin = y1 - 60;
mImg1.setLayoutParams(mLayoutParams);
check();
}
}
else if (v == mImg2)
{
x2 = 0;
y2 = 0;
LayoutParams mLayoutParams = (LayoutParams) mImg2.getLayoutParams();
if (event.getAction() == MotionEvent.ACTION_MOVE) {
x2 = (int) event.getRawX();
y2 = (int) event.getRawY();
mLayoutParams.leftMargin = x2 - 5;
mLayoutParams.topMargin = y2 - 60;
mImg2.setLayoutParams(mLayoutParams);
check();
}

}

return true;
}

boolean b = false;

private void check() {

if (x1 == x2 || y1 == y2) {
if (!b)
{
b = true;
Toast.makeText(getApplicationContext(), " interected ", 0).show();
}
}
}

}


For Source Clik Here

No comments:

Post a Comment