Skip to content

Instantly share code, notes, and snippets.

@pcevikogullari
Last active October 4, 2016 14:08
Show Gist options
  • Save pcevikogullari/2583e99c6dd2c8a57920f46213473810 to your computer and use it in GitHub Desktop.
Save pcevikogullari/2583e99c6dd2c8a57920f46213473810 to your computer and use it in GitHub Desktop.
Draggable Button
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