The functionality I am trying to achieve is dragging an image (imageView) that is centered at the bottom of the screen to an image that is centered in the middle of the screen. The issue I am facing is that as I drag my icon, the actual layout surrounding the icon resizes. And once the surrounding layout touches the center of the screen, this constitutes having dragged the image to the center of the screen. But really this did not happen, the layout surrounding the image reached the center. Additionally, the image disappears once I begin the drag it.
I need some help with re-configuring my code. I followed a tutorial. Here is what I have.
windowWidth = getWindowManager().getDefaultDisplay().getWidth();
windowHeight = getWindowManager().getDefaultDisplay().getHeight();
MarginLayoutParams marginParams = new MarginLayoutParams(ivDragObject.getLayoutParams());
marginParams.setMargins((windowWidth/24)*10, ((windowHeight/32)*8), 0, 0);
RelativeLayout.LayoutParams layoutDragObject = new RelativeLayout.LayoutParams(marginParams);
ivDragObject.setLayoutParams(layoutDragObject);
LinearLayout llDestination = (LinearLayout) findViewById(R.id.llDestination);
llDestination.setPadding(0,0,0,(windowHeight/32)*3);
MarginLayoutParams marginParamsDestination = new MarginLayoutParams(ivDestination.getLayoutParams());
marginParamsDestination.setMargins((windowWidth/24)*10, 0, (windowHeight/32)*8, 0);
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(marginParamsDestination);
ivDestination.setLayoutParams(layout);
ivDragObject.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
layoutParams = (LayoutParams) v.getLayoutParams();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
int[] unlockPos = new int[2];
lockPos = new int[2];
ivHome.getLocationOnScreen(unlockPos);
endPointX = unlockPos[0];
endPointY = unlockPos[1];
break;
case MotionEvent.ACTION_MOVE:
int xCord = (int) event.getRawX();
int yCord = (int) event.getRawY();
if (xCord > windowWidth - (windowWidth/24)) {
xCord = windowWidth - (windowWidth/24) * 2;
}
if (yCord > windowHeight - (windowHeight/32)) { //32
yCord = windowHeight - (windowHeight/32) * 2;
}
layoutParams.leftMargin = xCord;
layoutParams.topMargin = yCord;
ivLock.getLocationOnScreen(lockPos);
v.setLayoutParams(layoutParams);
if (((xCord - endPointX) <= (windowWidth/24)*5 && (endPointX - xCord) <= (windowWidth/24)*4)
&& ((endPointY - yCord) <= (windowHeight/32)*5)) {
Log.e(TAG, "Reached destination");
} else {
Log.e(TAG, "NOT OVERLAP");
}
break;
case MotionEvent.ACTION_UP:
int xCordUp = (int) event.getRawX();
int yCordUp = (int) event.getRawY();
if (((xCordUp - endPointX) <= (windowWidth/24)*5 && (endPointX - xCordUp) <= (windowWidth/24)*4)
&& ((endPointY - yCordUp) <=(windowHeight/32)*5)) {
Log.e(TAG, "Reach destination");
} else {
layoutParams.leftMargin = (windowWidth/24)*10;
layoutParams.topMargin = (windowHeight/32)*8;
v.setLayoutParams(layoutParams);
}
}
return true;
}
});
I also have these methods
public void onSlideTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
int xCord = (int) event.getRawX();
int yCord = (int) event.getRawY();
if (xCord > windowWidth) {
xCord = windowWidth;
}
if (yCord > windowHeight) {
yCord = windowHeight;
}
layoutParams.leftMargin = xCord - 25;
layoutParams.topMargin = yCord - 75;
view.setLayoutParams(layoutParams);
break;
default:
break;
}
}
@Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (keyCode == KeyEvent.KEYCODE_POWER)
|| (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
|| (keyCode == KeyEvent.KEYCODE_CAMERA)) {
return true;
}
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
return true;
}
return false;
}
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER
|| (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (event.getKeyCode() == KeyEvent.KEYCODE_POWER)) {
return false;
}
if ((event.getKeyCode() == KeyEvent.KEYCODE_HOME)) {
return true;
}
return false;
}
Thank you in advance.