Skip to content

Instantly share code, notes, and snippets.

@MensObscura
Last active September 13, 2016 08:56
Show Gist options
  • Save MensObscura/c6b925af3ab63829458c60c340a7d288 to your computer and use it in GitHub Desktop.
Save MensObscura/c6b925af3ab63829458c60c340a7d288 to your computer and use it in GitHub Desktop.
public void movedListener() {
if (mImageFloating != null) {
final GestureDetector gestureDetector = new GestureDetector(getActivity(), new SingleTapConfirm());
final float maxWidth = UIUtils.getScreenWidthInPx(getContext()) - DisplayMetricsUtils.convertDpToPx(getContext(), 70);
final float maxHeight = UIUtils.getScreenHeightInPx(getContext()) - DisplayMetricsUtils.convertDpToPx(getContext(), 200);
final float minWidth = DisplayMetricsUtils.convertDpToPx(getContext(), 10);
final float minHeight = minWidth;
mImageFloating.setOnTouchListener(new View.OnTouchListener() {
public boolean touch = false;
public float dy;
public float dx;
double easingAmount = 0.15;
public void move(float moveX, float moveY) {
float xDistance = moveX - mImageFloating.getX();
float yDistance = moveY - mImageFloating.getY();
double distance = Math.sqrt(xDistance * xDistance + yDistance * yDistance);
if (distance > 1) {
mImageFloating.setX((float) (mImageFloating.getX() + (xDistance * easingAmount)));
mImageFloating.setY((float) (mImageFloating.getY() + (yDistance * easingAmount)));
}
}
public void moveLoop(final float moveX, final float moveY, final int milli) {
float xDistance = moveX - mImageFloating.getX();
float yDistance = moveY - mImageFloating.getY();
double distance = Math.sqrt(xDistance * xDistance + yDistance * yDistance);
if (distance > 1 && !touch) {
mImageFloating.setX((float) (mImageFloating.getX() + (xDistance * easingAmount)));
mImageFloating.setY((float) (mImageFloating.getY() + (yDistance * easingAmount)));
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
moveLoop(moveX, moveY, milli);
}
}, milli);
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (gestureDetector.onTouchEvent(motionEvent)) {
onCardClicked();
return true;
}
Log.w("chatHead", "event = " + motionEvent.getRawX());
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
touch = true;
Log.e("chatHead", "MotionEvent.ACTION_DOWN");
dx = view.getX() - motionEvent.getRawX();
dy = view.getY() - motionEvent.getRawY();
break;
case MotionEvent.ACTION_UP:
Log.e("chatHead", "MotionEvent.ACTION_UP");
float finalY = motionEvent.getRawY() + dy;
if (finalY > maxHeight) {
finalY = maxHeight;
}
if (finalY < minHeight) {
finalY = minHeight;
}
touch = false;
moveLoop(maxWidth, finalY, 5);
break;
case MotionEvent.ACTION_MOVE:
Log.e("chatHead", "MotionEvent.ACTION_MOVE");
float newY = motionEvent.getRawY() + dy;
float newX = motionEvent.getRawX() + dx;
move(newX, newY);
break;
default:
return true;
}
return true;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment