Skip to content

Instantly share code, notes, and snippets.

@orhanobut
Created December 3, 2014 11:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orhanobut/8ec9959b7007886164a6 to your computer and use it in GitHub Desktop.
Save orhanobut/8ec9959b7007886164a6 to your computer and use it in GitHub Desktop.
Move a view with fingers
//set imageview listener
imageView.setOnTouchListener(onButtonTouchListener);
//control the movement
private final View.OnTouchListener onButtonTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
return true;
case MotionEvent.ACTION_MOVE:
int x = (int) event.getRawX();
int y = (int) event.getRawY();
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) v.getLayoutParams();
params.topMargin = y - v.getHeight()/2;
params.leftMargin = x - v.getWidth()/2;
params.gravity = Gravity.NO_GRAVITY;
v.setLayoutParams(params);
return true;
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment