Last active
October 4, 2016 14:08
-
-
Save pcevikogullari/2583e99c6dd2c8a57920f46213473810 to your computer and use it in GitHub Desktop.
Draggable Button
This file contains hidden or 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
public class DraggableView extends Button { | |
float dX; | |
float dY; | |
public DraggableView(Context context) { | |
super(context); | |
} | |
public DraggableView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public DraggableView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent motionEvent) { | |
switch (motionEvent.getActionMasked()){ | |
case MotionEvent.ACTION_DOWN: | |
dX = getX() - motionEvent.getRawX(); | |
dY = getY() - motionEvent.getRawY(); | |
break; | |
case MotionEvent.ACTION_MOVE: | |
setY(motionEvent.getRawY() + dY); | |
setX(motionEvent.getRawX() + dX); | |
break; | |
case MotionEvent.ACTION_UP: | |
break; | |
default: | |
return false; | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment