Thursday, September 19, 2013

[android help] Image drop to previous position


Image drop to previous position



Touch listener not work. I need to drag the image over screen and drop to previous position itself.


For that i write the code as below:



class Mylistener implements OnTouchListener{

@Override
public boolean onTouch(View v, MotionEvent event) {
MarginLayoutParams marginParams = new MarginLayoutParams(v.getLayoutParams());
layoutParams2 = (RelativeLayout.LayoutParams) v.getLayoutParams();


//int left=0,top=0;
switch(event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
startx=(int) event.getRawX();
starty=(int) event.getRawY();
System.out.println("ACTION DOWN: "+startx+","+starty);
break;
case MotionEvent.ACTION_MOVE:


int left = (int) event.getRawX() - (v.getWidth() / 2);
int top = (int) event.getRawY() - (v.getHeight());
marginParams.setMargins(left, top, 0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
v.setLayoutParams(layoutParams);

break;

case MotionEvent.ACTION_UP:

marginParams.setMargins(startx, starty, 0, 0);
layoutParams2 = new RelativeLayout.LayoutParams(marginParams);
System.out.println("ACTION UP: "+startx+","+starty);

v.setLayoutParams(layoutParams2);
default:
break;
}
return true;
}

}


But it drag over the screen and not drop in the previous position. Please provide me any help



Read more

stackoverflow.comm



No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...