Created
December 3, 2014 11:34
-
-
Save orhanobut/8ec9959b7007886164a6 to your computer and use it in GitHub Desktop.
Move a view with fingers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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